Nginx Passive Health Checks: Failures, Retries and Failover
A failed backend is not necessarily a failed website
Suppose a catalog has two identical read-only API processes. Nginx can observe a failed attempt to reach one process and use the other, provided the request is eligible for another attempt. Passive health detection learns from ordinary traffic; it does not send its own periodic probes. If no requests arrive, there is no new passive evidence.
upstream catalog_pool { zone catalog_pool 64k; server 127.0.0.1:18191 max_fails=2 fail_timeout=10s; server 127.0.0.1:18192 max_fails=2 fail_timeout=10s;}server { listen 127.0.0.1:18190; location / { proxy_pass http://catalog_pool; proxy_next_upstream error timeout; proxy_next_upstream_tries 2; proxy_next_upstream_timeout 3s; proxy_connect_timeout 1s; proxy_read_timeout 2s; }}The group shares peer state between workers using zone. Two qualifying failures within the configured observation window can temporarily exclude a peer; the same fail_timeout value governs the exclusion interval. Defaults are one qualifying failure and ten seconds. Setting max_fails to zero disables this accounting. A group with just one server is a special case: max_fails and fail_timeout do not exclude that server. A backup peer can provide capacity after eligible primary peers become unavailable.
Separate retry eligibility from failure accounting
For HTTP proxying, communication errors, timeouts and invalid response headers count as failed attempts even when the retry policy does not include them. Statuses 500, 502, 503, 504 and 429 count only if selected in proxy_next_upstream; 403 and 404 do not count as unsuccessful attempts. The default retry policy covers communication errors and timeouts. A healthy application can legitimately return 404 for an absent catalog item.
t=0s peer A: connect refused; first qualifying failuret=3s peer A: timeout; second qualifying failuret=4s peer B: response 200After exclusion interval: A may receive another eligible requestThe timeline assumes two attempts to A inside the window and no other activity. It is not a prediction of which exact request round robin selects. When a peer becomes eligible again, it has not necessarily recovered. A successful response is stronger evidence than elapsed time.
Bound retries and protect writes
location /catalog/ { proxy_pass http://catalog_pool; proxy_next_upstream error timeout http_503; proxy_next_upstream_tries 2; proxy_next_upstream_timeout 3s;}location /orders/ { proxy_pass http://catalog_pool; proxy_next_upstream off;}This read route assumes safe read-only operations. Two tries include the initial attempt; three seconds bounds the retry period, not the entire response deadline. Nginx cannot switch peers after sending response data to the client. A timeout after an application commits a write can leave the outcome unknown. Do not opt into non_idempotent retries casually; safe write recovery requires an application-level idempotency contract, not just another proxy attempt. Disabling retries does not guarantee exactly-once execution.
Observe a failover
status=200 upstream=127.0.0.1:18191, 127.0.0.1:18192 upstream_status=502, 200status=200 upstream=127.0.0.1:18192 upstream_status=200The first line suggests a failed attempt followed by a successful attempt; the second shows a direct success on B. Logs can have multiple upstream values; do not treat them as one integer. Use the staged Chapter 18 lab to stop one lab-owned backend, make a small finite number of requests, and restart it. Record peer markers and final status; never promise zero lost requests.
If a client and gateway each make three attempts, a single logical operation can lead to nine backend attempts. Use bounded attempts, appropriate deadlines and backoff at the application layer.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Nginx Passive Health Checks: Failures, Retries and Failover
Predict when a peer is temporarily avoided, how a bounded retry works and what evidence proves recovery.
What triggers passive health detection?
What happens to fail_timeout in a one-server group?
Does HTTP 404 count as a failed peer attempt?
What does proxy_next_upstream_tries 2 include?
A write times out after reaching the application. What is safe to conclude?