Sub topic 6
Grouping Backends with `upstream`
Assume you know basic Nginx server and location blocks, HTTP status codes, and how to run services on TCP ports. This chapter uses two application servers listening on 10.0.0.11:8080 and 10.0.0.12:8080.
An upstream group is a named collection of backend servers. Nginx uses the group name in proxy_pass instead of writing one backend address directly. This separates routing policy from the location that handles the client request.
Define an upstream group
Place the upstream block inside the Nginx http context. Distribution packages commonly include files in /etc/nginx/conf.d/ from that context, so an upstream definition can usually be placed in a separate file there.
upstream app_backend {
server 10.0.0.11:8080;
server 10.0.0.12:8080;
}
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://app_backend;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}When a request reaches the location, Nginx selects one server from app_backend and sends the request there. The client sees Nginx as the server. The application servers do not need to be publicly reachable.
The default balancing method is round robin. Nginx sends requests to each available server in turn. This works well when the backends have similar capacity and requests take roughly similar amounts of time.
| Setting | Meaning |
|---|---|
| `server address:port` | Adds a backend to the group |
| `weight=N` | Sends more or fewer requests to a backend |
| `backup` | Uses the server only when regular servers are unavailable |
| `down` | Keeps a server in the configuration but never selects it |
Choose a balancing method
You can select a method by placing a directive at the start of the upstream block.
- Round robin is the default and is suitable for similar servers.
least_connselects the server with the fewest active connections. It can help when request durations vary.ip_hashusually sends the same client IP to the same backend. This can help older applications that store session state locally, but it can distribute traffic poorly when many users share one public IP.hash $request_uri consistentselects a backend from a hash of the request URI. This can keep the same resource on the same server, but changes to the server group can move requests.
Weights work with round robin and several other methods. A server with weight=3 receives about three times as many requests as a server with weight=1, assuming the servers remain available.
upstream app_backend {
least_conn;
server 10.0.0.11:8080 weight=3;
server 10.0.0.12:8080;
}Understand the request path
Nginx chooses a backend for each proxied request. It does not automatically share application state between those servers. If users need shared sessions, uploads, or other data, store that data in a shared database or another shared service.
Common errors
- Put
upstreaminsideserverorlocation. It belongs in thehttpcontext. - Use a name in
proxy_passthat does not exactly match the upstream name. - Forget that backend addresses must be reachable from the Nginx host.
- Use
ip_hashas a substitute for shared session storage. - Add a backend without checking its firewall rules, listening address, and application logs.
Test before applying a change:
sudo nginx -t && sudo systemctl reload nginxExercise
You have two equal application servers, but requests vary from 10 milliseconds to 30 seconds. Which balancing method is a reasonable first choice, and why?
Show answer
Use least_conn, because it prefers the server with fewer active connections and can avoid sending new long-running requests to a busy server.