在Nginx官方模块提供的模块中,没有对负载均衡后端节点的健康检查模块,但可以使用第三方模块。
nginx_upstream_check_module来检测后端服务的健康状态。
第三方模块项目地址:TP

lb01部署:

# 安装依赖
yum install -y gcc glibc gcc-c++ pcre-devel openssl-devel patch
# 停掉nginx
systemctl stop nginx
# 下载nginx源码包和第三方模块
wget https://nginx.org/download/nginx-1.22.1.tar.gz
wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/master.zip
#创建目录
mkdir /app
# 解压nginx和第三方模块
tar xf nginx-1.22.1.tar.gz
unzip master.zip
[root@lb01 ~]# ll
total 4
drwxr-xr-x 8 1001 1001  158 Oct 19  2022 nginx-1.22.1
drwxr-xr-x 6 root root 4096 Nov  6 12:37 nginx_upstream_check_module-master
#移动到nginx-1.22.目录里
cd nginx-1.22.
#打补丁   
patch -p1 < /root/nginx_upstream_check_module-master/check_1.20.1+.patch
#生成
./configure --prefix=/app/nginx-1.22.1 --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' --add-module=/root/nginx_upstream_check_module-master
#如果报以下错误,查看是否缺少依赖
./configure: error: SSL modules require the OpenSSL library.
You can either do not enable the modules, or install the OpenSSL library
into the system, or build the OpenSSL library statically from the source
with nginx by using --with-openssl=<path> option.   #//缺少一个名叫openssl的依赖
yum install -y openssl-devel
#编译安装
make && make install
    底行出现如下表示安装成功
    make[1]: Leaving directory `/root/nginx-1.22.1'
#编辑配置文件
vim /app/nginx-1.22.1/conf/nginx.conf
    http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;                            //此行下方添加以下内容即可
    include /app/nginx-1.22.1/conf/conf.d/*.conf;
#创建目录
cd /app/nginx-1.22.1/conf
mkdir conf.d
## 编写配置文件
vim /app/nginx-1.22.1/conf/conf.d/jiujiu.conf
-----------------------------------------------------------------------------------------------------
upstream lb.xxx.com {
    server 172.16.1.7:9999 weight=3;
    server 172.16.1.8:9999;
    server 172.16.1.9:9999;
    check interval=3000 rise=2 fall=3 timeout=1000 type=tcp;
#interval  检测间隔时间,单位为毫秒
#rise      表示请求2次正常,标记此后端的状态为up
#fall      表示请求3次失败,标记此后端的状态为down
#type      类型为tcp
#timeout   超时时间,单位为毫秒
}

server{
        listen 80;
        server_name blog.xxx.com;
    location /{
    proxy_pass http://lb.xxx.com;   
    }
    location /check_health {
        check_status;   
    }
}
-----------------------------------------------------------------------------------------------------
#语法检查
[root@lb01 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
# 启动nginx
/app/nginx-1.22.1/sbin/nginx
# 重启nginx
/app/nginx-1.22.1/sbin/nginx -s reload
#本地域名解析
10.0.0.5 blog.xxx.com
#浏览器访问
http://blog.xxx.com/check_health