nginx upstream节点健康检查
1、前提条件
编译nginx时增加nginx_upstream_check_module模板
git地址:https://github.com/yaoweibin/nginx_upstream_check_module
--add-module=/root/nginx_upstream_check_module/
2、健康检查方式
tcp,http最为常用,本文主要介绍这两种。
tcp检查方式:
upstream tcp_xxxx_backend {
server 192.168.1.100:8090;
server 192.168.1.101:8090;
check interval=3000 rise=2 fall=5 timeout=3000 type=tcp;
}
#间隔时间interval,默认单位毫秒
#rise,连续重试多少次成功后nginx则认为后端可用
#fall,连续多少次失败后nginx则认为不可用
#timeout,超时时间,默认单位毫秒
#type,健康检查后端方式
每3秒检查一次,连续失败5次就认为不可用,最长需要:3秒(间隔) + 3秒(超时) * 5次 = 30秒
每3秒检查一次,连续成功2次就认为可用,最短需要:3秒(间隔) * 2次 = 6秒
http检查方式:
upstream tcp_xxxx_backend {
server 192.168.1.102:8091;
server 192.168.1.103:8091;
check interval=3000 rise=2 fall=5 timeout=3000 type=http;
check_http_send "GET / HTTP/1.0\r\n\r\n";
check_http_expect_alive http_2xx;
}
或
upstream tcp_xxxx_backend {
server 192.168.1.102:8091;
server 192.168.1.103:8091;
check interval=3000 rise=2 fall=5 timeout=1000 type=http default_down=true;
check_http_send "HEAD /health_status HTTP/1.0\r\nHOST: xxx.xxxx.xx\r\n\r\n";
check_http_expect_alive http_2xx http_3xx;
}
#间隔时间interval,默认单位毫秒
#rise,连续重试多少次成功后nginx则认为后端可用
#fall,连续多少次失败后nginx则认为不可用
#timeout,超时时间,默认单位毫秒
#type,健康检查后端方式
每3秒检查一次,连续失败5次就认为不可用,最长需要:3秒(间隔) + 3秒(超时) * 5次 = 30秒
每3秒检查一次,连续成功2次就认为可用,最短需要:3秒(间隔) * 2次 = 6秒
3、upstream健康状态查看
#nginx.conf主配置需要增加如下配置
server {
listen 80;
server_name _;
access_log /data/logs/nginx/access_status.log main;
allow 192.168.0.0/16;
allow 127.0.0.1;
deny all;
location = /health_status {
check_status;
}
}
http://nginx ip/health_status,upstream状态查看:

4、生产环境配置
- 配置上建议多使用http健康检查方式;
- 应用最好有一个专门用于健康URL,类似/health,更能体现应用真正健康状态;
- 做好nginx的/health_status状态监控,如后端节点全down或长时间down节点要告警。