NGINX Load Balancing: Round Robin, Least Connections, IP Hash and Health Checks

Configure NGINX upstream load balancing with round robin, weights, least_conn, ip_hash and hash. Learn passive health checks, backups and keepalive.

Published November 27, 2026

When one application server is not enough, NGINX can spread traffic across several. Load balancing adds capacity and removes single points of failure: if one backend dies, the others keep serving. NGINX open source supports several balancing algorithms and passive health checking with just a few lines of configuration.

The concept

Load balancing in NGINX is built around the upstream block, a named group of backend servers. proxy_pass then points at the group name instead of a single address. By default NGINX uses weighted round robin: requests go to each server in turn, with servers that have a higher weight receiving proportionally more.

Other methods change how the next server is chosen. least_conn sends each request to the server with the fewest active connections, which suits requests with very different durations. ip_hash and hash give "sticky" routing so the same client or key always reaches the same backend, which helps with local caches or legacy in-memory sessions. Each server can also be marked as backup, down, or given failure thresholds.

How an upstream distributes traffic

Clientsmany requestsNGINX upstreamleast_connapp1:8080weight=3app2:8080weight=1app3:8080max_fails=3app4:8080backup
An upstream group spreading load across backends, with a backup server.

Balancing methods

MethodDirectiveHow it picksGood for
Round robin(default)Each server in turn, respecting weightSimilar, short requests
Least connectionsleast_connFewest active connectionsMixed request durations, WebSockets
IP haship_hashHash of client IPv4 /24 or full IPv6Simple stickiness
Generic hashhash $key consistentHash of any variableCache sharding by URL, sticky by cookie
Randomrandom two least_connPick two at random, choose the less loadedMany NGINX instances sharing backends

Weights in practice

app1 (weight 3)60%app2 (weight 1)20%app3 (weight 1)20%
Share of requests with weighted round robin, weights 3:1:1.

Passive health checks

NGINX open source detects failures passively: when requests to a server fail (connection refused, timeout, or responses you list in proxy_next_upstream), it counts them. After max_fails failures within fail_timeout, the server is skipped for fail_timeout seconds, then tried again. Active health checks that probe a URL on a schedule are an NGINX Plus feature, or can be added with third-party modules.

upstream app {
    least_conn;
    server 10.0.0.11:8080 max_fails=3 fail_timeout=30s;
    server 10.0.0.12:8080 max_fails=3 fail_timeout=30s;
    server 10.0.0.13:8080 backup;
    keepalive 32;
}
server {
    location / {
        proxy_pass http://app;
        proxy_http_version 1.1;
        proxy_set_header Connection "";
        proxy_next_upstream error timeout http_502 http_503;
        proxy_next_upstream_tries 2;
    }
}

Practical examples

Sticky by cookie with generic hash

Route each logged-in user to the same backend based on a session cookie.

upstream app {
    hash $cookie_sessionid consistent;
    server 10.0.0.11:8080;
    server 10.0.0.12:8080;
}

Drain a server for maintenance

Mark it down, reload, deploy, then remove the flag.

upstream app {
    server 10.0.0.11:8080;
    server 10.0.0.12:8080 down;   # no new requests
}
# sudo nginx -t && sudo systemctl reload nginx

Load balance TCP (databases, MQTT) with stream

The stream context balances raw TCP/UDP.

stream {
    upstream pg_replicas {
        least_conn;
        server 10.0.1.21:5432;
        server 10.0.1.22:5432;
    }
    server { listen 5433; proxy_pass pg_replicas; }
}

Common mistakes

  • Retrying non-idempotent requests (POST) on another backend after a timeout, causing duplicate orders. NGINX does not retry non-idempotent methods by default unless you add non_idempotent.
  • Relying on ip_hash when most clients come from behind one corporate NAT, sending them all to the same server.
  • Forgetting keepalive and proxy_http_version 1.1, so every request opens a new TCP connection to the backend.
  • Assuming open-source NGINX actively probes health endpoints.
  • Using sticky sessions to hide state that should live in Redis or a database.

Key takeaways

  • upstream defines a backend group; proxy_pass points at it.
  • Round robin is the default; least_conn suits uneven request times; hash gives stickiness.
  • max_fails and fail_timeout provide passive health checking.
  • Enable upstream keepalive for efficiency and be careful with retries of POST requests.

FAQ

Can NGINX balance across servers in different data centres?

Yes, but latency differences matter. Use weights or a separate upstream per region with DNS or anycast deciding the region.

How does NGINX resolve hostnames in upstream?

At startup or reload only (open source). For changing DNS, use the resolver directive with a variable in proxy_pass, or the resolve parameter available in newer versions.

Is NGINX a replacement for a cloud load balancer?

It can be. Many teams run NGINX behind a cloud load balancer anyway, letting the cloud LB handle failover of the NGINX nodes themselves.

More Nginx articles