Nginx后端Web服务器健康检查

43 篇文章 1 订阅
10 篇文章 0 订阅

Nginx后端Web服务器健康检查

  • 背景:
    有一个问题,当Nginx没有后端检测功能,当后端某一服务器无法提供服务时,该链接先被转发到这台机器,然后发现该机故障,而后才转发到其它机器,导致资源浪费。
    因此引入 nginx_upstream_check_module 模块,该模块用于提供负载均衡器内节点的健康检查,通过它可以用来检测后端服务的健康状态。如果后端服务不可用,则后面的请求就不会转发到该节点上。
  • 操作
# yum -y install patch
# cd /usr/local/src/nginx-1.12.2; patch -p1 < /usr/local/src/nginx_upstream_check_module/check_1.12.1+.patch
patching file src/http/modules/ngx_http_upstream_hash_module.c
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 源码目录,打补丁 ( 注意与自己的 Nginx 版本匹配 )

# ./configure --prefix=/usr/local/nginx-1.12.2 --add-module=/usr/local/src/nginx_upstream_check_module
# make && make install

重新编译、安装 Nginx,注意加上原来的编译参数。

  • 修改配置文件
# mkdir /usr/local/nginx-1.12.2/conf/passwd
# vim /usr/local/nginx-1.12.2/conf/nginx.conf
添加如下配置
    location /status {
        check_status;
        access_log   off;
        allow IP;                     ##填写本地机器的外网IP,做访问限制
        deny all;
        auth_basic "Restricted";
        auth_basic_user_file /usr/local/nginx-1.12.2/conf/passwd/test_passwd;
    }

auth_basic “Restricted”;
auth_basic_user_file /usr/local/nginx-1.12.2/conf/passwd/test_passwd;
上面两行为设置Nginx访问用户认证,可参Nginx访问用户认证进行配置,

  • 重启Nginx服务
/usr/local/nginx-1.12.2/sbin/nginx -t
/usr/local/nginx-1.12.2/sbin/nginx -s stop
/usr/local/nginx-1.12.2/sbin/nginx
/usr/local/nginx-1.12.2/sbin/nginx -s reload
  • 验证
    打开浏览器输入 IP/status
    在这里插入图片描述

好了,这就是Nginx后端Web服务器健康检查的方法了,如有问题可与博主一起交流讨论!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值