Load Balancing with upstream
Harry
· 12 Sep 2026
· 11 views
Multiple Backends
An upstream group balances traffic across identical app instances:
upstream app_servers {
server 10.0.0.11:8080;
server 10.0.0.12:8080;
server 10.0.0.13:8080;
}
server {
listen 80;
location / {
proxy_pass http://app_servers;
}
}Balancing Methods
- Round-robin (default) - every backend gets a fair turn.
- least_conn - send to the backend with fewest connections.
- ip_hash - route a client to the same backend every time (nice for sticky-but-not-sticky sessions).
- weight - give bigger servers more traffic:
server 10.0.0.11 weight=3;.
Health Checks and Failover
Nginx Open Source marks backends down when requests fail hard:
upstream app_servers {
server 10.0.0.11:8080 max_fails=3 fail_timeout=30s;
server 10.0.0.12:8080 backup; # used only if the others fail
}Nginx Plus adds active HTTP health checks with health_check interval=5s;.
Caveat: Sticky Sessions
If the app stores sessions in memory, ip_hash keeps users pinned. Better: move sessions to a shared store (Redis/DB) so any backend can serve anyone - that is how you scale.
Key Points
- upstream + proxy_pass balances and tolerates backend failures.
- Choose least_conn/weight by your workload; ip_hash for sticky needs.
- Stateless apps let load balancing do its best work.