nginx主动检测后端健康模块

一、前言

nginx也有自带的后端检测模块ngx_http_upstream_module,该模块可以做到基本的健康检查,因为该健康检查是被动的,当nginx有请求后,才会对后端服务进行健康检测,当检测到有故障时会将这个请求转发到正常的后端服务中,所以该模块不太适用,因此引入第三方的主动健康检测模块ngx_http_upstream_check_module,该模块会在时间间隔内对后端进行主动的健康检测,当发现不健康的后端服务时,会将其主动下线,不再接受请求

二、部署

创建nginx存放目录

bash 复制代码
mkdir /opt/nginx && cd /opt/nginx

下载安装包

bash 复制代码
wget http://nginx.org/download/nginx-1.20.2.tar.gz

解压安装包

bash 复制代码
tar -zxvf nginx-1.20.2.tar.gz

下载需要增加的模块

bash 复制代码
git clone https://github.com/yaoweibin/nginx_upstream_check_module.git

将该模块导入到nginx中

bash 复制代码
cd /opt/nginx/nginx-1.20.2
patch -p1 < /opt/nginx/nginx_upstream_check_module/check_1.20.1+.patch

安装编译的环境

bash 复制代码
yum install -y gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel

设置参数并编译安装

bash 复制代码
./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp  --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=/opt/nginx/nginx_upstream_check_module
make && make install

创建参数中设定的目录,有一个目录nginx中没有

bash 复制代码
mkdir -p /var/cache/nginx/client_temp

使用systemd管理nginx

vi /etc/systemd/system/nginx.service

bash 复制代码
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=network.target

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

启动nginx服务

bash 复制代码
systemctl start nginx && systemctl enable nginx

检查是否开启对应模块

bash 复制代码
nginx -V

配置使用nginx主动健康检测 模块

vi /etc/nginx/nginx.conf

bash 复制代码
upstream dev_web {
        server 10.1.60.125:30003;
        server 10.1.60.126:30003;
        server 10.1.60.127:30003;
        server 10.1.60.128:30003;
        check interval=3000 rise=1 fall=3 timeout=2000 type=http;  #配置http检测,该接口为前端web接口,所以配置http检测
        check_http_send "HEAD /login.html HTTP/1.0\r\n\r\n";
        #check_http_send "HEAD /login.html HTTP/1.1\r\nConnection: keep-alive\r\n\r\n";  #需要看http协议是多少,需要根据http协议填写,不同的协议使用的方法不一样
        #check_http_send "GET /login.html HTTP/1.1\r\nConnection: keep-alive\r\n\r\n";
        check_http_expect_alive http_2xx http_3xx;  #检测返回状态码为2xx、3xx代表正常
}

upstream dev_gateway {
        server 10.1.60.125:30004;
        server 10.1.60.126:30004;
        server 10.1.60.127:30004;
        server 10.1.60.128:30004;
        check interval=3000 rise=1 fall=3 timeout=2000 type=tcp;  #配置tcp/ip检测,接口为后端服务接口,所以配置tcp/ip检测
}
    
server {
        listen       80;

        location =/ {
            proxy_pass http://dev_web;
        }
        location =/login.html {
            proxy_pass http://dev_web;
        }
        location ~* \.(?:htm|html)$ {
            proxy_pass http://dev_web;
        }
        location ~* \.(?:ico|git|jpg|jpeg|png|bmp|swf|flv|mp4)$ {
            proxy_pass http://dev_web;
        }
        location ~* \.(?:css|js|eot|svg|ttf|woff2|otf|woff)$ {
            proxy_pass http://dev_web;
        }
        location / {
            proxy_set_header x-for $http_x_forwarded_for;
            proxy_set_header X-Forwaided-Proto $scheme;
            proxy_set_header Host              $host:$server_port;
            proxy_set_header X-Real-IP         $remote_addr;
            proxy_set_header X-Forwarded-For   $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Host  $host;
            proxy_buffering off;
            client_max_body_size 200m;
            client_body_buffer_size 50m;
            proxy_pass http://dev_gateway;
        }
        
        location /status {
            check_status;     #开启健康检查的web页面
        }
}

interval:表示间隔多少毫秒检测一次

rise:表示最少健康检测通过多少次代表正常

fall:表示最少健康检测不通过多少次代表不正常

type:健康检测使用的协议

可以使用curl命令查看http协议的版本

bash 复制代码
curl -I 10.1.60.125:30003/login.html  #可以使用-I代表支持HEAD方式,不支持的话可以使用get方式

重新加载nginx配置

bash 复制代码
nginx -s reload

查看健康检测的web

http://10.1.60.124/status

相关推荐
XIAOHEZIcode16 小时前
Linux系统鼠标偏移常见原因以及修复方案
linux·运维·游戏
用户0328472220701 天前
如何搭建本地yum源(上)
运维
ping某2 天前
为什么 Nginx 明明监听了 80,转发后端时却用了 4xxxx 端口?
后端·nginx
大树884 天前
金刚石散热越强,管路越先见顶
大数据·运维·服务器·人工智能·ai
摇滚侠4 天前
Linux CentOS7 rpm 安装 MySQL 5.7
linux·运维·mysql
霸道流氓气质4 天前
领域驱动设计(DDD)在 Spring Boot 微服务中的实践指南
运维·spring boot·微服务
Inhand陈工4 天前
基于台达PLC与映翰通IG502的智慧水产养殖精准投喂与远程运维解决方案
运维·人工智能·物联网·阿里云·信息与通信
酣大智4 天前
ARP代理--工作原理
运维·网络·arp·arp代理
shushangyun_4 天前
2026年快消品B2B系统推荐:支持终端门店订货、促销政策自动化的工具?
java·运维·网络·数据库·人工智能·spring·自动化
施努卡机器视觉4 天前
SNK施努卡侧滑门锁上滑轮总成自动化装配线,从零件到组件,全流程精密制造方案
运维·自动化·制造