您的位置:首页 > 运维架构 > Nginx

nginx利用第三方模块nginx_upstream_check_module来检查后端服务器的健康情况

2015-01-18 22:40 686 查看
大家都知道,前段nginx做反代,如果后端服务器宕掉的话,nginx是不能把这台realserver提出upstream的,所以还会有请求转发到后端的这台realserver上面去,虽然nginx可以在localtion中启用proxy_next_upstream来解决返回给用户的错误页面,方法在:http://www.linuxyan.com/web-server/67.html,大家可以参考一下,但这个还是会把请求转发给这台服务器的,然后再转发给别的服务器,这样就浪费了一次转发,这次借助与淘宝技术团队开发的nginx模快nginx_upstream_check_module来检测后方realserver的健康状态,如果后端服务器不可用,则所以的请求不转发到这台服务器。

首先去这里下载nginx的模块https://github.com/yaoweibin/nginx_upstream_check_module

下面是nginx打上模块补丁的安装

$ wget ‘http://nginx.org/download/nginx-1.0.14.tar.gz’
$ tar -xzvf nginx-1.0.14.tar.gz
$ cd nginx-1.0.14/
$ patch -p1 < /path/to/nginx_http_upstream_check_module/check.patch
注:因nginx版本更新,1.2以上版本的nginx,补丁为check_1.2.1+.patch
$ ./configure –add-module=/path/to/nginx_http_upstream_check_module
$ make
$ make install


之后在nginx.conf配置文件里面的upstream加入健康检查,如下:

upstream linuxyan {
server 192.168.0.21:80;
server 192.168.0.22:80;
check interval=3000 rise=2 fall=5 timeout=1000;
}


这里下面加的这句话我解释下,interval检测间隔时间,单位为毫秒,rsie请求2次正常的话,标记此realserver的状态为up,fall表示请求5次都失败的情况下,标记此realserver的状态为down,timeout为超时时间,单位为毫秒。

在server段里面可以加入查看realserver状态的页面

location /nstatus {
check_status;
access_log off;
#allow SOME.IP.ADD.RESS;
#deny all;
}


这个时候打开nstatus这个页面就可以看到当前realserver的状态了,

如下图:

1.2台realserver都正常的情况下



2.一台realserver故障的情况下



更多有关于此模块的信息请查阅:https://github.com/yaoweibin/nginx_upstream_check_module

本文固定链接: http://www.linuxyan.com/web-server/90.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐