一、环境说明
系统版本:CentOS 7.9
内核版本:3.10.0-1160.119.1
Nginx版本:1.26.3
第三方检测模块及版本:nginx_upstream_check_module(v0.4.0,兼容nginx 1.20+)
二、nginx安装部署
2.1 下载检测模块
目前程序包的模块已经升级,升级后支持nginx 1.20以上的版本,源码包下载地址如下:
https://github.com/yaoweibin/nginx_upstream_check_module/archive/refs/tags/v0.4.0.tar.gz
2.2 安装相关依赖包
在执行部署之前,需要安装相关依赖包,如nginx所需的gcc、gcc-c++、openssl、zlib、pcre等,以及nginx_upstream_check_module执行补丁命令时所需的patch组件。
由于现在CentOS 7官方源已经无法使用,因此需要使用国内的其他yum源,或者使用CentOS 7.9镜像来配置本地的yum源,以安装相关依赖包。这里就不做过多说明,不熟悉的小伙伴,可以自行联网查询相关配置方法。
2.3 执行安装
2.3.1 上传相关包
将nginx以及nginx_upstream_check_module的源码包上传到Linux服务器的/usr/local/src目录下,并解压。
[root@vm-localsystem src]# ls
nginx-1.26.3.tar.gz nginx_upstream_check_module-0.4.0.tar.gz
[root@vm-localsystem src]# tar -xzf nginx-1.26.3.tar.gz
[root@vm-localsystem src]# tar -xzf nginx_upstream_check_module-0.4.0.tar.gz
[root@vm-localsystem src]# ls
nginx-1.26.3 nginx-1.26.3.tar.gz nginx_upstream_check_module-0.4.0 nginx_upstream_check_module-0.4.0.tar.gz
2.3.2 执行patch操作
[root@vm-localsystem src]# cd nginx-1.26.3/
[root@vm-localsystem nginx-1.26.3]# ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
[root@vm-localsystem nginx-1.26.3]# patch -p1 < ../nginx_upstream_check_module-0.4.0/check_1.20.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
2.3.3 创建用户和组
这里使用普通用户和组来安装和运行nginx,用户和组(如:app)需要提前创建。
[root@vm-localsystem ~]# groupadd app
[root@vm-localsystem ~]# useradd -d /data -g app app
[root@vm-localsystem ~]# chown -R app:app /data
2.3.4 编译和安装nginx
编译和安装的时候还是需要使用root用户的,但是需要在编译参数里面增加--group=app --user=app这样的参数,另外还需要增加引入nginx_upstream_check_module-0.4.0模块的参数--add-module=../nginx_upstream_check_module-0.4.0。
编译安装成功后,记得将nginx安装目录的用户和组设置为app:app。
[root@vm-localsystem ~]# cd /usr/local/src/nginx-1.26.3/
[root@vm-localsystem nginx-1.26.3]# ./configure --prefix=/data/nginx --with-threads --with-http_ssl_module --with-http_stub_status_module --add-module=../nginx_upstream_check_module-0.4.0 --group=app --user=app[root@vm-localsystem nginx-1.26.3]# make[root@vm-localsystem nginx-1.26.3]# make install[root@vm-localsystem nginx-1.26.3]# chown -R app:app /data/nginx/
2.3.5 nginx配置修改
这里需要注意的是,由于nginx使用使用普通用户运行,因此不能使用默认的80监听端口了,我这里修改为了8081端口。其他配置可根据需要自行修改。
2.3.6 启动nginx服务
nginx配置校验通过后,即可启动nginx服务,可使用-V检查nginx的编译参数。
[app@vm-localsystem conf]$ ../sbin/nginx -t
nginx: the configuration file /data/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /data/nginx/conf/nginx.conf test is successful
[app@vm-localsystem conf]$ ../sbin/nginx -V
nginx version: nginx/1.26.3
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/data/nginx --with-threads --with-http_ssl_module --with-http_stub_status_module --add-module=../nginx_upstream_check_module-0.4.0 --group=app --user=app
[app@vm-localsystem conf]$ ../sbin/nginx
[app@vm-localsystem conf]$ ps -ef| grep nginx| grep -v grep
app 4801 1 0 11:12 ? 00:00:00 nginx: master process ../sbin/nginx
app 4802 4801 2 11:12 ? 00:00:00 nginx: worker process
app 4803 4801 2 11:12 ? 00:00:00 nginx: worker process
三、主动检测功能测试
3.1 启动两个Tomcat实例
在一台Linux服务器上,启动2个Tomcat实例,部署的应用都为haweb(Oracle官方免费提供的一个测试小应用),为了做区分,对页面做了点改造,以示区别
tomcat1
tomcat2
在nginx中对这两个真实的页面访问地址进行反向代理和负载均衡配置,部分配置如下:
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;upstream haweb {server 192.168.223.96:18170; server 192.168.223.96:18171;#check interval=3000 rise=2 fall=5 timeout=1000 type=tcp;check interval=3000 rise=2 fall=5 timeout=1000 type=http; check_http_send "GET /haweb HTTP/1.0\r\n\r\n";check_http_expect_alive http_2xx http_3xx;}server {listen 8081;server_name 192.168.223.199;charset utf-8;#access_log logs/host.access.log main;location / {root html;index index.html index.htm;}location /haweb {proxy_pass http://haweb;}location /status {check_status;access_log off;allow 192.168.223.3;deny all;}
备注:
1)upstream{}配置,以及两个location{}配置,及/haweb /status;
2)upstream{}块配置中,启用主动健康检查,检查方式为http,使用http 1.0协议,发送/haweb的get请求。期望返回的正常的http代码为2XX、3XX开头的返回码。
配置校验没问题,然后热加载,即可通过nginx的IP和端口,来访问后端页面,默认访问策略为轮询,所以,刷新页面会在testPage1和testPage2之间来回切换。
[app@vm-localsystem conf]$ ../sbin/nginx -t
nginx: the configuration file /data/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /data/nginx/conf/nginx.conf test is successful
[app@vm-localsystem conf]$ ../sbin/nginx -s reload
[app@vm-localsystem conf]$
可以根据配置status模块,来查看探测的结果
把tomcat2的服务进程杀掉后,探测页面及展示出探测失败的告警
四、参考
https://www.cnblogs.com/linyouyi/p/11502282.html
https://www.cnblogs.com/LiuChang-blog/p/12494567.html
https://www.cnblogs.com/Xinenhui/p/15944725.html
https://www.cnblogs.com/dance-walter/p/12212607.html
https://github.com/yaoweibin/nginx_upstream_check_module