Skip to content

Upstream health checks in Nginx — passive, active, and what the open-source build can do

How Nginx decides a backend is unhealthy in the open-source build (passive checks), the active-check options (NGINX Plus, nginx_upstream_check_module), and the operational pattern we use.

Open-source Nginx does not do active health checks on its upstreams — it does passive ones. The distinction matters: passive checks notice failure only when a real request happens to hit a dead backend, accept that it failed, and then ejects the backend for fail_timeout. Active checks would poll the backend independently. This article covers what the open-source build can and cannot do, the operational pattern around it, and the third-party module option when you really do need active probing.

How to verify

See what is configured today:

sudo nginx -T | grep -E 'max_fails|fail_timeout|health_check'
sudo grep -E 'upstream|connect\(\) failed|no live upstreams' /var/log/nginx/error.log | tail
# observe whether traffic is hitting all backends
for i in 1 2 3 4 5 6; do curl -s https://example.com/health 2>&1; echo; done

If error.log shows connect() failed followed by upstream … is unavailable, passive checks are doing their job. If you see no live upstreams while connecting to upstream, every backend is currently marked down.

What’s happening

Each server line in an upstream block carries max_fails=N fail_timeout=T. The semantics: if N requests fail within fail_timeout (default 10s), the backend is marked unavailable for fail_timeout. After that window, Nginx tentatively tries again — if the next request succeeds, the backend is back in rotation.

“Fail” by default means a TCP/connect or read/send timeout, plus any status in proxy_next_upstream (default: error, timeout, invalid_header). A 500 from the backend does not count as a failure by default — you have to add http_500 to proxy_next_upstream if you want Nginx to re-try on the next backend.

Active checks (Nginx polls the backend on its own) are part of NGINX Plus, the commercial product. The community alternative is nginx_upstream_check_module from Yaoweibin or its forks — these add the check directive inside the upstream block.

The procedure

  1. The passive-checks baseline — what we set on every upstream block:
    upstream app {
        least_conn;
    
        server 10.0.1.11:3000 max_fails=3 fail_timeout=30s;
        server 10.0.1.12:3000 max_fails=3 fail_timeout=30s;
        server 10.0.1.13:3000 backup;
    
        keepalive 32;
    }
    3 failures in 30 seconds is forgiving enough that flaky-but-recoverable backends are not ejected; strict enough that a truly-down one drops out fast.
  2. Decide what counts as failure. In the server block:
    location / {
        proxy_pass http://app;
        proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
        proxy_next_upstream_tries 3;
        proxy_next_upstream_timeout 10s;
    }
    Adding http_500 means a single 5xx from one backend re-tries on the next — at the cost of doubling load on the rest of the pool during a backend incident.
  3. The application-side health endpoint. Build /healthz (or /livez) into every app that returns 200 only when the app can actually serve traffic — DB connection alive, cache warm, etc. Then your load balancer in front of Nginx (or another orchestrator) can call it.
  4. External active probing. We use Prometheus blackbox-exporter or a small probe loop that GETs /healthz on each backend’s IP every 5 seconds. When a probe fails, we drain the backend from the upstream block via a config-management tool and reload Nginx. This decouples “is the backend healthy” from “does the next request happen to fail”.
  5. If passive checks are not enough — large multi-region pool, slow trickle of failures masking themselves — install nginx_upstream_check_module:
    upstream app {
        server 10.0.1.11:3000;
        check interval=3000 rise=2 fall=3 timeout=2000 type=http;
        check_http_send "GET /healthz HTTP/1.0\r\n\r\n";
        check_http_expect_alive http_2xx;
    }
    This requires patching and re-compiling Nginx (or running the OpenResty distribution which bundles it).
  6. Test draining. Stop a backend, hit the site repeatedly, watch the error log. The first request to the dead backend should fail; the next request should go elsewhere. After fail_timeout, Nginx will probe the dead one again — bring it back up and confirm it re-joins.
  7. Decide between backup and “in-rotation”. backup servers only take traffic when every primary is down. Useful for a hot spare in a different region; not useful for a “drained for deploy” backend (just remove that line instead).

Common pitfalls

  • max_fails=0 disables passive checks entirely. Useful for a single-backend dev box; dangerous in production — Nginx will keep sending requests to a dead box forever.
  • Counting http_500 as failure on an app where 500 is a normal user-error response. Now every form validation error retries on the next backend, doubling load.
  • proxy_next_upstream_tries N is the max number of retries — if all backends fail in sequence, the user gets the last failure. Make sure this matches the pool size.
  • Active checks via curl-in-cron that probe https://your-site/ rather than the backend directly. You are testing your own load balancer, not the backend.
  • Forgetting that “passive” implies a failed request happened. The first user during an outage hits the dead backend and sees the error; only the second sees the failover. A leaderboard service that fails one request per second still fails one request per second until ejection.

In the engagements we run, we treat upstream membership as data: an authoritative inventory (Terraform, ASG state, Kubernetes endpoints) generates the upstream block on each reload. That way, “the backend is marked unhealthy” is a property of the orchestrator, not something Nginx finds out by sending a failed request to a real user. Stack Harbor wraps this into clustered environments.