Travis CI 构建详细信息:点击查看
(中文版本请参看这里)
Nginx 上游服务器的健康检查器(支持 http 上游 && 流上游)
该模块可以为NGINX提供主动后端服务器健康检查的能力(支持4台和7台后端服务器的健康检查)。
这个nginx模块还在开发中,你可以帮助改进它。
该项目在开发中也进展顺利,欢迎您贡献代码,或者报告bug。共同努力,让事情变得更好。
如果您有任何疑问,请联系我:
QQ
:373882405mail
:[email protected]当你使用nginx作为负载均衡器时,nginx原生只提供基本的重试来保证访问正常的后端服务器。
相比之下,这个nginx第三方模块为后端服务器提供主动健康状态检测。
它维护一个后端服务器列表,保证新请求直接发送到健康的后端服务器。
主要特点:
tcp
/ udp
/ http
http
/ fastcgi
html
/ json
/ csv
/ prometheus
html
/ json
/ csv
/ prometheus
check_http_expect_body ~ ".+OK.+";
git clone https://github.com/nginx/nginx.git
git clone https://github.com/zhouchangxun/ngx_healthcheck_module.git
cd nginx/;
git checkout branches/stable-1.12
git apply ../ngx_healthcheck_module/nginx_healthcheck_for_nginx_1.12+.patch
./auto/configure --with-stream --add-module=../ngx_healthcheck_module/
make && make install
返回目录
nginx.conf 示例
user root;
worker_processes 1 ;
error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024 ;
}
http {
server {
listen 80 ;
# status interface
location /status {
healthcheck_status json;
}
# http front
location / {
proxy_pass http://http-cluster;
}
}
# as a backend server.
server {
listen 8080 ;
location / {
root html;
}
}
upstream http-cluster {
# simple round-robin
server 127.0.0.1:8080;
server 127.0.0.2:81;
check interval=3000 rise=2 fall=5 timeout=5000 type=http;
check_http_send "GET / HTTP/1.0rnrn" ;
check_http_expect_alive http_2xx http_3xx;
}
}
stream {
upstream tcp-cluster {
# simple round-robin
server 127.0.0.1:22;
server 192.168.0.2:22;
check interval=3000 rise=2 fall=5 timeout=5000 default_down=true type=tcp;
}
server {
listen 522 ;
proxy_pass tcp-cluster;
}
upstream udp-cluster {
# simple round-robin
server 127.0.0.1:53;
server 8.8.8.8:53;
check interval=3000 rise=2 fall=5 timeout=5000 default_down=true type=udp;
}
server {
listen 53 udp;
proxy_pass udp-cluster;
}
}
状态接口
一种典型的输出是(json格式)
root @ changxun - PC : ~ / nginx - dev / ngx_healthcheck_module # curl localhost/status
{ "servers" : {
"total" : 6 ,
"generation" : 3 ,
"http" : [
{ "index" : 0 , "upstream" : "http-cluster" , "name" : "127.0.0.1:8080" , "status" : "up" , "rise" : 119 , "fall" : 0 , "type" : "http" , "port" : 0 },
{ "index" : 1 , "upstream" : "http-cluster" , "name" : "127.0.0.2:81" , "status" : "down" , "rise" : 0 , "fall" : 120 , "type" : "http" , "port" : 0 }
],
"stream" : [
{ "index" : 0 , "upstream" : "tcp-cluster" , "name" : "127.0.0.1:22" , "status" : "up" , "rise" : 22 , "fall" : 0 , "type" : "tcp" , "port" : 0 },
{ "index" : 1 , "upstream" : "tcp-cluster" , "name" : "192.168.0.2:22" , "status" : "down" , "rise" : 0 , "fall" : 7 , "type" : "tcp" , "port" : 0 },
{ "index" : 2 , "upstream" : "udp-cluster" , "name" : "127.0.0.1:53" , "status" : "down" , "rise" : 0 , "fall" : 120 , "type" : "udp" , "port" : 0 },
{ "index" : 3 , "upstream" : "udp-cluster" , "name" : "8.8.8.8:53" , "status" : "up" , "rise" : 3 , "fall" : 0 , "type" : "udp" , "port" : 0 }
]
}}
root @ changxun - PC : ~ / nginx - dev / ngx_healthcheck_module #
或(普罗米修斯格式)
root @ changxun - PC : ~ / nginx - dev / ngx_healthcheck_module # curl localhost/status
# HELP nginx_upstream_count_total Nginx total number of servers
# TYPE nginx_upstream_count_total gauge
nginx_upstream_count_total 6
# HELP nginx_upstream_count_up Nginx total number of servers that are UP
# TYPE nginx_upstream_count_up gauge
nginx_upstream_count_up 0
# HELP nginx_upstream_count_down Nginx total number of servers that are DOWN
# TYPE nginx_upstream_count_down gauge
nginx_upstream_count_down 6
# HELP nginx_upstream_count_generation Nginx generation
# TYPE nginx_upstream_count_generation gauge
nginx_upstream_count_generation 1
# HELP nginx_upstream_server_rise Nginx rise counter
# TYPE nginx_upstream_server_rise counter
nginx_upstream_server_rise { index = "0" , upstream_type = "http" , upstream = "http-cluster" , name = "127.0.0.1:8082" , status = "down" , type = "http" , port = "0" } 0
nginx_upstream_server_rise { index = "1" , upstream_type = "http" , upstream = "http-cluster" , name = "127.0.0.2:8082" , status = "down" , type = "http" , port = "0" } 0
nginx_upstream_server_rise { index = "1" , upstream_type = "stream" , upstream = "tcp-cluster" , name = "192.168.0.2:22" , status = "down" , type = "tcp" , port = "0" } 0
nginx_upstream_server_rise { index = "2" , upstream_type = "stream" , upstream = "udp-cluster" , name = "127.0.0.1:5432" , status = "down" , type = "udp" , port = "0" } 0
nginx_upstream_server_rise { index = "4" , upstream_type = "stream" , upstream = "http-cluster2" , name = "127.0.0.1:8082" , status = "down" , type = "http" , port = "0" } 0
nginx_upstream_server_rise { index = "5" , upstream_type = "stream" , upstream = "http-cluster2" , name = "127.0.0.2:8082" , status = "down" , type = "http" , port = "0" } 0
# HELP nginx_upstream_server_fall Nginx fall counter
# TYPE nginx_upstream_server_fall counter
nginx_upstream_server_fall { index = "0" , upstream_type = "http" , upstream = "http-cluster" , name = "127.0.0.1:8082" , status = "down" , type = "http" , port = "0" } 41
nginx_upstream_server_fall { index = "1" , upstream_type = "http" , upstream = "http-cluster" , name = "127.0.0.2:8082" , status = "down" , type = "http" , port = "0" } 42
nginx_upstream_server_fall { index = "1" , upstream_type = "stream" , upstream = "tcp-cluster" , name = "192.168.0.2:22" , status = "down" , type = "tcp" , port = "0" } 14
nginx_upstream_server_fall { index = "2" , upstream_type = "stream" , upstream = "udp-cluster" , name = "127.0.0.1:5432" , status = "down" , type = "udp" , port = "0" } 40
nginx_upstream_server_fall { index = "4" , upstream_type = "stream" , upstream = "http-cluster2" , name = "127.0.0.1:8082" , status = "down" , type = "http" , port = "0" } 40
nginx_upstream_server_fall { index = "5" , upstream_type = "stream" , upstream = "http-cluster2" , name = "127.0.0.2:8082" , status = "down" , type = "http" , port = "0" } 43
# HELP nginx_upstream_server_active Nginx active 1 for UP / 0 for DOWN
# TYPE nginx_upstream_server_active gauge
nginx_upstream_server_active { index = "0" , upstream_type = "http" , upstream = "http-cluster" , name = "127.0.0.1:8082" , type = "http" , port = "0" } 0
nginx_upstream_server_active { index = "1" , upstream_type = "http" , upstream = "http-cluster" , name = "127.0.0.2:8082" , type = "http" , port = "0" } 0
nginx_upstream_server_active { index = "1" , upstream_type = "stream" , upstream = "tcp-cluster" , name = "192.168.0.2:22" , type = "tcp" , port = "0" } 0
nginx_upstream_server_active { index = "2" , upstream_type = "stream" , upstream = "udp-cluster" , name = "127.0.0.1:5432" , type = "udp" , port = "0" } 0
nginx_upstream_server_active { index = "4" , upstream_type = "stream" , upstream = "http-cluster2" , name = "127.0.0.1:8082" , type = "http" , port = "0" } 0
nginx_upstream_server_active { index = "5" , upstream_type = "stream" , upstream = "http-cluster2" , name = "127.0.0.2:8082" , type = "http" , port = "0" } 0
root @ changxun - PC : ~ / nginx - dev / ngx_healthcheck_module #
返回目录
Syntax
检查间隔=毫秒 [fall=count] [rise=count] [超时=毫秒] [default_down=true|false] [type=tcp|udp|http] [port=check_port]
Default
:间隔=30000 下降=5 上升=2 超时=1000 default_down=true 类型=tcp
Context
:http/upstream ||河流/上游
该命令可以开启后端服务器健康检查功能。
Detail
interval
:向后端发送健康检查包的时间间隔。fall
( fall_count
):如果连续失败次数达到fall_count,则认为服务器宕机。rise
( rise_count
):如果连续成功次数达到rise_count,则认为服务器已上线。timeout
:后端健康请求超时。default_down
:设置服务器的初始状态,如果为true,则表示默认为down,如果为false,则为up。默认值为true,即一开始服务器不可用,要等待健康检查包达到一定次数后才认为健康。type
:健康检查包类型,目前支持以下类型tcp
:简单的tcp连接,如果连接成功,则说明后端正常。udp
:简单发送udp数据包,如果收到icmp错误(主机或端口不可达),则显示后端异常。(流配置块中仅支持UDP类型检查)http
:发送HTTP请求,通过后端回复包的状态来判断后端是否存活。示例如下:
stream {
upstream tcp-cluster {
# simple round-robin
server 127.0.0.1:22;
server 192.168.0.2:22;
check interval=3000 rise=2 fall=5 timeout=5000 default_down=true type=tcp;
}
server {
listen 522 ;
proxy_pass tcp-cluster;
}
...
}
Syntax
:healthcheck_status [html|csv|json|prometheus]
Default
:healthcheck_status html
Context
:http/服务器/位置
示例如下:
http {
server {
listen 80 ;
# status interface
location /status {
healthcheck_status;
}
...
}
您可以指定默认显示格式。格式可以是html
、 csv
或json
。默认类型是html
。它还支持通过请求参数指定格式。假设你的check_status
位置是'/status', format
参数可以改变显示页面的格式。你可以这样做:
/状态?格式=html
/状态?格式=csv
/状态?格式=json
/状态?格式=普罗米修斯
目前,您可以通过status
参数获取具有相同状态的服务器列表。例如:
/status?format=json&status=down
/status?format=html&status=down
/status?format=csv&status=up
/status?format=prometheus&status=up
返回目录
返回目录
请报告错误
或通过以下方式提交补丁
返回目录
Chance Chou (周长勋) [email protected]。
返回目录
健康检查部分基于姚维斌的健康检查模块nginx_upstream_check_module(http://github.com/yaoweibin/nginx_upstream_check_module);
该模块根据 BSD 许可证获得许可。
版权所有(C)2017-, by 周昌勋 [email protected]
版权所有(C) 2014 姚伟斌 [email protected]
版权所有。
如果满足以下条件,则允许以源代码和二进制形式重新分发和使用,无论是否经过修改:
源代码的重新分发必须保留上述版权声明、此条件列表和以下免责声明。
以二进制形式重新分发必须在随分发提供的文档和/或其他材料中复制上述版权声明、此条件列表以及以下免责声明。
本软件由版权所有者和贡献者“按原样”提供,不承担任何明示或默示的保证,包括但不限于适销性和特定用途适用性的默示保证。在任何情况下,版权持有者或贡献者均不对任何直接、间接、附带、特殊、惩戒性或后果性损害(包括但不限于采购替代商品或服务;使用、数据或利润损失;或其他损失)承担责任。或商业中断),无论是何种原因造成的,并且基于任何责任理论,无论是合同责任、严格责任还是侵权行为(包括疏忽或其他),均因使用本软件而产生,即使已被告知可能发生此类损害。
返回目录
返回目录