nginx check健康检查

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

大家都知道,前段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/archive/master.zip

https://github.com/yaoweibin/nginx_upstream_check_module

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

yum install –y openssl openssl-devel pcre-devel pcre

wget http://nginx.org/download/nginx-1.8.1.tar.gz

tar zxvf nginx-1.8.1.tar.gz

cd nginx-1.8.1

nginx_upstream_check_module ,nginx 我是放在同一目录下

在nginx目录先patch 一下

patch -p1 < ../nginx_upstream_check_module-master/check_1.7.5+.patch

配置和安装nginx

./configure --user=www --group=www --prefix=/usr/local/nginx  --with-http_sub_module --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module --with-http_spdy_module --with-http_gzip_static_module --add-module=../nginx_upstream_check_module-master

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状态的页面

server {

            listen 80;

 location / {

                proxy_pass http:// linuxyan;

            }

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

这个时候打开nstatus这个页面就可以看到当前realserver的状态了,
如下图:
1.2台realserver都正常的情况下

nginx check健康检查

2.一台realserver故障的情况下

nginx check健康检查

 

上一篇:linux nginx负载均衡


下一篇:Nginx负载均衡