根据参考Nginx负载均衡中后端节点服务器健康检查 - 运维笔记
和 Nginx 健康检查,使用开源模块 nginx_upstream_check_module。
其他参考:
nginx自动摘除和恢复后端服务,进行自动检测
安装module需要重新编译nginx!由于我之前是ubuntu系统,用apt安装的nginx1.14,所以实际上等于需要重新装下nginx,这次就换了新的版本。(没有apt remove)
参考:
centos
4. nginx-1.20*安装check模块
6. yum安装nginx添加upstream_check_module模块
github打不开,通过gitee下载,master即可。
https://gitee.com/mirrors/nginx_upstream_check_module
获得nginx_upstream_check_module-master.zip,解压。
unzip nginx_upstream_check_module-master.zip
ls nginx_upstream_check_module-master/
CHANGES check_1.14.0+.patch check_1.2.2+.patch check_1.7.5+.patch doc ngx_http_upstream_check_module.h upstream_fair.patch
check_1.11.1+.patch check_1.16.1+.patch check_1.2.6+.patch check_1.9.2+.patch nginx-sticky-module.patch ngx_http_upstream_jvm_route_module.patch util
check_1.11.5+.patch check_1.20.1+.patch check_1.5.12+.patch check.patch nginx-tests README
check_1.12.1+.patch check_1.2.1.patch check_1.7.2+.patch config ngx_http_upstream_check_module.c test
module和nginx版本要匹配,选择较新的1.20.*版本,nginx-1.20.2。解压,进入nginx路径。
https://nginx.org/en/download.html
tar -zxvf nginx-1.20.2.tar.gz
cd nginx-1.20.2
在nginx路径下执行!在nginx路径下!不然就会提示-p参数错误。
patch -p1 < ../nginx_upstream_check_module-master/check_1.20.1+.patch
接下来进入ubuntu不断踩坑的过程,主要问题就是依赖。centos的可以参考上面的链接。
首先,进入原来apt安装nginx的执行目录/usr/sbin/
,执行./nginx -V
,获取到configure arguments。添加--add-module=../nginx_upstream_check_module-master/
就是要执行的新的configure参数。
root@root:/usr/sbin# ./nginx -V
nginx version: nginx/1.14.0 (Ubuntu)
built with OpenSSL 1.1.1 11 Sep 2018
TLS SNI support enabled
configure arguments: --with-cc-opt='-g -O2 -fdebug-prefix-map=/build/nginx-YlUNvj/nginx-1.14.0=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -fPIC' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --modules-path=/usr/lib/nginx/modules --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_v2_module --with-http_dav_module --with-http_slice_module --with-threads --with-http_addition_module --with-http_geoip_module=dynamic --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_sub_module --with-http_xslt_module=dynamic --with-stream=dynamic --with-stream_ssl_module --with-mail=dynamic --with-mail_ssl_module
在执行时会遇到各种依赖问题,包括不限于以下,所以先安装依赖。
参考
C compiler cc is not found
apt-get install gcc
SSL modules require the OpenSSL library
apt-get install libssl-dev
the HTTP rewrite module requires the PCRE library.
apt-get install libpcre3 libpcre3-dev
the HTTP gzip module requires the zlib library.
apt install zlib1g
apt install zlib1g-dev
the GeoIP module requires the GeoIP library.
参考Nginx添加GeoIP并监控IP分布(接上一篇)
apt install libgeoip1 libgeoip-dev geoip-bin
The HTTP XSLT module requires the libxml2/libxslt libraries.
apt-get install libxslt1-dev
the HTTP image filter module requires the GD library.
apt-get install libgd-dev
The following packages have unmet dependencies:
libgd-dev : Depends: libxpm-dev but it is not going to be installed
Depends: libx11-dev but it is not going to be installed
Depends: libxt-dev but it is not going to be installed
E: Unable to correct problems, you have held broken packages.
apt-get install libxpm-dev libx11-dev libxt-dev libgd-dev
** libx11-dev : Depends: libx11-6 (= 2:1.6.4-3ubuntu0.4) but 2:1.6.4-3ubuntu0.5 is to be installed
Recommends: libx11-doc but it is not going to be installed
E: Unable to correct problems, you have held broken packages.**
参考LINUX安装依赖库冲突的最终版本
apt-get install libx11-doc
apt-get install libx11-6=2:1.6.4-3ubuntu0.4
apt-get install libx11-dev
执行configure
./configure --with-cc-opt='-g -O2 -fdebug-prefix-map=/build/nginx-YlUNvj/nginx-1.20.2=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -fPIC' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --modules-path=/usr/lib/nginx/modules --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_v2_module --with-http_dav_module --with-http_slice_module --with-threads --with-http_addition_module --with-http_geoip_module=dynamic --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_sub_module --with-http_xslt_module=dynamic --with-stream=dynamic --with-stream_ssl_module --with-mail=dynamic --with-mail_ssl_module --add-module=../nginx_upstream_check_module-master/
make
开始编译,无报错即可。make
cd objs/
./nginx -t
出现ok、successful就正确了。
3. 接下来make install
,安装已经编译好的程序。会复制文件树中到文件到指定的位置,可能会覆盖之前的文件,谨慎操作。
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
./nginx -t
可能遇到的各种错误:
nginx: [emerg] dlopen() "/usr/local/nginx/modules/ngx_http_geoip_module.so" failed (/usr/local/nginx/modules/ngx_http_geoip_module.so: cannot open shared object file: No such file or directory) in /etc/nginx/modules-enabled/50-mod-http-geoip.conf:1
nginx: [emerg] module "/usr/share/nginx/modules/ngx_http_geoip_module.so" version 1014000 instead of 1020002 in /etc/nginx/modules-enabled/50-mod-http-geoip.conf:1
nginx: configuration file /etc/nginx/nginx.conf test failed
参考很多centos下的,都说要卸载:
https://blog.csdn.net/enthan809882/article/details/118671648
https://www.cnblogs.com/mingetty/p/11125391.html
yum remove nginx-mod* ### 卸载旧模块
yum install nginx-module-* ### 安装新的
但是并不想卸载旧的,想寻找一个兼容apt旧版本的方法。
https://blog.csdn.net/weixin_46765649/article/details/128018659
https://blog.csdn.net/willingtolove/article/details/111462639
https://developer.aliyun.com/article/876253
https://blog.csdn.net/qq_36478642/article/details/119801984
参考https://bbs.csdn.net/topics/394359868可知version 1014000 instead of 1020002
代表,1.14.0版本和1.20.2版本,刚好是我apt安装nginx的版本和编译的版本,所以猜测是ngx_http_geoip_module.so
还是使用的apt版本的。
进入/usr/share/nginx/modules/
路径,ll -h
确定下确实ngx_http_geoip_module.so
是apt版本的。备份。
回到编译nginx的路径,执行make install
。再看/usr/share/nginx/modules/
路径下的文件已经被替换为新版本。
在nginx编译路径下的objs
路径启动nginx,会报错nginx: [emerg] getpwnam("nginx") failed
,因为获取用户失败,需要添加用户。
useradd -M -s /sbin/nologin nginx
再启动
./nginx -c /etc/nginx/nginx.conf
教程基本都是nginx做http反向代理。
修改conf配置文件。就将check正常的加到http upstream
块中,具体参数参见一中的链接。
upstream server_upstreams {
server 192.168.1.9:81;
server 192.168.1.15:81;
check interval=3000 rise=2 fall=5 timeout=1000 type=tcp;
}
完整文件/etc/nginx/nginx-http.conf
root@root:~/nginx-1.20.2/objs# cat /etc/nginx/nginx-http.conf
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
# multi_accept on;
}
# TCP反向代理
stream
{
log_format proxy '$remote_addr $remote_port - [$time_local] $status $protocol ' '"$upstream_addr" "$upstream_bytes_sent" "$upstream_connect_time"' ;
access_log logs/proxy.log proxy;
upstream server_upstreams_1 {
server 192.168.1.9:40;
server 192.168.1.15:40;
}
server {
listen 40;
proxy_pass server_upstreams_1;
}
}
# http反向代理
http
{
access_log logs/www-access.log;
error_log logs/www-error.log;
log_format proxy '$remote_addr $remote_port - [$time_local] $status ' '"$upstream_addr" "$upstream_bytes_sent" "$upstream_connect_time"' ;
access_log logs/proxy-http.log proxy;
upstream server_upstreams {
server 192.168.1.9:81;
server 192.168.1.15:81;
check interval=3000 rise=2 fall=5 timeout=1000 type=tcp;
}
server {
listen 81;
location / {
proxy_pass http://server_upstreams;
}
}
# 增加status方便观察
server {
listen 80;
location /status {
check_status;
access_log off;
charset utf-8;
}
}
}
重新加载nginx。
./nginx -s reload
访问http://192.168.2.20/status
(nginx的地址),可以观察当前的情况
因为访问的都是81根目录下的路径,选择的是tcp方式,服务布在192.168.1.X的docker中。
在两个upstream都是up的状态下,对192.168.1.15测试以下几种方式,不断刷新观察页面:
docker stop
停掉部署的docker容器。——可检测到,fall counts增加,到5status变down测试使用http方式,将上面的type改成http,启动后两个状态都是down。应该是因为我81下没有/页面。
参考Nginx的HTTP健康检测,添加check_http_send参数。
如果是nginx做tcp反向代理,查了一圈没找到应用。
最后发现github的issue中有https://github.com/yaoweibin/nginx_upstream_check_module/issues/193。这个模块确实只支持http中upstream的检测。
Tengine也是,并不支持tcp……https://github.com/alibaba/tengine/issues/1602
有人提供了stream检测的模块https://github.com/zhouchangxun/ngx_healthcheck_module/。
安装stream检测的模块,nginx用的1.20-stable
几个坑:
./auto/configure --with-stream=dynamic --with-http_geoip_module=dynamic --with-http_image_filter_module=dynamic --add-module=../ngx_healthcheck_module/ --with-http_xslt_module=dynamic --with-mail=dynamic --with-mail_ssl_module
/etc/nginx/module-enable
目录下的conf,其实不需要加载这么多的模块,会报类似module "ngx_pagespeed" is already loaded in /etc/nginx/modules.conf.d/pagespeed.conf:1 · Issue
的错误。我直接把指定要load的conf文件删掉了。