Configuring Nginx for Load Balancing

Step 1: Define the Backend Server Pool

Inside the http block, define an upstream block with your backend servers. Each server can be configured with various parameters:

upstream backend_cluster {
    # weight: Higher value means more traffic distribution
    # max_fails: Number of failed requests before marking server as unavailable
    # fail_timeout: Time window to wait before retrying a failed server
    # down: Marks server as permanently unavailable
    # backup: Used only when all primary servers are down
    
    server 127.0.0.1:8080 weight=1 max_fails=3 fail_timeout=20s;
    server 127.0.0.1:8081 weight=2 max_fails=3 fail_timeout=20s;
    server 127.0.0.1:8082 backup;
}

Step 2: Configure the Proxy Server

Create a server block that listens for incoming requests and forwards them to the upstream group:

server {
    listen       80;
    server_name  api.example.com;
    
    # Set maximum upload size
    client_max_body_size 15M;
    
    location / {
        proxy_pass http://backend_cluster;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

Complete Configuraton Example

Below is a complete nginx.conf file demonstrating load balancing setup:

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    
    sendfile        on;
    keepalive_timeout  65;
    
    # Load balancing upstream definition
    upstream backend_cluster {
        server 127.0.0.1:8080 weight=1 max_fails=3 fail_timeout=20s;
        server 127.0.0.1:8081 weight=2 max_fails=3 fail_timeout=20s;
        server 127.0.0.1:8082 backup;
    }
    
    # Default server
    server {
        listen       80 default_server;
        server_name  _;
        
        location / {
            root   html;
            index  index.html index.htm;
        }
    }
    
    # Frontend server
    server {
        listen       80;
        server_name  www.example.com;
        root         /var/www/html;
        
        location / {
            try_files $uri $uri/ /index.html;
        }
    }
    
    # API server with load balancing
    server {
        listen       80;
        server_name  api.example.com;
        client_max_body_size 15M;
        
        location / {
            proxy_pass http://backend_cluster;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
        }
    }
}

Key Configuration Praameters

  • weight: Controls traffic distribution ratio. A server with weight=2 receives twice as many requests as weight=1.
  • max_fails: Sets the threshold for marking a server as unavailable after consecutive failures.
  • fail_timeout: Defines how long Nginx waits before attempting to send requests to a failed server.
  • backup: Designates a server as a fallback that only receives traffic when all primary servers are down.
  • down: Permanently removes a server from the rotation.

Tags: nginx Load Balancing reverse proxy upstream Server Configuration

Posted on Tue, 06 Oct 2026 16:30:09 +0000 by john_zakaria