nginx

Nginx Load Balancing Configuration

Nginx load balancing config with round-robin, weighted, and least-connections strategies plus health checks.

Overview

Nginx can distribute incoming traffic across multiple backend servers to improve reliability and throughput. This configuration covers common load balancing strategies, health checks, and sticky sessions for stateful applications.

Configuration

# /etc/nginx/conf.d/load-balancer.conf

# Round-robin upstream (default strategy)
upstream app_servers {
    # Each server gets requests in turn
    server 10.0.1.10:3000;            # App server 1
    server 10.0.1.11:3000;            # App server 2
    server 10.0.1.12:3000;            # App server 3
    server 10.0.1.13:3000 backup;     # Only used when others are down

    keepalive 32;                     # Persistent connections per worker
}

# Weighted upstream — send more traffic to powerful servers
upstream app_weighted {
    server 10.0.1.10:3000 weight=5;   # Gets 5x more requests
    server 10.0.1.11:3000 weight=3;   # Gets 3x more requests
    server 10.0.1.12:3000 weight=1;   # Gets 1x (baseline)
}

# Least-connections — send to server with fewest active connections
upstream app_least_conn {
    least_conn;                       # Enable least-connections algorithm
    server 10.0.1.10:3000;
    server 10.0.1.11:3000;
    server 10.0.1.12:3000;
}

# IP hash — same client always hits same server (sticky sessions)
upstream app_sticky {
    ip_hash;                          # Hash client IP for consistent routing
    server 10.0.1.10:3000;
    server 10.0.1.11:3000;
    server 10.0.1.12:3000;
    server 10.0.1.13:3000 down;       # Temporarily removed from rotation
}

server {
    listen 80;
    server_name app.example.com;

    location / {
        proxy_pass http://app_servers;
        proxy_http_version 1.1;

        # Pass client info to backends
        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;
        proxy_set_header Connection "";

        # Passive health checks
        proxy_next_upstream error timeout http_500 http_502 http_503;
        proxy_next_upstream_tries 3;       # Try up to 3 servers
        proxy_next_upstream_timeout 10s;   # Give up after 10s total

        # Timeouts
        proxy_connect_timeout 5s;          # Fast fail on connect
        proxy_read_timeout 30s;
        proxy_send_timeout 30s;
    }

    # Status page for monitoring (restrict access)
    location /nginx-status {
        stub_status on;
        allow 10.0.0.0/8;                 # Internal network only
        deny all;
    }
}

Key Options Explained

  • Round-robin (default) — Requests cycle through servers sequentially. Simple and effective for stateless apps.
  • weight=N — Assigns proportional traffic to servers. A server with weight=5 gets 5 times the traffic of weight=1.
  • least_conn — Routes to the server with the fewest active connections. Best for requests with varying processing times.
  • ip_hash — Hashes the client IP to assign a consistent backend. Useful when sessions are stored in server memory.
  • backup — Server only receives traffic when all non-backup servers are unavailable.
  • down — Marks a server as permanently unavailable without removing it from config.
  • proxy_next_upstream — Automatically retries the request on another server if the current one returns an error or times out.

Common Modifications

  • Slow start: Add slow_start=30s to a server directive so it gradually receives more traffic after recovery (Nginx Plus only).
  • Max connections: Add max_conns=100 to limit concurrent connections to a backend server.
  • Max failures: Add max_fails=3 fail_timeout=30s to mark a server as down after 3 failures within 30 seconds.
  • Hash on custom key: Use hash $request_uri consistent; to route by URL path instead of client IP.
  • Active health checks: Nginx Plus supports health_check interval=5s with custom match conditions inside location blocks.