Nginx Load Balancing
Definition
Load balancing spreads requests across several app servers. In Nginx you list backends in an upstream block and point proxy_pass at that group name.
Default method is round-robin. You can switch to least connections, IP hash (sticky-ish by client IP), and per-server weights.
Config
Start with this shape. Adjust paths for your server.
http {
upstream myapp1 {
server 127.0.0.1:3000;
server 127.0.0.1:3001;
server 127.0.0.1:3002;
}
server {
listen 80;
location / {
proxy_pass http://myapp1;
}
}
}
Use the upstream name after http:// in proxy_pass. No extra path is required for a full-site proxy.
Key Directives
| Name | Plain meaning |
|---|
upstream | Named group of backend servers. |
least_conn | Prefer the backend with the fewest active connections. |
ip_hash | Pick a backend from the client IP hash (session affinity). |
weight= | Send more traffic to stronger servers. |
max_fails / fail_timeout | Passive health: skip a failing server for a while. |
Examples
Example 1 — Default Round-Robin
With no method directive, each new request goes to the next server in turn.
upstream myapp1 {
server srv1.example.com;
server srv2.example.com;
server srv3.example.com;
}
How It Works
- Equal servers share traffic evenly over time.
- proxy_pass http://myapp1; enables balancing.
- Works for HTTP and, with https://, for HTTPS backends.
Example 2 — Least Connections
Better when some requests are slow — busy servers get fewer new ones.
upstream myapp1 {
least_conn;
server srv1.example.com;
server srv2.example.com;
}
How It Works
- least_conn goes at the top of the upstream block.
- Long requests do not pile onto one box as easily.
- Still passive health checks apply on errors.
Example 3 — Sticky-ish Sessions with ip_hash
The same client IP tends to hit the same backend — useful for simple in-memory sessions.
upstream myapp1 {
ip_hash;
server srv1.example.com;
server srv2.example.com;
}
How It Works
- Hash of client IP chooses the server.
- If that server is down, another is chosen.
- Not a full session store — shared Redis is still wiser for serious apps.
Example 4 — Weighted Servers
Give a stronger machine a higher weight so it receives more requests.
upstream myapp1 {
server srv1.example.com weight=3;
server srv2.example.com;
server srv3.example.com;
}
# roughly 3 of every 5 requests → srv1
How It Works
- Default weight is 1.
- weight=3 makes srv1 three times as likely in round-robin.
- Combine with least_conn or ip_hash in modern Nginx.
Frequently Asked Questions
No. proxy_pass http://127.0.0.1:3000; is enough for one app. upstream shines with two or more.
After max_fails errors within fail_timeout, Nginx avoids that server for a while, then probes again.
Yes — put servers in upstream and use fastcgi_pass http://mygroup; (same idea for uwsgi, etc.).
This runs inside Nginx on your machines. An ALB is a managed AWS service in front of instances.
No. Use ip_hash, sticky cookies (Plus), or an external session store.
Next: HTTPS servers
Keep going through the beginner Nginx path.
HTTPS servers → CODETOFUN NEWS
Stay Ahead in Tech
Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author
Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
8 people found this page helpful