原生Nginx健康检查机制初探

Nginx健康检查

[官方文档](HTTP Health Checks | NGINX Documentation)

先列一下我的nginx的配置

upstream nginx_test {
        server 172.25.78.46:8000   max_fails=2 fail_timeout=10s;
        server 172.25.78.46:8800   max_fails=2 fail_timeout=10s;
    }

location / {
                proxy_pass http://nginx_test;   
        } 

nginx版本

# nginx -V
nginx version: nginx/1.20.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) 
built with OpenSSL 1.1.1g FIPS  21 Apr 2020 (running with OpenSSL 1.1.1k  FIPS 25 Mar 2021)
TLS SNI support enabled
configure arguments: --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-compat --with-debug --with-file-aio --with-google_perftools_module --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_degradation_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_mp4_module --with-http_perl_module=dynamic --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-http_xslt_module=dynamic --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-stream_ssl_preread_module --with-threads --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' --with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E'

问题

问题1:Nginx是如何检测后端服务器,判断资源可用的?

首先,我能想到可能的方式有

  1. nginx不定时去探测后端的资源,根据探测的结果用某种标准判断后端的资源是否可用

  2. nginx直接将正常的情况分发,但后端的资源不可用(某种标准),将此转发给另外的节点,然后标记这个资源不可用(一段时间)

  3. 后端节点上报状态给nginx(由于后端的server没有配置跟nginx交互相关的信息,所以这种这种可能性为0)

看回nginx的文档,针对HTTP Health Checks, 文档描述的有被动检测和主动检测。 这里的主动被动是从Nginx的视角来说的。

根据文档描述,原生的nginx默认是被动的模式,也就是类似于上面的2. 主要与2个参数有关

  • fail_timeout – Sets the time during which a number of failed attempts must happen for the server to be marked unavailable, and also the time for which the server is marked unavailable (default is 10 seconds).
  • max_fails – Sets the number of failed attempts that must occur during the fail_timeout period for the server to be marked unavailable (default is 1 attempt).

理解起来就是说在fail_timout时间内出现max_failsfail, 这个server就不可用,并且在未来的fail_timout时间不再分发请求过来。

下图是我所理解的

(虚线表示访问不可达)

这里的测试如下:

每一秒发起一次请求,在某个时间点的时候,将8000这个服务停止,所得的日志如下:


[TIME]14/Jun/2023:16:38:06 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.003 [SERVERRSPTIME]0.003 [REQUEST]GET /test_view/timeout/?num=1 HTTP/1.1 [STATUS]200 [BODYBYTES]15 [REFERER]- 
[TIME]14/Jun/2023:16:38:07 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=2 HTTP/1.1 [STATUS]200 [BODYBYTES]15 [REFERER]- 
[TIME]14/Jun/2023:16:38:08 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8000 [REQTIME]0.004 [SERVERRSPTIME]0.004 [REQUEST]GET /test_view/timeout/?num=3 HTTP/1.1 [STATUS]200 [BODYBYTES]15 [REFERER]- 
[TIME]14/Jun/2023:16:38:09 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=4 HTTP/1.1 [STATUS]200 [BODYBYTES]15 [REFERER]- 
[TIME]14/Jun/2023:16:38:10 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8000 [REQTIME]0.002 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=5 HTTP/1.1 [STATUS]200 [BODYBYTES]15 [REFERER]- 
[TIME]14/Jun/2023:16:38:11 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8000 [REQTIME]0.002 [SERVERRSPTIME]0.001 [REQUEST]GET /test_view/timeout/?num=6 HTTP/1.1 [STATUS]200 [BODYBYTES]15 [REFERER]- 
[TIME]14/Jun/2023:16:38:12 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=7 HTTP/1.1 [STATUS]200 [BODYBYTES]15 [REFERER]- 
[TIME]14/Jun/2023:16:38:13 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.003 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=8 HTTP/1.1 [STATUS]200 [BODYBYTES]15 [REFERER]- 
[TIME]14/Jun/2023:16:38:14 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8000 [REQTIME]0.002 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=9 HTTP/1.1 [STATUS]200 [BODYBYTES]15 [REFERER]- 
[TIME]14/Jun/2023:16:38:15 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=10 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:38:16 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8000, 172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.000, 0.002 [REQUEST]GET /test_view/timeout/?num=11 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:38:17 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=12 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:38:18 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8000, 172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.001, 0.001 [REQUEST]GET /test_view/timeout/?num=13 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:38:19 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=14 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:38:20 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8000, 172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.000, 0.002 [REQUEST]GET /test_view/timeout/?num=15 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:38:21 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8000, 172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.001, 0.002 [REQUEST]GET /test_view/timeout/?num=16 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:38:22 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.001 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=17 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:38:23 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=18 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:38:24 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=19 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:38:25 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.001 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=20 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:38:26 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.003 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=21 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:38:27 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=22 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:38:28 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.003 [REQUEST]GET /test_view/timeout/?num=23 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:38:29 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=24 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:38:30 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=25 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:38:31 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=26 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:38:32 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=27 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:38:33 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8000, 172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.000, 0.002 [REQUEST]GET /test_view/timeout/?num=28 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:38:34 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=29 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:38:35 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=30 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:38:36 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8000, 172.25.78.46:8800 [REQTIME]0.003 [SERVERRSPTIME]0.000, 0.002 [REQUEST]GET /test_view/timeout/?num=31 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:38:37 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=32 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:38:38 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=33 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:38:39 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=34 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:38:40 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=35 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:38:41 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=36 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:38:42 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=37 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:38:43 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.003 [SERVERRSPTIME]0.003 [REQUEST]GET /test_view/timeout/?num=38 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:38:44 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=39 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:38:45 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=40 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:38:46 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=41 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:38:47 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8000, 172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.001, 0.002 [REQUEST]GET /test_view/timeout/?num=42 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:38:48 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=43 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:38:49 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=44 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:38:50 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8000, 172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.001, 0.001 [REQUEST]GET /test_view/timeout/?num=45 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:38:51 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=46 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:38:52 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=47 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:38:53 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=48 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:38:54 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=49 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:38:55 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=50 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:38:56 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.003 [SERVERRSPTIME]0.003 [REQUEST]GET /test_view/timeout/?num=51 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:38:57 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=52 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:38:58 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.003 [REQUEST]GET /test_view/timeout/?num=53 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:38:59 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=54 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:39:00 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=55 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:39:01 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=56 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:39:02 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=57 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:39:03 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8000, 172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.000, 0.002 [REQUEST]GET /test_view/timeout/?num=58 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:39:04 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.003 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=59 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:39:05 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=60 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:39:06 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8000, 172.25.78.46:8800 [REQTIME]0.003 [SERVERRSPTIME]0.000, 0.002 [REQUEST]GET /test_view/timeout/?num=61 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:39:07 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.001 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=62 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:39:08 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=63 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:39:09 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=64 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:39:10 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.003 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=65 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:39:11 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=66 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:39:12 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.003 [SERVERRSPTIME]0.004 [REQUEST]GET /test_view/timeout/?num=67 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:39:13 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.003 [REQUEST]GET /test_view/timeout/?num=68 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:39:14 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=69 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:39:15 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8000, 172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.000, 0.002 [REQUEST]GET /test_view/timeout/?num=70 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:39:16 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=71 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:39:17 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=72 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:39:18 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=73 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:39:19 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8000, 172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.001, 0.002 [REQUEST]GET /test_view/timeout/?num=74 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:39:20 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=75 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:39:21 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=76 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:39:22 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=77 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:39:23 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=78 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:39:24 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=79 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:39:25 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=80 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:39:26 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.003 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=81 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:39:27 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=82 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:39:28 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=83 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:39:29 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.003 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=84 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:39:30 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=85 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:39:31 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8000, 172.25.78.46:8800 [REQTIME]0.003 [SERVERRSPTIME]0.000, 0.002 [REQUEST]GET /test_view/timeout/?num=86 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:39:32 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8000, 172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.000, 0.002 [REQUEST]GET /test_view/timeout/?num=87 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:39:33 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=88 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:39:34 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=89 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:39:35 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.001 [REQUEST]GET /test_view/timeout/?num=90 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:39:36 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.001 [REQUEST]GET /test_view/timeout/?num=91 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:39:37 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=92 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:39:38 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.001 [REQUEST]GET /test_view/timeout/?num=93 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:39:39 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.003 [REQUEST]GET /test_view/timeout/?num=94 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:39:40 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=95 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:39:41 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=96 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:39:42 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=97 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:39:43 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.003 [SERVERRSPTIME]0.003 [REQUEST]GET /test_view/timeout/?num=98 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]- 
[TIME]14/Jun/2023:16:39:44 +0800 [CLIENTIP]- [PREIP]192.168.5.68 [SERVERNAME]_ [SERVERIP]172.25.78.46:8000, 172.25.78.46:8800 [REQTIME]0.003 [SERVERRSPTIME]0.000, 0.002 [REQUEST]GET /test_view/timeout/?num=99 HTTP/1.1 [STATUS]200 [BODYBYTES]16 [REFERER]-

[SERVERIP]172.25.78.46:8000, 172.25.78.46:8800的表示先发给了172.25.78.46:8000,由于后端RST了,转发给了172.25.78.46:8800
从日志上看,大体符合所理解的策略(并没有100%,特别是前面的几次),这点抓包也证实了(这里不列出抓包的文件了)。

问题2:后端什么样的检测结果,Nginx才会认为是fail的呢?

这个是由proxy_next_upstream 控制的,但在[文档](Module ngx_stream_proxy_module关于这个参数的描述,没有看到这个参数有哪些可选值,根据一些网上的文章,这个参数的可选值有

● error — an error has occurred while connecting to the server, sending a request to it, or reading its response
● timeout — occurred timeout during the connection with the server, transfer the requst or while reading response from the server;
● invalid_header — server returned a empty or incorrect answer;
● http_500 — server returned answer with code 500
● http_503 — server returned answer with code 503
● http_404 — server returned answer with code 404

默认proxy_next_upstream的值为error timeout

来测试看看。

先保持默认配置,那就是说500,404这些不会被当做不可用。来测试一下:

我把server8000配置为响应500,server8800正常响应200。

访问的结果如下:

[TIME]15/Jun/2023:09:52:20 +0800 [CLIENTIP]- [PREIP]172.25.48.53 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.004 [SERVERRSPTIME]0.004 [REQUEST]GET /test_view/timeout/?num=1 HTTP/1.1 [STATUS]200 [BODYBYTES]15 [REFERER]- 
[TIME]15/Jun/2023:09:52:23 +0800 [CLIENTIP]- [PREIP]172.25.48.53 [SERVERNAME]_ [SERVERIP]172.25.78.46:8000 [REQTIME]0.002 [SERVERRSPTIME]0.003 [REQUEST]GET /test_view/timeout/?num=1 HTTP/1.1 [STATUS]500 [BODYBYTES]3 [REFERER]- 
[TIME]15/Jun/2023:09:52:24 +0800 [CLIENTIP]- [PREIP]172.25.48.53 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=1 HTTP/1.1 [STATUS]200 [BODYBYTES]15 [REFERER]- 
[TIME]15/Jun/2023:09:52:24 +0800 [CLIENTIP]- [PREIP]172.25.48.53 [SERVERNAME]_ [SERVERIP]172.25.78.46:8000 [REQTIME]0.003 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=1 HTTP/1.1 [STATUS]500 [BODYBYTES]3 [REFE

也就是说,当后端server响应500的时候,nginx依旧认为server是health的。

来加上proxy_next_upstream http_404 http_500; 参数试试。

访问的结果如下:

[TIME]15/Jun/2023:10:49:11 +0800 [CLIENTIP]- [PREIP]172.25.48.53 [SERVERNAME]_ [SERVERIP]172.25.78.46:8000, 172.25.78.46:8800 [REQTIME]0.047 [SERVERRSPTIME]0.020, 0.027 [REQUEST]GET /test_view/timeout/?num=1 HTTP/1.1 [STATUS]200 [BODYBYTES]15 [REFERER]- 
[TIME]15/Jun/2023:10:49:11 +0800 [CLIENTIP]- [PREIP]172.25.48.53 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.003 [SERVERRSPTIME]0.004 [REQUEST]GET /test_view/timeout/?num=1 HTTP/1.1 [STATUS]200 [BODYBYTES]15 [REFERER]- 
[TIME]15/Jun/2023:10:49:12 +0800 [CLIENTIP]- [PREIP]172.25.48.53 [SERVERNAME]_ [SERVERIP]172.25.78.46:8000, 172.25.78.46:8800 [REQTIME]0.005 [SERVERRSPTIME]0.002, 0.002 [REQUEST]GET /test_view/timeout/?num=1 HTTP/1.1 [STATUS]200 [BODYBYTES]15 [REFERER]- 
[TIME]15/Jun/2023:10:49:12 +0800 [CLIENTIP]- [PREIP]172.25.48.53 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.003 [SERVERRSPTIME]0.003 [REQUEST]GET /test_view/timeout/?num=1 HTTP/1.1 [STATUS]200 [BODYBYTES]15 [REFERER]- 
[TIME]15/Jun/2023:10:49:13 +0800 [CLIENTIP]- [PREIP]172.25.48.53 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.003 [REQUEST]GET /test_view/timeout/?num=1 HTTP/1.1 [STATUS]200 [BODYBYTES]15 [REFERER]- 
[TIME]15/Jun/2023:10:49:14 +0800 [CLIENTIP]- [PREIP]172.25.48.53 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.003 [REQUEST]GET /test_view/timeout/?num=1 HTTP/1.1 [STATUS]200 [BODYBYTES]15 [REFERER]- 
[TIME]15/Jun/2023:10:49:15 +0800 [CLIENTIP]- [PREIP]172.25.48.53 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.003 [REQUEST]GET /test_view/timeout/?num=1 HTTP/1.1 [STATUS]200 [BODYBYTES]15 [REFERER]- 
[TIME]15/Jun/2023:10:49:22 +0800 [CLIENTIP]- [PREIP]172.25.48.53 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=1 HTTP/1.1 [STATUS]200 [BODYBYTES]15 [REFERER]- 
[TIME]15/Jun/2023:10:49:24 +0800 [CLIENTIP]- [PREIP]172.25.48.53 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.003 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=1 HTTP/1.1 [STATUS]200 [BODYBYTES]15 [REFERER]- 
[TIME]15/Jun/2023:10:49:30 +0800 [CLIENTIP]- [PREIP]172.25.48.53 [SERVERNAME]_ [SERVERIP]172.25.78.46:8000, 172.25.78.46:8800 [REQTIME]0.004 [SERVERRSPTIME]0.002, 0.003 [REQUEST]GET /test_view/timeout/?num=1 HTTP/1.1 [STATUS]200 [BODYBYTES]15 [REFERER]- 
[TIME]15/Jun/2023:10:49:31 +0800 [CLIENTIP]- [PREIP]172.25.48.53 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=1 HTTP/1.1 [STATUS]200 [BODYBYTES]15 [REFERER]- 
[TIME]15/Jun/2023:10:49:32 +0800 [CLIENTIP]- [PREIP]172.25.48.53 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=1 HTTP/1.1 [STATUS]200 [BODYBYTES]15 [REFERER]- 

可以看到,当转发给server8000,收到其500响应码时,将请求转发给了server8800。 证明proxy_next_upstream http_404 http_500;   是有效果的。

但实际生产中,这个值是保持默认值还是要自定义,这个得根据用户实际的业务场景来,一般情况下,还是保持为默认值。

那么默认值中的timeout又是如何定义的呢? 它与哪些有关呢?主要有以下几个参数:

  • proxy_connect_timeout #nginx跟后端服务器连接超时时间(代理连接超时,我理解为三次握手时间)

  • proxy_send_timeout # 代理发送超时时间

  • proxy_read_timeout # 连接成功后,后端服务器响应时间(代理接收超时)

下面修改一下后端server8000 服务,配置为6s返回结果,在Nginx上配置proxy_read_timeout 5; 也就是5s没收到后端返回视为超时。

来看一下结果:

[TIME]15/Jun/2023:11:17:52 +0800 [CLIENTIP]- [PREIP]172.25.48.53 [SERVERNAME]_ [SERVERIP]172.25.78.46:8000, 172.25.78.46:8800 [REQTIME]5.008 [SERVERRSPTIME]5.006, 0.002 [REQUEST]GET /test_view/timeout/?num=1 HTTP/1.1 [STATUS]200 [BODYBYTES]15 [REFERER]- 
[TIME]15/Jun/2023:11:17:56 +0800 [CLIENTIP]- [PREIP]172.25.48.53 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=1 HTTP/1.1 [STATUS]200 [BODYBYTES]15 [REFERER]- 
[TIME]15/Jun/2023:11:18:11 +0800 [CLIENTIP]- [PREIP]172.25.48.53 [SERVERNAME]_ [SERVERIP]172.25.78.46:8000, 172.25.78.46:8800 [REQTIME]5.003 [SERVERRSPTIME]5.000, 0.002 [REQUEST]GET /test_view/timeout/?num=1 HTTP/1.1 [STATUS]200 [BODYBYTES]15 [REFERER]- 
[TIME]15/Jun/2023:11:18:13 +0800 [CLIENTIP]- [PREIP]172.25.48.53 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=1 HTTP/1.1 [STATUS]200 [BODYBYTES]15 [REFERER]- 
[TIME]15/Jun/2023:11:18:16 +0800 [CLIENTIP]- [PREIP]172.25.48.53 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=1 HTTP/1.1 [STATUS]200 [BODYBYTES]15 [REFERER]- 
[TIME]15/Jun/2023:11:18:19 +0800 [CLIENTIP]- [PREIP]172.25.48.53 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=1 HTTP/1.1 [STATUS]200 [BODYBYTES]15 [REFERER]- 
[TIME]15/Jun/2023:11:18:20 +0800 [CLIENTIP]- [PREIP]172.25.48.53 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=1 HTTP/1.1 [STATUS]200 [BODYBYTES]15 [REFERER]- 
[TIME]15/Jun/2023:11:18:30 +0800 [CLIENTIP]- [PREIP]172.25.48.53 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=1 HTTP/1.1 [STATUS]200 [BODYBYTES]15 [REFERER]- 
[TIME]15/Jun/2023:11:18:41 +0800 [CLIENTIP]- [PREIP]172.25.48.53 [SERVERNAME]_ [SERVERIP]172.25.78.46:8000, 172.25.78.46:8800 [REQTIME]5.005 [SERVERRSPTIME]5.002, 0.002 [REQUEST]GET /test_view/timeout/?num=1 HTTP/1.1 [STATUS]200 [BODYBYTES]15 [REFERER]- 
[TIME]15/Jun/2023:11:18:46 +0800 [CLIENTIP]- [PREIP]172.25.48.53 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=1 HTTP/1.1 [STATUS]200 [BODYBYTES]15 [REFERER]- 
[TIME]15/Jun/2023:11:18:47 +0800 [CLIENTIP]- [PREIP]172.25.48.53 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.002 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=1 HTTP/1.1 [STATUS]200 [BODYBYTES]15 [REFERER]- 
[TIME]15/Jun/2023:11:18:48 +0800 [CLIENTIP]- [PREIP]172.25.48.53 [SERVERNAME]_ [SERVERIP]172.25.78.46:8800 [REQTIME]0.003 [SERVERRSPTIME]0.002 [REQUEST]GET /test_view/timeout/?num=1 HTTP/1.1 [STATUS]200 [BODYBYTES]15 [REFERER]- 

从日志中可以看出,正如之前所猜想的,当转发给后端,后端返回超时的话,Nginx将请求转发给另一个节点。

假如说当超时的时候,不希望转发请求, 则可以配置proxy_next_upstream off;即可。

总结

  • 原生的Nginx通过被动检测后端的应用是否可用

  • 健康检测主要跟proxy_next_upstream,proxy_connect_timeout, proxy_send_timeout, proxy_read_timeout参数有关

参考

[# 详解nginx的原生被动健康检查机制&灾备使用](详解nginx的原生被动健康检查机制&灾备使用(含测试)_nginx被动健康检查_无影V随风的博客-CSDN博客

[# QQA: Nginx 如何做健康检查?](QQA: Nginx 如何做健康检查? | 三点水)

posted @ 2023-06-15 11:38  xuege  阅读(958)  评论(0编辑  收藏  举报