微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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都正常的情况下

2.一台realserver故障的情况下

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐