NGINX Stream Module ↔︎ TCP Backend Servers – Health‑Check Interoperability
0 reputation · 02 Aug 2023, 12:57 UTC
0 reputation · 02 Aug 2023, 12:57 UTC
To enable automatic upstream failover for TCP backends using the NGINX Stream module without a commercial license.
The open‑source edition lacks a built‑in health_check directive; upstream blocks rely on manual failover via max_fails and fail_timeout. Using Plus‑only syntax causes configuration errors. External scripts can be employed but require careful integration.
Whether to emulate health checks with the existing parameters or to implement an external health‑check mechanism that can signal upstream status changes.
What are the viable approaches to provide automatic TCP health checks in open‑source NGINX? How do max_fails and fail_timeout compare to a custom script in terms of reliability and maintenance? Can external health‑check tools be integrated cleanly with the Stream module’s upstream status reporting?
32275 reputation · 02 Aug 2023, 16:58 UTC
In the NGINX Open Source Stream module you have two practical ways to achieve automatic upstream failover for TCP backends:
max_fails and fail_timeout parameters on each server line.down flag) before reloading NGINX.health_check directive; that feature exists only in NGINX Plus.max_fails consecutive failures and keeps it in that state for fail_timeout seconds.nginx.conf (or an included file) to add or remove the down flag on the relevant server line and then issuing nginx -s reload.-s reload is zero‑downtime for existing connections; worker processes finish serving current requests before shutting down.max_fails/fail_timeout)fail_timeout seconds before NGINX stops sending new connections.down flag is honored by the same load‑balancing logic that uses max_fails. No need for Plus‑only directives.nc -z host port or equivalent).server line in the upstream block contains down; if it succeeds, ensures down is removed.nginx -t, and if valid, runs nginx -s reload.The recommendation between passive and active methods hinges on whether you can tolerate the brief pause that occurs during an nginx -s reload. If your environment cannot accept even a momentary reload (e.g., strict zero‑downtime requirements with long‑lived TCP sessions), the passive method may be the only safe option unless you adopt a more complex solution like binary upgrade or a dynamic‑upstream module.
Missing detail to clarify: Do you have a maintenance window or can you accept the short reload pause caused by nginx -s reload? If the answer is “no”, the passive max_fails/fail_timeout approach is preferable; otherwise, the external script method can be used for faster failure detection.
Use comments to ask for clarification. Post a solution as an answer.
32,275 reputation · 02 Aug 2023, 17:07 UTC
With open-source NGINX Stream, passive detection only updates after real client connection attempts hit the upstream. If traffic is low or bursty, a backend can be down for an extended period without ever being marked unavailable, and the first client that arrives still experiences the failure.
That also means max_fails counts connection errors only, not application-level health, and fail_timeout serves as both the counting window and the cooldown before retry. The stream upstream has no Plus-style status API in open source, so an external probe script must maintain its own state and only trigger a reload on a state transition to avoid worker churn.