ngx_http_upstream_check_module 模块
模块开源地址:https://github.com/yaoweibin/nginx_upstream_check_module
官网介绍:http://tengine.taobao.org/document_cn/http_upstream_check_cn.html
Nginx 健康检测机制对比
nginx自带健康检查机制 | 第三方ngx_http_upstream_check_module模块 |
---|---|
被动健康检查 | 提供主动式后端服务器健康检查的功能 |
访问时才对后端进行探活且无法预警,影响效率 | 时间间隔内主动探活,后端不可用,请求不会转发到故障节点,故障节点恢复后请求正常转发 |
Nginx 编译安装
环境说明
- Centos7.9 最小化安装
- 未安装 Nginx
- 新建临时文件夹,用于存放相关安装包和源码包
mkdir -p /tmp/nginx
cd /tmp/nginx
# 下载第三方模块的最新包
wget https://codeload.github.com/yaoweibin/nginx_upstream_check_module/zip/master
- 解压第三方模块包
unzip master
# 进入解压后的目录,可以看到相关补丁包的版本,下载的nginx源码包不能大于这些补丁包,不然用不起模块
- 下载 nginx 源码包
# 下载1.16.1的源码包即可
wget http://nginx.org/download/nginx-1.16.1.tar.gz
# 解压源码包
tar -xf nginx-1.16.1.tar.gz
- 准备编译环境
# 有外网的条件下配置好国内的yum源;没有外网的条件下可以在有外网的系统里用yum下载相关rpm包拷贝到没网的进行处理
# 需要的依赖包有:gcc、pcre、pcre-devel、zlib、zlib-devel、openssl、openssl-devel、
yum install -y gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel
- 补丁包操作(重要操作)
# 进入解压后的 nginx 源码目录
cd nginx-1.16.1
# 补丁包操作
patch -p1 < /tmp/nginx/nginx_upstream_check_module-master/check_1.16.1+.patch
- 编译安装 nginx
# 根据自己所需加上编译的选项以及模块
./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=/tmp/nginx/nginx_upstream_check_module-master
# 编译安装:等待完成即可
make && make install
- 启动 nginx
# 编译后的 nginx配置文件
cd /etc/nginx/
# 启动文件位置/usr/sbin/nginx
cd /usr/sbin
./nginx
# 启动如遇报错信息:nginx: [emerg] mkdir() "/var/cache/nginx/client_temp" failed (2: No such file or directory)
# 创建该文件夹即可:这是我自己编译选项有这个导致的
- 访问验证
# 查看nginx的 开启模块
nginx -V
# 浏览器访问IP地址进行访问
Nginx 探活测试
- 准备三台后端服务器
# 后端三台服务器:修改一个index,标记一下
192.168.137.3
192.168.137.4
192.168.137.5
- 设置前置nginx代理到后端3台服务器
# 设置后访问前置nginx
# 可以看到目前代理到后端服务器1上面了
- 设置第三方模块的探活参数:相关参数和选项可参考模块官网说明
check interval=3000 rise=2 fall=5 timeout=1000 type=http;
check_http_send "HEAD / HTTP/1.0\r\n\r\n";
check_http_expect_alive http_2xx http_3xx;
# 增加location
location /status {
check_status;
access_log off;
}
- 重启前置 nginx
nginx -s reload
- 访问验证探活:IP地址/ststus
- 停掉一台后端服务器,观察探活结果
文章来源:https://www.toymoban.com/news/detail-770355.html
- 启动停掉的后端服务器,观察探活结果
文章来源地址https://www.toymoban.com/news/detail-770355.html
总结
- 编译安装在没有外网的环境下存在很多依赖性问题
- 第三方模块版本和nginx的版本存在一定的兼容性,需要提前确定
- 生产环境如果要更改前置nginx的安装模式重新安装,需要注意编译安装的选项和需要编译的模块,尽量做到一致
到了这里,关于Nginx 第三方健康检测模块的使用的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!