首页 > 解决方案 > 如何在 nginx 负载均衡器的多个端口上添加健康检查?

问题描述

我们正在使用 nginx 来平衡我们的应用程序。有 4 个节点需要以循环方式进行负载平衡。负载平衡工作正常。

运行时服务正在侦听端口 9001 ,该端口在内部重定向到同一节点上的其他服务。

所以我们在 nginx.conf 中定义了上游,状态文件为“cluster.state”。以下是 nginx.conf 的摘录

upstream cluster {
        zone cluster 64k;
        state /var/nginx/state/cluster.state;
}

以下是“服务器”块中用于路由呼叫的摘录:

location /apipattern {
    proxy_set_header Host $host:$server_port;
    proxy_read_timeout 300s;
    proxy_pass http://cluster/;
}
        

以下是 cluster.state 文件的摘录(更改了 FQDN,但端口正确)

server foobar1.com:9001 resolve;
server foobar2.com:9001 resolve;
server foobar3.com:9001 resolve;
server foobar4.com:9001 resolve;

要求是进行健康检查(对于 cluster.state 中提到的节点)。这些节点上的健康检查服务(2 个服务)在端口80818082上可用,uri=/healthcheck/isup(而不是在 9001 上)

我们如何配置这些健康检查?

标签: nginxnginx-config

解决方案


you can add multiple health_check directives under location directive with custom port and uri to have compound/multiple monitors for upstream. This is only possible with NGINX plus, which offers active health checking.

location /juice {
proxy_set_header Host $host;
proxy_pass http://juice/;
health_check port=800 uri=/custom.html;
health_check port=8081 uri=/hello.html;
}
    

推荐阅读