Nginx超时20秒导致502错误网关

时间:2020-09-08 21:39:52

标签: nginx

奇怪的错误/错误/功能。在我的nginx配置中,我将超时设置得非常低,例如:

proxy_connect_timeout       20s;
proxy_send_timeout          20s;
proxy_read_timeout          20s;
send_timeout                20s;
reset_timedout_connection on;

有一个奇怪的发生。现在的超时时间是20秒,如果访问网站www.example.com,它有时会连接,或者有时会出现502 Bad Gateway error.50 / 50的机会。但是,如果我将其提高到60s,它就可以正常工作。

为什么会这样?

1 个答案:

答案 0 :(得分:0)

20s

proxy_connect_timeout确实不是很低-我建议它很高。这是nginx仅打开代理的套接字或端口所花费的时间(即,初始连接不使代理响应)。例如,我在其中一个配置中设置了100ms

我将注释您的设置,因为这些设置通常是从其他地方粘贴的货物崇拜的内容:

proxy_connect_timeout       20s; # time to open a connection to the proxy before we give up
proxy_send_timeout          20s; # timeout for transmitting a request to the proxied server
proxy_read_timeout          20s; # timeout for reading a response from the proxied server - did it send back anything before this has expired
send_timeout                20s; # timeout for sending a response to the requesting client - note this isn't proxy_send_timeout, but the time between two successive write operations to the requesting client (ie. browser)
reset_timedout_connection on;

当您说将超时更改为60s时-我想您对所有20s值都这样做了吗?

最可能的解释是您的代理响应速度慢-例如它必须执行慢速数据库查询或从慢速API请求数据,或者运行一些糟糕的php代码,在这种情况下,您需要更改的唯一超时值是proxy_read_timeout。您需要调查为什么Poxy另一端的代码响应缓慢。

另一个潜在的故障点是proxy_connect_timeout,但这表明您正在运行一个非守护进程或非常慢的CGI,仅需20多秒钟即可启动,启动,甚至可以开始处理nginx请求。