Skip to content
Nginx

Load Balancing

Distribute traffic across multiple upstream servers.

#load-balancing#upstream#ha

Code

nginx
# 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;
    }
}