Mastering Nginx Health Checks: Configurations, Parameters, and Best Practices
This article explains how Nginx can automatically register services and remove unhealthy nodes by using TCP or HTTP health‑check schemes, details each configuration parameter, and provides a complete example configuration for reliable service governance.
Service governance requires automatic service registration and the removal of abnormal nodes, demanding timely and accurate detection of service health.
Solution Overview
Nginx offers three HTTP health‑check schemes for users to choose from:
TCP default check : periodically establishes a TCP connection to the backend; a successful connection means the node is healthy.
HTTP default check : addresses the limitations of TCP checks, such as stateful HTTP services and inability to reflect internal processing congestion. It sends an HTTP request (e.g., GET / HTTP/1.0\r\n\r\n) and treats 2xx or 3xx responses as healthy.
Custom scheme : users can define their own checks as described below.
Configuration Parameter Details
check
The check directive defines the health‑check behavior.
check fall=3 interval=3000 rise=2 timeout=2000 type=http;</code><code>check_http_expect_alive http_2xx http_3xx ;</code><code>check_http_send "GET /checkAlive HTTP/1.0
" ;Syntax:
Syntax: check interval=milliseconds [fall=count] [rise=count] [timeout=milliseconds] [default_down=true|false] [type=tcp|http|ssl_hello|mysql|ajp] [port=check_port]Default values (if omitted):
interval=30000 fall=5 rise=2 timeout=1000 default_down=true type=tcp interval: interval between health‑check packets. fall: number of consecutive failures after which the server is considered down. rise: number of consecutive successes after which the server is considered up. timeout: timeout for the health‑check request. default_down: initial state of the server; true means it starts as down. type: type of health‑check packet; supported types include tcp, ssl_hello, http, mysql, ajp, and port.
check_http_expect_alive
Specifies which HTTP response codes are considered successful for active health checks.
Syntax: check_http_expect_alive [ http_2xx | http_3xx | http_4xx | http_5xx ]</code><code>Default: http_2xx | http_3xxcheck_http_send
Configures the content of the HTTP health‑check request. Using the HEAD method reduces data transfer. For long‑connection checks, add a Connection: keep-alive header, e.g., HEAD / HTTP/1.1\r\nConnection: keep-alive\r\n\r\n. When using GET, keep the URI size small to fit within one interval.
Syntax: check_http_send http_packet</code><code>Default: "GET / HTTP/1.0
"Complete Example
http {</code><code> upstream cluster1 {</code><code> # simple round-robin</code><code> server 192.168.0.1:80;</code><code> server 192.168.0.2:80;</code><code> check interval=3000 rise=2 fall=5 timeout=1000 type=http;</code><code> check_http_send "HEAD / HTTP/1.0
";</code><code> check_http_expect_alive http_2xx http_3xx;</code><code> }</code><code> upstream cluster2 {</code><code> # simple round-robin</code><code> server 192.168.0.3:80;</code><code> server 192.168.0.4:80;</code><code> check interval=3000 rise=2 fall=5 timeout=1000 type=http;</code><code> check_keepalive_requests 100;</code><code> check_http_send "HEAD / HTTP/1.1
Connection: keep-alive
";</code><code> check_http_expect_alive http_2xx http_3xx;</code><code> }</code><code> server {</code><code> listen 80;</code><code> location /1 { proxy_pass http://cluster1; }</code><code> location /2 { proxy_pass http://cluster2; }</code><code> location /status {</code><code> check_status;</code><code> access_log off;</code><code> allow SOME.IP.ADD.RESS;</code><code> deny all;</code><code> }</code><code> }</code><code>}Source: https://ningg.top/nginx-series-health-check/
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
