Nginx + php-fpm“504 Gateway Time-out”错误几乎为零负载(在测试服务器上)

rah*_*286 34 nginx timeout gateway fastcgi php-fpm

调试 6 小时后 - 我放弃了:|

我们在 LAN 中有一个 nginx+php-fpm+mysql,有近 100 个 wordpress(由不同的设计人员/开发人员创建和使用,他们都在测试 wordpres 设置)

我们长期使用 nginx 没有任何问题。

今天,突然间——nginx开始突然返回“504网关超时”......

我检查了虚拟主机的 nginx 错误日志...

2010/09/06 21:24:24 [error] 12909#0: *349 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 192.168.0.1, server: rahul286.rtcamp.info, request: "GET /favicon.ico HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "rahul286.rtcamp.info"
2010/09/06 21:25:11 [error] 12909#0: *349 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 192.168.0.1, server: rahul286.rtcamp.info, request: "GET /favicon.ico HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "rahul286.rtcamp.info"
2010/09/06 21:25:11 [error] 12909#0: *443 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 192.168.0.1, server: rahul286.rtcamp.info, request: "GET /info.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "rahul286.rtcamp.info"
2010/09/06 21:25:12 [error] 12909#0: *443 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.0.1, server: rahul286.rtcamp.info, request: "GET /favicon.ico HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "rahul286.rtcamp.info"
2010/09/06 22:08:32 [error] 12909#0: *1025 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 192.168.0.1, server: rahul286.rtcamp.info, request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "rahul286.rtcamp.info"
2010/09/06 22:09:33 [error] 12909#0: *1025 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 192.168.0.1, server: rahul286.rtcamp.info, request: "GET /favicon.ico HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "rahul286.rtcamp.info"
2010/09/06 22:09:40 [error] 12909#0: *1064 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 192.168.0.1, server: rahul286.rtcamp.info, request: "GET /info.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "rahul286.rtcamp.info"
2010/09/06 22:09:40 [error] 12909#0: *1064 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.0.1, server: rahul286.rtcamp.info, request: "GET /favicon.ico HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "rahul286.rtcamp.info"
2010/09/06 22:24:44 [error] 12909#0: *1313 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 192.168.0.1, server: rahul286.rtcamp.info, request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "rahul286.rtcamp.info"
2010/09/06 22:24:53 [error] 12909#0: *1313 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 192.168.0.1, server: rahul286.rtcamp.info, request: "GET /favicon.ico HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "rahul286.rtcamp.info"
Run Code Online (Sandbox Code Playgroud)

当我通过 TCP 模式在端口 9000 上运行 php-fpm 时,我运行了“netstat | grep 9000”并注意到了一些不寻常的东西...... (在此处粘贴部分输出以便于阅读)

tcp        9      0 localhost:9000          localhost:36094         CLOSE_WAIT  14269/php5-fpm  
tcp        0      0 localhost:46664         localhost:9000          FIN_WAIT2   -               
tcp     1257      0 localhost:9000          localhost:36135         CLOSE_WAIT  -               
tcp     1257      0 localhost:9000          localhost:36125         CLOSE_WAIT  -               
tcp        9      0 localhost:9000          localhost:36102         CLOSE_WAIT  14268/php5-fpm  
tcp        0      0 localhost:46662         localhost:9000          FIN_WAIT2   -               
tcp      745      0 localhost:9000          localhost:46644         CLOSE_WAIT  -               
tcp        0      0 localhost:46658         localhost:9000          FIN_WAIT2   -               
tcp     1265      0 localhost:9000          localhost:46607         CLOSE_WAIT  -               
tcp        0      0 localhost:46672         localhost:9000          ESTABLISHED 12909/nginx: worker
tcp     1257      0 localhost:9000          localhost:36119         CLOSE_WAIT  -               
tcp     1265      0 localhost:9000          localhost:46613         CLOSE_WAIT  -               
tcp        0      0 localhost:46646         localhost:9000          FIN_WAIT2   -               
tcp     1257      0 localhost:9000          localhost:36137         CLOSE_WAIT  -               
tcp        0      0 localhost:46670         localhost:9000          ESTABLISHED 12909/nginx: worker
tcp     1265      0 localhost:9000          localhost:46619         CLOSE_WAIT  -               
tcp     1336      0 localhost:9000          localhost:46668         ESTABLISHED -               
tcp        0      0 localhost:46648         localhost:9000          FIN_WAIT2   -               
tcp     1336      0 localhost:9000          localhost:46670         ESTABLISHED -               
tcp        9      0 localhost:9000          localhost:36108         CLOSE_WAIT  14274/php5-fpm  
tcp     1336      0 localhost:9000          localhost:46684         ESTABLISHED -               
tcp        0      0 localhost:46674         localhost:9000          ESTABLISHED 12909/nginx: worker
tcp     1336      0 localhost:9000          localhost:46666         ESTABLISHED -               
tcp     1257      0 localhost:9000          localhost:46648         CLOSE_WAIT  -               
tcp     1336      0 localhost:9000          localhost:46678         ESTABLISHED -               
tcp        0      0 localhost:46668         localhost:9000          ESTABLISHED 12909/nginx: wo             
Run Code Online (Sandbox Code Playgroud)

有很多“CLOSE_WAIT”和“FIN_WAIT2”对如下突出显示(在上面的输出中):

tcp     1337      0 localhost:9000          localhost:46680         CLOSE_WAIT  -               
tcp        0      0 localhost:46680         localhost:9000          FIN_WAIT2   -
Run Code Online (Sandbox Code Playgroud)

请注意上面的端口 46680。

我启用了mysql慢查询错误日志,但是没有用。

截至目前,通过 cronjob(请参阅下面的命令)每分钟重新启动 php5-fpm 保持一切“顺利”运行,但我讨厌拼凑,并想解决这个问题......

1 * * * * service php5-fpm restart > /dev/null
Run Code Online (Sandbox Code Playgroud)

我在谷歌上广泛搜索 - 没有任何帮助。如前所述,这是 LAN 中的测试服务器,CPU 负载永远不会超过 0.10,内存使用率也低于 25%(系统有 2GB RAM 和 ubuntu-server 安装)所以如果你发现它的时间混乱来帮助我,请至少给个提示。

预先感谢您的帮助。

-拉胡尔

(注意 - 这是重新发布 - http://forum.nginx.org/read.php?11,127694

更新:我找到了答案,贴在下面。

rah*_*286 33

我在 nginx 论坛上的帖子中找到了答案 - http://forum.nginx.org/read.php?2,127854

就我而言,答案是设置:

request_terminate_timeout=30s
Run Code Online (Sandbox Code Playgroud)

在 php-fpm 配置中(通常/etc/php5/fpm/php-fpm.conf

请注意,您也可以使用 30s 以外的值。

我用它来匹配我在主php.ini文件中的值,即:

max_execution_time = 30
Run Code Online (Sandbox Code Playgroud)

谢谢大家。:-)

  • 这个配置也可以在 www.conf 文件中找到。不过,感谢您的回答,这似乎已经成功了。 (5认同)
  • 这是一个池级指令,当您尝试将其放入 php-fpm.conf 的“[global]”部分时,您将收到一条错误消息。只有当您在那里也有池配置时,它才能在那里工作。另外:[request_terminate_timeout docs](http://php.net/manual/en/install.fpm.configuration.php#request-terminate-timeout)。 (2认同)
  • 我发现将 `request_terminate_timeout=30s` 放入我的 `php-fpm.conf` 文件会导致 (111 Connection Refused) 错误。当我将它移到我的 `www.conf` 文件时,它起作用了。 (2认同)

小智 18

这是它如何解决我的问题:

在 http { 部分对 /etc/nginx/nginx.conf 进行以下更改

proxy_connect_timeout  600s;
proxy_send_timeout  600s;
proxy_read_timeout  600s;
fastcgi_send_timeout 600s;
fastcgi_read_timeout 600s;
Run Code Online (Sandbox Code Playgroud)

然后重启nginx

/etc/init.d/nginx重启

  • 但幸运的是,这正是我所需要的 :) (3认同)
  • 是的,这看起来确实与提出问题的人的问题没有任何关系。 (2认同)