If your website outgrows a single VPS, you need to distribute traffic across multiple servers. Nginx can act as a load balancer, routing requests to multiple backend servers and improving performance and reliability.

This guide shows you how to configure Nginx as a load balancer.

What Is Load Balancing?

Load balancing distributes incoming traffic across multiple backend servers. This improves performance, increases capacity, and provides redundancy. If one server fails, others continue serving traffic.

Prerequisites

You need at least two backend servers running your application. Nginx acts as the frontend, accepting requests and forwarding them to one of the backends.

Step 1: Install Nginx

sudo apt update
sudo apt install nginx -y

Step 2: Configure Upstream Servers

Edit your Nginx configuration file:

sudo nano /etc/nginx/nginx.conf

Add this in the http block:

upstream backend_servers {
    server 192.168.1.101:80;
    server 192.168.1.102:80;
}

Replace the IP addresses with your backend server IPs.

Step 3: Configure the Proxy

Create a site configuration:

sudo nano /etc/nginx/sites-available/loadbalancer

Add this configuration:

server {
    listen 80;
    server_name example.com;

    location / {
        proxy_pass http://backend_servers;
        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;
    }
}

Step 4: Load Balancing Methods

Nginx supports several load balancing methods:

Round Robin (default): Requests are distributed evenly across servers.

Least Connections: Requests go to the server with the fewest active connections:

upstream backend_servers {
    least_conn;
    server 192.168.1.101:80;
    server 192.168.1.102:80;
}

IP Hash: Requests from the same IP go to the same server (useful for session persistence):

upstream backend_servers {
    ip_hash;
    server 192.168.1.101:80;
    server 192.168.1.102:80;
}

Step 5: Enable the Site

sudo ln -s /etc/nginx/sites-available/loadbalancer /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx

Step 6: Health Checks

If a backend server fails, Nginx will detect it if you add health checks:

upstream backend_servers {
    server 192.168.1.101:80 max_fails=3 fail_timeout=30s;
    server 192.168.1.102:80 max_fails=3 fail_timeout=30s;
}

If a server fails 3 times within 30 seconds, Nginx stops sending traffic to it.

Troubleshooting

502 Bad Gateway – One or more backend servers are unreachable. Check your IP addresses and ensure the servers are running.

Session persistence issues – If users lose their session data, use ip_hash to route the same user to the same backend.

Need more VPS instances to set up load balancing? Check our recommended VPS providers.