Nginx健康检查

目录

一、失败重试

二、健康检查

三、安装模块nginx_upstream_check_module

1. 下载模块

2. 解压模块

3. 查看Nginx版本

4. 安装补丁

5. 配置模块

6. 修改配置

7. 验证检查结果


一、失败重试

Nginx配置如下:


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    
	upstream backend2 {
		server 192.168.1.55:9022 max_fails=2 fail_timeout=10s weight=1;
	}

    server {
        listen       8200;
        server_name  localhost;

        #access_log  logs/host.access.log  main;

        location / {
			proxy_pass http://backend2;
			
			#连接超时
			proxy_connect_timeout 5s; 
			#读超时
			proxy_read_timeout 5s;
			#发送超时
			proxy_send_timeout 5s;
			
			#上游服务器超时
			proxy_next_upstream error timeout;
			proxy_next_upstream_timeout 10s;
			#上游服务器重试次数
			proxy_next_upstream_tries 2;
        }
		
		location /lua {
			default_type 'text/html';
			content_by_lua_block {
                ngx.say("<p>hello, world</p>")
            }
        }
		
		location /upstream_status {
			#开启upstream状态页面
			check_status;     
		}

}

二、健康检查

如上图所示,主动检查需要Nginx添加模块nginx_upstream_check_module(GitHub - yaoweibin/nginx_upstream_check_module: Health checks upstreams for nginx),见下章节介绍。

Nginx配置如下:

http {
    
	upstream backend2 {
		server 192.168.1.55:9022 weight=1;
		
		#http类型检查:interval调用间隔时间,成功rise次后存活,失败fall次后不存活
		check interval=3000 rise=1 fall=3 timeout=2000 type=http;
		#和上游服务器长连接数
		check_keepalive_requests 100;
		#检查调用接口
		check_http_send "HEAD /vms-match/v3/staticcache/basic/switch-info/900000211 HTTP/1.0\r\nConnection: keep-alive\r\n\r\n";
		#调用接口响应状态码为正常
		check_http_expect_alive http_2xx http_3xx;
	}
}

三、安装模块nginx_upstream_check_module

1. 下载模块

https://github.com/yaoweibin/nginx_upstream_check_module

2. 解压模块

3. 查看Nginx版本

目录下:/home/openresty/openresty-1.19.9.1/bundle/nginx-1.19.9 

使用补丁版本:check_1.20.1+.patch

4. 安装补丁

在/home/openresty/openresty-1.19.9.1/bundle/nginx-1.19.9下命令:

patch -p1 < /home/nginx_upstream_check_module-master/check_1.20.1+.patch 

5. 配置模块

./configure --with-luajit --add-module=/home/nginx_upstream_check_module-master
make & make install

6. 修改配置

        a. 检查接口:/status

        b. 接口返回:2XX和3XX为成功

        c. 检查上游服务器结果:check_status

7. 验证检查结果

  • 0
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值