方案

调整这几个参数来调大nginx超时时间

proxy_connect_timeout
proxy_send_timeout
proxy_read_timeout

nginx 三个代理超时时间配置

proxy_connect_timeout 60s;
Defines a timeout for establishing a connection with a proxied server. It should be noted that this timeout cannot usually exceed 75seconds.
定义与被代理服务器建立连接超时时间。请注意,此超时通常不能超过75秒。
proxy_send_timeout dedault 60s
Sets a timeout for transmitting a request to the proxied server. The timeout is set only between two successive write operations, not for the transmission of the whole request. If the proxied server does not receive anything within this time, the connection is closed.
设置请求传输到被代理服务器超时时间。只在两个连续的写操作之间设置超时,而不是整个请求传输。如果代理服务器在这段时间没有收到数据连接将被关闭
proxy_read_timeout default 60s
Defines a timeout for reading a response from the proxied server. The timeout is set only between two successive read operations, not for the transmission of the whole response. If the proxied server does not transmit anything within this time, the connection is closed.
定义从被代理服务器读取响应超时超时设置仅在两个连续的读取操作之间,而不是整个响应传输。如果代理服务器在这段时间内没有传输数据连接将被关闭

在这里插入图片描述

为什么不建议调大proxy_connect_timeout

nginx的被动健康检查 (阅)

upstream upcheck { server 127.0.0.1:8000 max_fails=1 fail_timeout=10s ; } 这个一个默认nginx被动健康检查配置max_fails=1 fail_timeout=10s
这段的大致意思是10s内,这个节点请求失败数超过1则会将其标记为down,下个10s内不会有新的请求到达该节点。详细的介绍大家可以官网或者论坛去搜索这里不再赘述。下面我着重讲一下proxy_conncet_timeout对他的影响。

被动健康检查一个常见的场景就是当后端有台服务器宕机的时候,如果proxy_connect_timeout
设置为5s,5s后nginx和后端建链超时,该条请求标记失败,该节点被置为down。这样可以在一定程度上做到快速止损。而假如我们proxy_connect_timeout
设置为60s,所有到达该节点的请求60s后才会因为超时剔除宕机节点。

下面我做一个简单实验来对比一下宕机情况下proxy_connect_timeout设置为5s和60s的区别

两组upstream都为2个节点,采用默认被动健康检查配置(max_fails=1
fail_timeout=10s),其中一个节点不可用(ping不通),关闭nginx重试,每秒请求一次,持续10分钟

nginx配置

server {
	listen 80;
	server_name _ ;
	access_log /opt/log/ngx-lan_access.log main;
	location = /upcheck {
    	proxy_next_upstream_tries 1;
		proxy_connect_timeout 5s;
		proxy_pass http://upcheck;
	} 
	location = /upcheck2 {
		proxy_next_upstream_tries 1;
		proxy_connect_timeout 60s;
		proxy_pass http://upcheck2;
	}
	upstream upcheck {
		server 127.0.0.1:8500 max_fails=1 fail_timeout=10s ;
		server 192.168.100.100:8500 max_fails=1 fail_timeout=10s ;
	}
	upstream upcheck2 {
    	server 127.0.0.1:8500 max_fails=1 fail_timeout=10s ;
    	server 192.168.100.100:8500 max_fails=1 fail_timeout=10s ;
	}
}

curl访问

for i in {1..600}; do curl http://127.0.0.1/upcheck -I > /dev/null 2>&1  & sleep 1; done
for i in {1..600}; do curl http://127.0.0.1/upcheck2 -I > /dev/null 2>&1  & sleep 1; done

然后统计一下这两组配置的200状态

5s的这组 200状态个数是 558
60s的这组 200状态码个数是 468

原文地址:https://blog.csdn.net/tangsiqi130/article/details/131091954

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。

如若转载,请注明出处:http://www.7code.cn/show_9709.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注