Skip to content
Nginx

Load Balancing

Distribute traffic across multiple upstream servers.

By EZ4Code Team
load-balancingupstreamha

Code

# Define an upstream pool
upstream app_backend {
    least_conn;                          # or ip_hash; or default RR
    server 10.0.0.1:3000 weight=3;
    server 10.0.0.2:3000 weight=2;
    server 10.0.0.3:3000 backup;         # only if primaries fail
    keepalive 32;                        # upstream keep-alive pool
}

server {
    listen 80;

    location / {
        proxy_pass http://app_backend;
        proxy_http_version 1.1;
        proxy_set_header Connection "";  # enable upstream keepalive
        proxy_next_upstream error timeout http_502 http_503;
        proxy_next_upstream_tries 3;
    }
}

Explanation

The upstream block defines a pool of backend servers that Nginx load-balances with optional methods: round-robin (default), least_conn, or ip_hash for session stickiness. The backup flag designates fallback servers used only when primaries fail. keepalive reuses upstream connections to reduce latency.

More Nginx Snippets