Sub topic 13
Load Balancing with Upstream Groups
This chapter assumes that you can edit Nginx configuration with sudo, understand HTTP requests, and run services that listen on TCP ports. You should also know how to test a configuration before reloading a systemd service.
An upstream group is a named list of application servers. Nginx sends requests to the group instead of sending every request to one fixed server. By default, Nginx uses round robin: the first request goes to one server, the next request goes to another, and the pattern continues.
Defining an upstream group
Place the upstream block inside the top-level http context. A server block can then refer to the group by name.
http {
upstream app_backend {
server 10.0.0.11:3000;
server 10.0.0.12:3000;
}
server {
listen 80;
server_name app.example.com;
location / {
proxy_pass http://app_backend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
}The proxy_pass directive uses the upstream group name, app_backend, rather than an IP address. Nginx resolves the group when it loads the configuration.
The application servers must be able to handle equivalent requests. If one server has files, sessions, or configuration that the other lacks, users may see inconsistent results. Store shared data in a common service, or use a deliberate session strategy.
Choosing a balancing method
Round robin is suitable when servers have similar capacity and requests take roughly similar amounts of time. You can select another method by adding a directive before the server entries.
| Method | Use it when |
|---|---|
| Round robin | Servers have similar capacity |
| `least_conn` | Requests have different processing times |
| `ip_hash` | A client should usually return to the same server |
For example, least_conn sends a new request to the server with the fewest active connections:
upstream app_backend {
least_conn;
server 10.0.0.11:3000;
server 10.0.0.12:3000;
}ip_hash can help legacy applications that keep sessions in local memory. It reduces flexibility when a server becomes overloaded, so shared sessions are usually a better long-term design.
Handling failed servers
Nginx marks a server as temporarily unavailable after connection or response failures. You can make the policy explicit:
upstream app_backend {
server 10.0.0.11:3000 max_fails=3 fail_timeout=30s;
server 10.0.0.12:3000 max_fails=3 fail_timeout=30s;
}This is passive failure detection. Nginx learns about failure while serving real requests. Open-source Nginx does not provide general active health checks that call an endpoint continuously. Use an external health-check system when you need active checks.
You can also mark a backend as a backup:
upstream app_backend {
server 10.0.0.11:3000;
server 10.0.0.12:3000 backup;
}The backup receives traffic only when the primary servers are unavailable.
After changing the configuration, test it before applying it:
sudo nginx -t && sudo systemctl reload nginxA successful syntax test does not prove that the backends are reachable. Check the Nginx error log and test the application through app.example.com.
Exercise
An upstream group contains two equal servers. Which balancing method is the default, and when might least_conn be a better choice?
Show answer
Round robin is the default. least_conn is better when requests take different amounts of time or keep connections open for different durations.