Nginx后端Web服务器健康检查

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服务器健康检查_第1张图片

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

你可能感兴趣的:(运维,linux,nginx,nginx,负载均衡,服务器,centos)