nginx健康检查
young / / / 阅读量

简介

本文主要介绍nginx后端节点的健康检查,分为自带模块及第三方模块

nginx原生模块

nginx自带的针对后端节点健康检查的功能比较简单,通过默认自带的ngx_http_proxy_module 模块和ngx_http_upstream_module模块中的相关指令来完成当后端节点出现故障时,自动切换到健康节点来提供访问。

方法一

添加upstream的时候,直接ip+port后接weight=1 max_fails=2 fail_timeout=30s;

upstream fastdfs_tracker {
    server 192.168.1.1:8080 weight=1 max_fails=2 fail_timeout=30s;
    server 192.168.1.2:8080 weight=1 max_fails=2 fail_timeout=30s;
}

weight为配置的权重,在30s内检查2次数,失败则剔除均衡。

方法二

添加upstream的时候,在最后一行添加

upstream test{ 
    server 192.168.1.1:8080;
    server 192.168.1.2:8080;
    server 192.168.1.3:8080;
    check interval=3000 rise=2 fall=3 timeout=3000 type=http port=7070;
}

参数:

interval=3000:间隔3秒检查一次
rise=2:检查2次ok后端节点up
fall=3:三次检查失败后端节点down
timeout=3000:超时时间3秒
type=http:发http检查请求类型
port=8080检查端口,可省略,默认和server 192.168.1.1:8080中的端口一致。

第三方模块

借助淘宝技术团队开发的nginx模快nginx_upstream_check_module来检测后方realserver的健康状态,如果后端服务器不可用,则会将其踢出upstream,所有的请求不转发到这台服务器。当期恢复正常时,将其加入upstream。

在淘宝自己的tengine上是自带了该模块的,大家可以访问淘宝Tengine官网来获取该版本的nginx,也可以到Gitbub
如果没有使用淘宝的tengine的话,可以通过补丁的方式来添加该模块到我们自己的nginx
中。

安装

首先,停止nginx。进入nginx安装包,如我的路径是/web/soft/nginx-1.16.0/

为了统一管理第三方模块,新建了一个modules目录,并进入modules目录

mkdir modules
cd modules

下载nginx_upstream_check_module模块

wget https://codeload.github.com/yaoweibin/nginx_upstream_check_module/zip/master;
unzip master;
patch -p1 < /web/soft/nginx-1.16.0/modules/nginx_upstream_check_module-master/check_1.14.0+.patch

查看nginx原本的配置参数

[root@ycserver ~]#/web/soft/nginx-resource-80/sbin/nginx -V
nginx version: Amt/6.6.6
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) 
built with OpenSSL 1.1.1  11 Sep 2018
TLS SNI support enabled
configure arguments: --prefix=/web/soft/nginx-resource-80 --with-openssl=/usr/local/ssl --with-http_stub_status_module --with-http_ssl_module --with-pcre --with-stream --with-http_addition_module --add-module=/web/data/ngx-fancyindex-master

重新编译,要使用上面输出的配置参数

[root@ycserver ~]# ./configure --prefix=/web/soft/nginx-resource-80 --with-openssl=/usr/local/ssl --with-http_stub_status_module --with-http_ssl_module --with-pcre --with-stream --with-http_addition_module --add-module=/web/data/ngx-fancyindex-master --add-module=/web/soft/nginx-1.16.0/modules/nginx_upstream_check_module-master
[root@ycserver ~]# make

最后的add-module就是加载了nginx_upstream_check_module模块。

然后使用make编译。注意,不要make install

将生成的nginx替换原来的nginx

cp /web/soft/nginx-resource-80/sbin/nginx /web/soft/nginx-resource-80/sbin/nginx.bak;
cp ./objs/nginx /web/soft/nginx-resource-80/sbin

开启nginx服务

/web/soft/nginx-resource-80/sbin/nginx 

ok,至此,nginx_upstream_check_module模块安装完毕。

配置

upstream backend {
    #ip_hash;
    server 127.0.0.1:81 max_fails=2 fail_timeout=10s weight=1;
    server 127.0.0.1:82 max_fails=2 fail_timeout=10s weight=2;

    check interval=5000 rise=2 fall=3 timeout=3000 type=http;
    check_http_send "HEAD / HTTP/1.0\r\n\r\n";
    check_http_expect_alive http_2xx http_3xx;       
}

上述心跳检测的规则是:每5s检测一次,失败3次则标识后端服务为不存活,成功两次标识为存活,超时时间为3s,检测方式为http。心跳检测的方式有http和tcp两种方式。如果只是tcp方式,则下面的check_http_send和check_http_expect_alive不需要。

还可以配置check_status,通过网页查看后端服务器状态

location /backend_status {
     check_status;
     access_log off;
}

可以通过访问http://服务器ip/backend_status查看后端服务状态

总结

以上我们同时使用了nginx原生的及淘宝的健康检查模块,但是淘宝的间隔时是毫秒级,而且可以自定义监控url,定制监控页,响应速度快,比原生的敏感度要高。

支付宝捐赠
请使用支付宝扫一扫进行捐赠
微信捐赠
请使用微信扫一扫进行赞赏
有 0 篇文章