Travis CI 构建状态 : 点击查看
(English language see here)
该模块可以为Nginx提供主动式后端服务器健康检查的功能(同时支持四层和七层后端服务器的健康检测)。
这个项目还在开发中完善中,欢迎贡献代码,或报告bug。一起使它变得更好。
有意愿一起开发完善的同学或者有疑问的可以联系我:
QQ
:373882405mail
: changxunzhou@qq.com
当你使用nginx作为负载均衡器时,nginx原生只提供了基本的重试方式来保证访问到正常的后端服务器。
相比之下,这个nginx第三方模块可以对后端服务器提供主动式的健康状态检测。
它维护了一个后端服务器列表,保证新的请求直接发送到一个健康的后端服务器。
主要特性:
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.0\r\n\r\n";
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#
语法
check interval=milliseconds [fall=count] [rise=count] [timeout=milliseconds] [default_down=true|false] [type=tcp|udp|http] [port=check_port]
默认值
: interval=30000 fall=5 rise=2 timeout=1000 default_down=true type=tcp
上下文
: http/upstream || stream/upstream
通过在http或stream下的upstream配置块中添加check
指令来开启对该upstream中的后端服务器的健康检查。
详细参数
该指令用法样例:
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;
}
...
}
语法
: healthcheck_status [html|csv|json]
默认值
: healthcheck_status html
上下文
: http/server/location
详细参数
该指令用法样例:
http {
server {
listen 80;
# status interface
location /status {
healthcheck_status;
}
...
}
通过配置该指令,你可以查看当前已开启健康检查的upstream的检查状态。
【关于输出格式】你可以通过URL请求参数format
指定查询接口的输出格式:
/status?format=html
/status?format=csv
/status?format=json
【关于数据过滤】 你可以通过URL请求参数status
来过滤显示down和up的后端服务器:
/status?format=json&status=down
/status?format=html&status=down
/status?format=csv&status=up
报告错误
提交你的修复补丁
Chance Chou (周长勋) changxunzhou@qq.com.
部分代码参考Yaoweibin的nginx_upstream_check_module模块: (http://github.com/yaoweibin/nginx_upstream_check_module);
This module is licensed under the BSD license.
Copyright (C) 2017-, by Changxun Zhou changxunzhou@qq.com
Copyright (C) 2014 by Weibin Yao yaoweibin@gmail.com
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。