阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Nginx健康检查模块

158次阅读
没有评论

共计 3272 个字符,预计需要花费 9 分钟才能阅读完成。

在本小节我们介绍一个用于 Nginx 对后端 UpStream 集群节点健康状态检查的第三方模块:nginx_upstream_check_module(https://github.com/yaoweibin/nginx_upstream_check_module)。这个模块有资料介绍是 TaoBao 团队开发的,但是我在 GitHua 上试图求证时并没有找到直接证据。

这里需要说明的是,目前有很多 Nginx 模块实现 Nginx 对后端集群节点的健康监测,不止 nginx_upstream_check_module。Nginx 官方有一个模块 healthcheck_nginx_upstreams 也可以实现对后端节点的健康监测(https://github.com/cep21/healthcheck_nginx_upstreams 有详细的安装和使用介绍)

我们回到对 nginx_upstream_check_module 的讲解,要使用这个第三方模块首先您需要进行下载,然后通过 patch 命令将补丁打入您原有的 Nginx 源码中,并且重新进行编译安装。下面我们来重点讲解一下这个模块的安装和使用。

下载 nginx_upstream_check_module 模块:
wget https://codeload.github.com/yaoweibin/nginx_upstream_check_module/zip/master

您也可以直接到 GitHua 上进行下载,还一个在 linux 系统上使用 git 命令进行下载。

解压安装,并补丁打入 Nginx 源码
# unzip ./nginx_upstream_check_module-master.zip

注意是将补丁打入 Nginx 源码,不是 Nginx 的安装路径:

# cd ./nginx-1.6.2

# patch -p1 < ../nginx_upstream_check_module-master/check_1.5.12+.patch

如果补丁安装成功,您将看到以下的提示信息:
patching file src/http/modules/ngx_http_upstream_ip_hash_module.c
patching file src/http/modules/ngx_http_upstream_least_conn_module.c
patching file src/http/ngx_http_upstream_round_robin.c
patching file src/http/ngx_http_upstream_round_robin.h

这里请注意:在 nginx_upstream_check_module 官网的安装说明中,有一个打补丁的注意事项:
If you use nginx-1.2.1 or nginx-1.3.0, the nginx upstream round robin
module changed greatly. You should use the patch named
‘check_1.2.1.patch’.
If you use nginx-1.2.2+ or nginx-1.3.1+, It added the upstream
least_conn module. You should use the patch named ‘check_1.2.2+.patch’.
If you use nginx-1.2.6+ or nginx-1.3.9+, It adjusted the round robin
module. You should use the patch named ‘check_1.2.6+.patch’.
If you use nginx-1.5.12+, You should use the patch named
‘check_1.5.12+.patch’.
If you use nginx-1.7.2+, You should use the patch named
‘check_1.7.2+.patch’.

这里我们的 Nginx 的版本是 1.6.2,那么就应该打入 check_1.5.12+.patch 这个补丁 

重新编译安装 Nginx:
注意重新编译 Nginx,要使用 add-module 参数将这个第三方模块安装进去:

# ./configure –prefix=/usr/nginx-1.6.2/ –add-module=../nginx_upstream_check_module-master/

# make && make install

通过以上的步骤,第三方的 nginx_upstream_check_module 模块就在 Nginx 中准备好了。接下来我们讲解一下如何使用这个模块。首先看一下 upstream 的配置信息:
upstream cluster {
    # simple round-robin
    server 192.168.0.1:80;
    server 192.168.0.2:80;

    check interval=5000 rise=1 fall=3 timeout=4000;

    #check interval=3000 rise=2 fall=5 timeout=1000 type=ssl_hello;
    #check interval=3000 rise=2 fall=5 timeout=1000 type=http;
    #check_http_send “HEAD / HTTP/1.0\r\n\r\n”;
    #check_http_expect_alive http_2xx http_3xx;
}

上面的代码中,check 部分就是调用 nginx_upstream_check_module 模块的语法:
check interval=milliseconds [fall=count] [rise=count]
[timeout=milliseconds] [default_down=true|false]
[type=tcp|http|ssl_hello|mysql|ajp|fastcgi]

interval:必要参数,检查请求的间隔时间。

fall:当检查失败次数超过了 fall,这个服务节点就变成 down 状态。

rise:当检查成功的次数超过了 rise,这个服务节点又会变成 up 状态。

timeout:请求超时时间,超过等待时间后,这次检查就算失败。

default_down:后端服务器的初始状态。默认情况下,检查功能在 Nginx 启动的时候将会把所有后端节点的状态置为 down,检查成功后,在置为 up。

type:这是检查通信的协议类型,默认为 http。以上类型是检查功能所支持的所有协议类型。
check_http_send http_packet

http_packet 的默认格式为:”GET / HTTP/1.0\r\n\r\n”

check_http_send 设置,这个设置描述了检查模块在每次检查时,向后端节点发送什么样的信息
check_http_expect_alive [http_2xx | http_3xx | http_4xx | http_5xx] 

这些状态代码表示服务器的 HTTP 响应上是 OK 的,后端节点是可用的。默认情况的设置是:http_2xx | http_3xx

当您根据您的配置要求完成检查模块的配置后,请首先使用 nginx -t 命令监测配置文件是否可用,然后在用 nginx -s reload 重启 nginx。

1.4、不得不提的 tengine

Tengine 是由淘宝网发起的 Web 服务器项目。它在 Nginx 的基础上,针对大访问量网站的需求,添加了很多高级功能和特性。Tengine 的性能和稳定性已经在大型的网站如淘宝网,天猫商城等得到了很好的检验。它的最终目标是打造一个高效、稳定、安全、易用的 Web 平台(http://tengine.taobao.org/)。

您应该懂了,我建议您根据业务的实际情况,适时在生产环境引入 Tengine。但在本博客发布时,Tengine 的 2.X 版本还不稳定,所以建议实用 1.5.2 的稳定版本。请记住 Tengine 就是经过升读改造后的 Nginx。

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-21发表,共计3272字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中