Reverse proxy

Load-balance across multiple backends

3 min · updated June 16, 2026

When one app process isn’t enough, run several and let Nginx spread traffic across them. Declare an upstream pool at the http { } level, then proxy_pass to it by name.

upstream app_backend {
    least_conn;                       # send to the server with fewest active connections

    server 127.0.0.1:3001;
    server 127.0.0.1:3002;
    server 127.0.0.1:3003 weight=2;   # gets ~2x the traffic
    server 10.0.0.9:3000 backup;      # only used if the others are down

    keepalive 32;                     # pooled connections to the backends
}

server {
    listen 80;
    server_name example.com;

    location / {
        proxy_pass http://app_backend;

        proxy_http_version 1.1;
        proxy_set_header Connection "";        # required for upstream keepalive
        proxy_set_header Host            $host;
        proxy_set_header X-Real-IP       $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

Balancing methods (pick one, default is round-robin):

Other knobs:

sudo nginx -t && sudo nginx -s reload

← All snippets