Sub topic 8
Handling Failed Backends with Passive Checks and Retries
Assume you know basic Nginx configuration, HTTP status codes, and how to define an upstream group. This chapter focuses on what Nginx does after a backend connection fails or returns an upstream error.
How passive health checks work
Nginx uses passive health checks during normal traffic. It does not continuously call a separate health endpoint. Instead, it watches real requests sent to each upstream server.
The max_fails parameter sets how many unsuccessful attempts can occur during the fail_timeout period. After that limit, Nginx temporarily marks the server unavailable. The server becomes eligible again after fail_timeout expires.
An unsuccessful attempt can include a connection error, a timeout, or another failure that Nginx treats as an upstream failure. A normal application response such as 404 does not usually count as a failed server.
The following group has two primary servers and one backup server:
upstream app_backend {
least_conn;
server 10.0.0.11:8080 weight=3 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;
}The first server receives more traffic because its weight is higher. Each primary server can have three unsuccessful attempts in 30 seconds before Nginx avoids it temporarily. The backup server receives traffic only when all non-backup servers are unavailable.
| Setting | Meaning | Important detail |
|---|---|---|
| `max_fails` | Failed attempts allowed in the failure window | `0` disables passive failure marking |
| `fail_timeout` | Failure window and temporary unavailable period | It also controls how long Nginx avoids a failed server |
| `backup` | Server used after primary servers become unavailable | Backup servers are not selected while a primary is available |
Retrying failed requests
proxy_next_upstream tells Nginx which failures allow it to try another upstream server. This example retries on connection errors, timeouts, and selected gateway errors:
upstream app_backend {
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;
}
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_next_upstream error timeout http_502 http_503;
proxy_next_upstream_tries 2;
proxy_next_upstream_timeout 5s;
}
}proxy_next_upstream_tries limits the number of upstream attempts. proxy_next_upstream_timeout limits the total time spent trying servers. Without limits, retries can increase request latency.
Nginx can retry only while it can still choose another server and before it has sent a response to the client. Retrying a POST request can be dangerous: the first backend might have completed the operation before its response was lost. The client could then cause the operation to run twice. Enable retries for operations that are idempotent, such as many GET requests, or make application operations safely repeatable.
Checking and diagnosing failures
Validate the configuration before every reload:
sudo nginx -t && sudo systemctl reload nginxLog the selected upstream address and response timing to identify failing or slow servers. A 502 often means Nginx could not obtain a valid response. A 504 usually indicates an upstream timeout. Check the Nginx error log and the backend's own logs together.
Exercise
Two primary servers fail, and a server marked backup starts receiving requests. What does this show?
Show answer
Nginx has marked the primary servers unavailable through passive failure handling and selected the backup server.