nginx自带的针对后端节点健康检查的功能比较简单,通过默认自带的ngx_http_proxy_module 模块和ngx_http_upstream_module模块中的参数来完成,当后端节点出现故障时,自动切换到健康节点来提供访问。但是nginx不能事先知道后端节点状态是否健康,后端即使有不健康节点,负载均衡器依然会先把请求转发给该不健康节点,然后再转发给别的节点,这样就会浪费一次转发,而且自带模块无法做到预警。所以我们可以使用第三方模块 nginx_upstream_check_module模块
nginx_upstream_check_module模块由淘宝团队开发 淘宝自己的 tengine 上是自带了该模块的。我们使用原生Nginx,采用添加模块的方式
此处使用的是nginx1.20.1
安装补丁:
注意 check版本和Nginx版本要求有限制 1.12以上版本的nginx,补丁为check_1.11.5+.patch 具体参考github
[root@nginx-server ~]# yum install -y unzip
下载模块
[root@nginx-server ~]# wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/refs/heads/master.zip
或
rz 上传包master.zip
[root@nginx-server ~]# unzip -d /usr/local/ master.zip
[root@bogon ~]# ls /usr/local/
bin games lib libexec nginx-1.20.2 sbin src
etc include lib64 nginx nginx_upstream_check_module-master share
-p0,是“当前路径” -p1,是“上一级路径”
[root@bogon ~]# cd /usr/local/
[root@bogon local]# mv nginx_upstream_check_module-master/ nginx_upstream_check_module 改名字
[root@nginx-server ~]# cd /usr/local/nginx-1.20.1/ #进入nginx的解压目录中
[root@nginx-server nginx-1.20.1]# yum install -y patch
[root@nginx-server nginx-1.20.1]# patch -p1 < ../nginx_upstream_check_module/check_1.20.1+.patch
[root@nginx-server nginx-1.20.1]# ./configure --prefix=/usr/local/nginx --group=nginx --user=nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/erro