主动地健康检查,nignx定时主动地去ping后端的服务列表,当发现某服务出现异常时,把该服务从健康列表中移除,当发现某服务恢复时,又能够将该服务加回健康列表中。

使用第三访模块nginx_checkcheck模块下载

1、若健康检查包类型为http,在开启健康检查功能后,nginx会根据设置的间隔向指定的后端服务器端口发送健康检查包,并根据期望的HTTP回复状态码来判断服务是否健康。
2、后端真实节点不可用,则请求不会转发到故障节点
3、故障节点恢复后,请求正常转发

其他依赖,可以参考本地yum源

yum -y install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel

加载nginx_upstream_check_module模块

进去下载nginx解压目录下运行如下打包命令,如果没有patch就yum install patch -y

注:不管是1.20.············都加载check_1.20.1+.patch,只看主版本号

nginx_upstream_check_module下载安装 nginx check_nginx

编译安装nginx,注意各自模块的存放位置

./configure --user=root --group=root --with-http_ssl_module --with-threads --with-file-aio --with-http_stub_status_module --add-module=/opt/check_nginx/nginx_upstream_check_module-master

nginx_upstream_check_module下载安装 nginx check_tomcat_02

make && make install

nginx_upstream_check_module下载安装 nginx check_tomcat_03

nginx配置

我nginx的配置一般是单独

include conf.d/*.conf;

nginx_upstream_check_module下载安装 nginx check_运维_04

vim /usr/local/nginx/conf/nginx.conf新增/usr/local/nginx/conf/conf.d

nginx_upstream_check_module下载安装 nginx check_nginx_05

1、配置upstream.conf

项目部署目录/opt/apache-tomcat-8.5.81/webapps/ROOT/WEB-INF/下

探针文件放在/opt/apache-tomcat-8.5.81/webapps/ROOT/monitor/index.html

nginx_upstream_check_module下载安装 nginx check_nginx_06

当检测不到index.html存在会自动切换到另外一台服务器。

nginx_upstream_check_module下载安装 nginx check_运维_07

upstream tomcat
{
        ip_hash;
        server 127.0.0.1:8080;
        server 127.0.0.1:8081;
        check interval=3000 rise=2 fall=5 timeout=1000 type=http;
        check_http_send "GET /monitor/index.html HTTP/1.0";
        check_http_expect_alive http_2xx http_3xx;
}

interval检测间隔时间,单位为毫秒,rsie请求2次正常的话,标记此realserver的状态为up,fall表示请求5次都失败的情况下,标记此realserver的状态为down,timeout为超时时间,单位为毫秒。

server段里面可以加入查看realserver状态的页面

location / {       
	proxy_pass http://tomcat;       
	proxy_set_header Host $http_host;       
	proxy_set_header X-Real-IP $remote_addr;       
	proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;    
}

location  /status {     
	check_status;     
	access_log off;     
	charset utf-8; 
	}

启动nginx

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

nginx_upstream_check_module下载安装 nginx check_服务器_08

server number是后端服务器的数量
generation是Nginx reload的次数
Index是服务器的索引
Upstream是在配置中upstream的名称
Name是服务器IP
Status是服务器的状态
Rise是服务器连续检查成功的次数
Fall是连续检查失败的次数
Check type是检查的方式
Check port是后端专门为健康检查设置的端口