ngx_http_upstream_check_module

  • 该模块可以为 Tengine 提供主动式后端服务器健康检查的功能
  • 会不间断请求 upstream 代理下各服务指定接口,根据返回状态吗判定后端服务存活状态,控制 Nginx 反向代理的请求转发

使用

开启监控状态展示页面

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
server{
        listen  ;
        server_name     ;

        proxy_set_header Host   $$host:$server_port;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Connection "";
        proxy_connect_timeout 35s;
        proxy_read_timeout 35s;
        proxy_send_timeout 35s;
        proxy_intercept_errors on;
        proxy_http_version 1.1;
        proxy_next_upstream  off;
        error_page 500 502 504 503 /50x.html;
        error_page 403 /403.html;
        error_page 404 /404.html;

        access_log      logs/monitor_access.log accesslog;
        error_log       logs/monitor_error.log;

#######################################################################
        # 仅允许get和post方法
        if ($request_method !~* GET|POST|HEAD) {
            return 403;
        }

        location = /50x.html {
            root   html;
        }

        location = /403.html {
            root   html;
        }
        location = /404.html {
            root   html;
        }


        location ~* /upstream_status {
                check_status;
                allow ;
                deny all;
        }
}

upsteam监控配置

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
upstream wechat{
        server  10.0.3.7:4201 max_fails=0 weight=10;
        server  10.0.3.7:4202 max_fails=0 weight=10;

        #健康检查
        check interval=1000 rise=2 fall=5 timeout=2000 type=http default_down=false;
        check_keepalive_requests 3;
        check_http_send "GET /systemMonitor/monitor HTTP/1.1\r\nConnection: keep-alive\r\nHost:localhost.com \r\n\r\n";
        check_http_expect_alive http_2xx http_3xx;
}

参数说明

  • 默认:interval=30000 fall=5 rise=2 timeout=1000 default_down=true type=tcp
  • interval:向后端发送健康检查包的间隔
  • fall(fall_count):如果连续失败次数达到fall_count,服务器就呗认为是down
  • rise(rise_count):如果连续成功次数达到rise_count,服务器就被认为是up
  • timeout:后端健康请求的超时时间
  • default_down:设定初始时服务器的状态,如果是true,就说明是down的;如果是false,就是up的
    • 默认值是true,也就是一开始服务器认为是不可用,要等健康检查包达到一定成功次数以后,才会被认为是健康的
  • type:健康检查包的类型
    • tcp:简单的tcp链接,如果连接成功,就说明后端正常
    • ssl_hello:发送一个初始的SSL hello包,并接受服务器的SSL hello包
    • http:发送HTTP请求,通过后端的回复包的状态来判断后端是否存活
    • mysql:向mysql服务器连接,通过接受服务器的greeting包来判断后端是否存活
    • ajp:向后端发送AJP协议的Cping包,通过接收Cpong包来判断后端是否存活。
  • port:指定后端服务器的检查端口。你可以指定不同于真实服务的后端服务器的端口,比如后端提供的是443端口的应用,你可以去检查80端口的状态来判断后端健康状况。默认是0,表示跟后端server提供真实服务的端口一样。该选项出现于Tengine-1.4.0。
  • check_keepalive_requests request_num :该指令可以配置一个连接发送的请求数,其默认值为1,表示Tengine完成1次请求后即关闭连接。
    • Default: 1
    • Context: upstream
  • check_http_expect_alive [ http_2xx | http_3xx | http_4xx | http_5xx ]:该指令指定HTTP回复的成功状态,默认认为2XX和3XX的状态是健康的。
    • Default: http_2xx | http_3xx
    • Context: upstream
  • check_http_send http_packet:该指令可以配置http健康检查包发送的请求内容。为了减少传输数据量,推荐采用"HEAD"方法。当采用长连接进行健康检查时,需在该指令中添加keep-alive请求头,如:“HEAD / HTTP/1.1\r\nConnection: keep-alive\r\n\r\n”。同时,在采用"GET"方法的情况下,请求uri的size不宜过大,确保可以在1个interval内传输完成,否则会被健康检查模块视为后端服务器或网络异常。
    • Default: “GET / HTTP/1.0\r\n\r\n”
    • Context: upstream
  • check_shm_size size:所有的后端服务器健康检查状态都存于共享内存中,该指令可以设置共享内存的大小。默认是1M,如果你有1千台以上的服务器并在配置的时候出现了错误,就可能需要扩大该内存的大小。
    • Default: 1M
    • Context: http
  • check_status [html|csv|json]:显示服务器的健康状态页面。该指令需要在http块中配置
    • Default: check_status html
    • Context: location

参考