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

jitter= time

fails= number

passes= number

uri= uri

mandatory

match= name

port= number

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

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

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

要求$ variable ...;

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

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;
}
首页