Docs / Web Servers / Nginx Load Balancing Between Backend Servers

Nginx Load Balancing Between Backend Servers

By Admin · Jan 19, 2026 · Updated Apr 23, 2026 · 4 views · 3 min read

This guide covers how to set up and configure nginx on a Linux VPS. Whether you're running a production environment or a development setup, these instructions will help you get started quickly and securely.

Prerequisites

  • A registered domain name (for public-facing services)
  • SSL certificate or Certbot for Let's Encrypt
  • A web server installed (Nginx or Apache)
  • Basic familiarity with the Linux command line
  • Root or sudo access to the server

Server Installation

The default configuration works well for development environments, but production servers require additional tuning. Pay particular attention to connection limits, timeout values, and logging settings.


# /etc/nginx/sites-available/nginx.conf
server {
    listen 80;
    server_name example.com;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl http2;
    server_name example.com;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

    location / {
        proxy_pass http://127.0.0.1:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

This configuration provides a good balance between performance and resource usage. For high-traffic scenarios, you may need to increase the limits further.

Virtual Host Configuration

For production deployments, consider implementing high availability by running multiple instances behind a load balancer. This approach provides both redundancy and improved performance under heavy load.


# Test and reload Nginx configuration
sudo nginx -t
sudo systemctl reload nginx

# Check access and error logs
sudo tail -f /var/log/nginx/access.log
sudo tail -f /var/log/nginx/error.log

Note that file paths may vary depending on your Linux distribution. The examples here are for Debian/Ubuntu; adjust paths accordingly for RHEL/CentOS-based systems.

Configuration Options

The default configuration works well for development environments, but production servers require additional tuning. Pay particular attention to connection limits, timeout values, and logging settings.

SSL/TLS Setup

It's recommended to test this configuration in a staging environment before deploying to production. This helps identify potential compatibility issues and allows you to benchmark performance differences.


# Install Nginx with modules
sudo apt update
sudo apt install -y nginx

# Enable the site
sudo ln -s /etc/nginx/sites-available/nginx.conf /etc/nginx/sites-enabled/
sudo rm /etc/nginx/sites-enabled/default
sudo systemctl restart nginx

Each line in the configuration serves a specific purpose. The comments explain the reasoning behind each setting, making it easier to customize for your specific use case.

Performance Tuning

After applying these changes, monitor the server's resource usage for at least 24 hours to ensure stability. Tools like htop, iostat, and vmstat can provide real-time insights into system performance.


# /etc/nginx/sites-available/nginx.conf
server {
    listen 80;
    server_name example.com;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl http2;
    server_name example.com;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

    location / {
        proxy_pass http://127.0.0.1:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

Each line in the configuration serves a specific purpose. The comments explain the reasoning behind each setting, making it easier to customize for your specific use case.

Summary

You've successfully configured nginx on your VPS. Remember to monitor performance, keep your software updated, and maintain regular backups. If you run into issues, consult the official documentation or open a support ticket for assistance.

Was this article helpful?