Module ngx_http_upstream_hc_module

ngx_http_upstream_hc_module模块允许对在周围位置引用的group中的服务器进行定期运行状况检查。服务器组必须位于shared memory中。

如果运行状况检查失败,则服务器将被视为不正常。如果为同一组服务器定义了多个运行状况检查,则任何一次检查失败都会使相应的服务器不正常。客户端请求不会传递到运行状况不佳的服务器以及处于“检查”状态的服务器。

Note

请注意,与运行状况检查一起使用时,大多数变量将具有空值。

Note

此模块可作为commercial subscription的一部分使用。

Example Configuration

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 将每五秒钟向backend组中的每个服务器发送“ /”请求。如果发生任何通信错误或超时,或者代理的服务器以 2xx 或 3xx 以外的状态代码响应,则运行状况检查将失败,并且服务器将被视为不正常。

可以将运行状况检查配置为测试响应的状态代码,某些 Headers 字段及其值的存在以及主体内容。使用match指令分别配置测试,并在health_check指令的match参数中引用测试:

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

Syntax:health_check [parameters];
Default:
Context:location

对周围位置引用的group中的服务器启用定期运行状况检查。

支持以下可选参数:

interval= time

  • 设置两次连续运行状况检查之间的间隔,默认情况下为 5 秒。

jitter= time

  • 设置每次运行状况检查将随机延迟的时间,默认情况下,没有延迟。

fails= number

  • 设置特定服务器连续失败的健康检查次数,在此之后该服务器将被视为不健康,默认情况下为 1.

passes= number

  • 设置特定服务器连续通过的运行状况检查的次数,之后该服务器将被视为运行状况良好,默认情况下为 1.

uri= uri

  • 定义健康检查请求中使用的 URI,默认情况下为“ /”。

mandatory

  • 设置服务器的初始“检查”状态,直到完成第一次运行状况检查(1.11.7)。客户端请求不会以“检查”状态传递到服务器。如果未指定该参数,则服务器最初将被视为运行状况良好。

match= name

  • 指定match块,配置响应应通过的测试以通过运行状况检查。默认情况下,响应应具有状态码 2xx 或 3xx。

port= number

  • 定义连接到服务器以执行运行状况检查(1.9.7)时使用的端口。默认情况下,等于server端口。

Syntax:match name { ... }
Default:
Context:http

定义用于验证对健康检查请求的响应的命名测试集。

可以在响应中测试以下项目:

  • status 200;

    • 状态是 200
  • status ! 500;

    • 状态不是 500
  • status 200 204;

    • 状态是 200 或 204
  • status ! 301 302;

    • 状态既不是 301 也不是 302
  • status 200-399;

    • 状态为 200 到 399
  • status ! 400-599;

    • 状态不在 400 到 599 的范围内
  • status 301-303 307;

    • 状态为 301、302、303 或 307
  • header Content-Type = text/html;

    • Headers 包含值为“ text/html”的“ Content-Type”
  • header Content-Type != text/html;

    • Headers 包含“ Content-Type”,其值不是text/html
  • header Connection ~ close;

    • Headers 包含值与正则表达式close匹配的“连接”
  • header Connection !~ close;

    • Headers 包含“连接”,其值与正则表达式close不匹配
  • header Host;

    • Headers 包含“主机”
  • header ! X-Accel-Redirect;

    • Headers 缺少“ X-Accel-Redirect”
  • body ~ "Welcome to nginx!";

    • 正文匹配正则表达式“ Welcome to nginx!
  • body !~ "Welcome to nginx!";

    • 正文与正则表达式“ Welcome to nginx!”不匹配

要求$ variable ...;

  • 所有指定的变量都不为空并且不等于“ 0”(1.15.9)。

如果指定了多个测试,则仅当匹配所有测试时,响应才匹配。

Note

仅检查响应主体的前 256k。

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";
}
# status is 200 or 204
map $upstream_status $good_status {
    200 1;
    204 1;
}

match server_ok {
    require $good_status;
}