3.4 HTTP Health Checks

Problem

You need to actively check your upstream HTTP servers for health.

问题

需要主动检测 HTTP 服务器健康状态


Solution

Use the health_check directive in a location block:

http {
    server {
        ...
        location / {
            proxy_pass http://backend;
            health_check interval=2s
                         fails=2
                         passes=5
                         uri=/
                         match=welcome;
        }
    }

    # 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!";
    }
}

This health check configuration for HTTP servers checks the health

of the upstream servers by making an HTTP request to the URI '/'

every two seconds. The upstream servers must pass five consecutive

health checks to be considered healthy and will be considered

unhealthy if they fail two consecutive checks. The response from the

upstream server must match the defined match block, which defines

the status code as 200, the header Content-Type value as 'text/

html', and the string "Welcome to nginx!" in the response body.

解决方案

在 location 块级指令中使用 health_check 指令检测:

http {
    server {
        ...
        location / {
            proxy_pass http://backend;
            health_check interval=2s
                         fails=2
                         passes=5
                         uri=/
                         match=welcome;
        }
    }

    # 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!";
    }
}

上例,通过向被代理服务器每隔 2 秒,发送一个到 '/' URI 的请求来检测

被代理服务器是否失效。被代理服务器连续接收 5 个请求,如果其中有 2 个

连续请求响应失败,将被视作服务器失效。被代理服务器的健康响应格式

在 match 块级指令中配置,规定响应状态码为 200, 响应 Content-Type类型为

'text/html',响应 body 为 "Welcome to nginx!" 字符串的响应为有效服务器。


Discussion

HTTP health checks in NGINX Plus can measure more than just

the response code. In NGINX Plus, active HTTP health checks

monitor based on a number of acceptance criteria of the response

from the upstream server. Active health check monitoring can be

configured for how often upstream servers are checked, the URI to

check, how many times it must pass this check to be considered

healthy, how many times it can fail before being deemed unhealthy,

and what the expected result should be. The match parameter points

to a match block that defines the acceptance criteria for the

response. The match block has three directives: status, header, and

body. All three of these directives have comparison flags as well.

结论

在 NGINX PLUS 版本中,除了通过响应状态码来判断被代理服务器是否有效。

还能够通过其它的一些响应指标来判断是否有效。如:主动检测的时间间隔

(频率),主动请求的 URI 地址,健康检测的请求次数及失败次数和预期响应

结果等。在 health_check 指令中的 match 参数指向 match 块级指令配置,

match 块级指令配置定义了标准的响应,包括 status、header 和 body 指令,

他们都有各自的检测标准。


资料(译者补充)

这篇文章 「HTTP Health Checks」 是 NGINX 服务器官网的管理员运维教程,主要讲解开源版本

和 NGINX PLUS(商业版) 的 HTTP 健康检测配置处理

TODO 翻译「HTTP Health Checks」

results matching ""

    No results matching ""