又一个 nginx 502

又一个 nginx 502

  

现象

  
最近碰到一个线上502的问题,查看 nginx 日志发现有类似如下日志:

2019/07/15 16:00:50 [error] 29640#0: *15 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:61005", host: "localhost:60005"

  

百度google了一下,发现有提到 request_terminate_timeout 可能导致这个问题。
  
  

小实验

  

配置下 fpm,设置为1秒

request_terminate_timeout = 1

  

写个PHP脚本:

<?php
for($i=1; $i<1000000000;$i++){
echo "";
}

  
访问下,果然 502了。

[root@VM_28_182_centos app.api.ke.com]# curl localhost:60005 
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html>
<head><title>502 Bad Gateway</title></head>
<body bgcolor="white">
<h1>502 Bad Gateway</h1>
<p>The proxy server received an invalid response from an upstream server. Sorry for the inconvenience.<br/>
Please report this message and include the following information to us.<br/>
Thank you very much!</p>
</html>

  
  

总结

  

从php官方网站获取到这个参数的说明:

request_terminate_timeout mixed

The timeout for serving a single request after which the worker process will be killed. This option should be used when the 'max_execution_time' ini option does not stop script execution for some reason. A value of '0' means 'Off'. Available units: s(econds)(default), m(inutes), h(ours), or d(ays). Default value: 0.

from https://php.net/manual/en/install.fpm.configuration.php

  
这个参数用来给 max_execution_time 兜底,如果执行时间到了还没有终止,那么就会强制杀掉。

从这个原理上来说,php crash 了,应该也会是同样的 502 和同样的 nginx error日志。

  
  

阅读感受肿么样?

平均分: / 5. 打分次数:

发表评论

电子邮件地址不会被公开。 必填项已用*标注