The ngx_http_upstream_hc_module
module allows enabling periodic health checks of the servers in a group referenced in the surrounding location. The server group must reside in the shared memory.
If a health check fails, the server will be considered unhealthy. If several health checks are defined for the same group of servers, a single failure of any check will make the corresponding server be considered unhealthy. Client requests are not passed to unhealthy servers and servers in the “checking” state.
Please note that most of the variables will have empty values when used with health checks.
This module is available as part of our commercial subscription.
upstream dynamic { zone upstream_dynamic 64k; server backend1.example.com weight=5; server backend2.example.com:8080 fail_timeout=5s slow_start=30s; server 192.0.2.1 max_fails=3; server backup1.example.com:8080 backup; server backup2.example.com:8080 backup; } server { location / { proxy_pass http://dynamic; health_check; } }
With this configuration, nginx will send “/
” requests to each server in the backend
group every five seconds. If any communication error or timeout occurs, or a proxied server responds with the status code other than 2xx or 3xx, the health check will fail, and the server will be considered unhealthy.
Health checks can be configured to test the status code of a response, presence of certain header fields and their values, and the body contents. Tests are configured separately using the match directive and referenced in the match
parameter of the health_check directive:
http { server { ... location / { proxy_pass http://backend; health_check match=welcome; } } match welcome { status 200; header Content-Type = text/html; body ~ "Welcome to nginx!"; } }
This configuration shows that in order for a health check to pass, the response to a health check request should succeed, have status 200, and contain “Welcome to nginx!
” in the body.
Syntax: | health_check [parameters]; |
---|---|
Default: | — |
Context: | location |
Enables periodic health checks of the servers in a group referenced in the surrounding location.
The following optional parameters are supported:
interval
=time
jitter
=time
fails
=number
passes
=number
uri
=uri
/
”. mandatory
match
=name
match
block configuring the tests that a response should pass in order for a health check to pass. By default, the response should have status code 2xx or 3xx. port
=number
Syntax: | match name { ... } |
---|---|
Default: | — |
Context: | http |
Defines the named test set used to verify responses to health check requests.
The following items can be tested in a response:
status 200;
status ! 500;
status 200 204;
status ! 301 302;
status 200-399;
status ! 400-599;
status 301-303 307;
header Content-Type = text/html;
text/html
header Content-Type != text/html;
text/html
header Connection ~ close;
close
header Connection !~ close;
close
header Host;
header ! X-Accel-Redirect;
body ~ "Welcome to nginx!";
Welcome to nginx!
” body !~ "Welcome to nginx!";
Welcome to nginx!
” If several tests are specified, the response matches only if it matches all tests.
Only the first 256k of the response body are examined.
Examples:
# status is 200, content type is "text/html", # and body contains "Welcome to nginx!" match welcome { status 200; header Content-Type = text/html; body ~ "Welcome to nginx!"; }
# status is not one of 301, 302, 303, or 307, and header does not have "Refresh:" match not_redirect { status ! 301-303 307; header ! Refresh; }
# status ok and not in maintenance mode match server_ok { status 200-399; body !~ "maintenance mode"; }
© 2002-2018 Igor Sysoev
© 2011-2018 Nginx, Inc.
Licensed under the BSD License.
https://nginx.org/en/docs/http/ngx_http_upstream_hc_module.html