模块 ngx_http_upstream_hc_module
| health_check match 的配置 Directives 示例 |
这ngx_http_upstream_hc_module模块
允许对周围位置引用的组中的服务器启用定期运行状况检查。
服务器组必须驻留在共享内存中。
如果运行状况检查失败, 服务器将被视为运行状况不佳。 如果为同一组服务器定义了多个运行状况检查, 任何检查的一次失败都将使相应的服务器 被视为不健康。 客户端请求不会传递到运行状况不佳的服务器 以及处于 “checking” 状态的服务器。
请注意,大多数变量都有空值 与运行状况检查一起使用时。
此模块作为我们商业订阅的一部分提供。
示例配置
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;
}
}
使用此配置,nginx 将向每个
server 中的/backend每 5 秒分组一次。
如果发生任何通信错误或超时,或者
代理服务器使用状态代码进行响应,而不是
2xx 或 3xx,则运行状况检查将失败,服务器将
被视为运行状况不佳。
运行状况检查可以配置为测试响应的状态代码,
存在某些标头字段及其值,
和正文内容。
使用 match Directives单独配置测试
并在match参数
health_checkDirectives中:
http {
server {
...
location / {
proxy_pass http://backend;
health_check match=welcome;
}
}
match welcome {
status 200;
header Content-Type = text/html;
body ~ "Welcome to nginx!";
}
}
此配置显示,为了使运行状况检查通过,响应
到运行状况检查请求应成功,状态为 200,
并包含”Welcome to nginx!“在体内。
Directives
| 语法: |
health_check [ |
|---|---|
| 默认值: | — |
| 上下文: |
location |
对周围位置引用的组中的服务器启用定期运行状况检查。
支持以下可选参数:
-
interval=time - 设置两个连续运行状况检查之间的间隔, 默认情况下为 5 秒。
-
jitter=time - 设置时间 每次健康检查都会随机延迟, 默认情况下,没有延迟。
-
fails=number - 设置特定服务器的连续运行状况检查失败次数 之后,此服务器将被视为运行状况不佳, 默认情况下为 1。
-
passes=number - 设置特定服务器连续通过的运行状况检查的次数 之后,服务器将被视为正常, 默认情况下为 1。
-
uri=uri - 定义运行状况检查请求中使用的 URI,
默认情况下为 “”。
/ -
mandatory[persistent] -
设置服务器的初始 “checking” 状态 直到第一次运行状况检查完成 (1.11.7)。 客户端请求不会传递到处于 “checking” 状态的服务器。 如果未指定该参数, 服务器最初将被视为正常。
这
persistent参数 (1.19.7) 设置重新加载后服务器的初始 “UP” 状态 如果在重新加载之前认为服务器运行状况良好。 -
match=name - 指定
matchblock 配置测试 response 应该通过,运行状况检查才能通过。 默认情况下,响应应具有状态代码 2xx 或 3xx。 -
port=number - 定义连接到服务器时使用的端口 执行运行状况检查 (1.9.7)。 默认情况下,等于服务器端口。
-
type=grpc[grpc_service=name] [grpc_status=code] - 启用定期运行状况
检查 gRPC 服务器
或使用可选的
grpc_service参数 (1.19.5)。 如果服务器不支持 gRPC 健康检查协议, 可选的grpc_status参数 要指定非零 gRPC 状态(例如, 状态代码 ”12” / “UNIMPLEMENTED”) 将被视为正常:
这health_check mandatory type=grpc grpc_status=12;
type=grpc参数 必须在所有其他Directives参数之后指定,grpc_service和grpc_status必须遵循type=grpc. 该参数与 不兼容uri或match参数。 -
keepalive_time=time - 启用用于运行状况检查的 keepalive 连接,并指定 可以通过一个 keepalive 连接 (1.21.7) 处理请求。 默认情况下,keepalive 连接处于禁用状态。
| 语法: |
match |
|---|---|
| 默认值: | — |
| 上下文: |
http |
定义用于验证对运行状况检查请求的响应的命名测试集。
可以在响应中测试以下项目:
status 200;- 状态为 200
status ! 500;- status 不是 500
status 200 204;- status 为 200 或 204
status ! 301 302;- status 既不是 301 也不是 302
status 200-399;- status 的范围为 200 到 399
status ! 400-599;- status 不在 400 到 599 的范围内
status 301-303 307;- status 为 301、302、303 或 307
header Content-Type = text/html;- header 包含 “Content-Type”
with value
text/html header Content-Type != text/html;- header 包含 “Content-Type”
值不是
text/html header Connection ~ close;- 标头包含 “Connection”
with 值匹配正则表达式
close header Connection !~ close;- 标头包含 “Connection”
with 值与正则表达式不匹配
close header Host;- 标头包含 “Host”
header ! X-Accel-Redirect;- 标头缺少 “X-Accel-Redirect”
body ~ "Welcome to nginx!";- body 与正则表达式 “ 匹配
Welcome to nginx!” body !~ "Welcome to nginx!";- body 与正则表达式 “ 不匹配
Welcome to nginx!”
require$variable...;- 所有指定的变量都不为空,也不等于 “0” (1.15.9)。
如果指定了多个测试,则 仅当响应与所有测试匹配时,响应才匹配。
仅检查响应正文的前 256k。
例子:
# 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";
}
# status is 200 or 204
map $upstream_status $good_status {
200 1;
204 1;
}
match server_ok {
require $good_status;
}