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

经过6个小时的debugging – 我放弃了:|

我们在局域网上有一个nginx + php-fpm + mysql,几乎有100个wordpress(由不同的devise人员/开发人员创build和使用,所有工作都在testingwordpres设置上)

我们使用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" 

当我通过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 

下面突出显示了大量的“CLOSE_WAIT”和“FIN_WAIT2”对(在上面的输出中):

 tcp 1337 0 localhost:9000 localhost:46680 CLOSE_WAIT - tcp 0 0 localhost:46680 localhost:9000 FIN_WAIT2 - 

请注意上面的端口46680。

我启用mysql缓慢查询错误日志,但它没有工作。

截至现在重新启动php5-fpm每分钟通过一个cronjob(见下面的命令)保持一切运行“顺利”,但我讨厌拼凑,并希望解决这个…

 1 * * * * service php5-fpm restart > /dev/null 

我在谷歌search广泛 – 没有帮助。 如上所述,这是一个局域网中的testing服务器,CPU负载不会超过0.10,内存使用率也低于25%(系统有2GB内存和安装的Ubuntu服务器)所以,如果你发现它的时间混淆帮助我,至less下降一个提示。

预先感谢您的帮助。

-Rahul

(注意 – 这是重新发布 – http://forum.nginx.org/read.php?11127694 )

更新:我find答案,这是张贴在下面。

我在nginx论坛发现了我的答案 – http://forum.nginx.org/read.php?2127854

对我来说,答案是:

 request_terminate_timeout=30s 

在php-fpmconfiguration(通常是/etc/php5/fpm/php-fpm.conf

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

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

 max_execution_time = 30 

谢谢大家。 🙂

这里如何解决我的问题:

在http {section中对/etc/nginx/nginx.conf进行如下修改

 proxy_connect_timeout 600s; proxy_send_timeout 600s; proxy_read_timeout 600s; fastcgi_send_timeout 600s; fastcgi_read_timeout 600s; 

然后重新启动nginx

/etc/init.d/nginx重新启动

如果你使用PHP 5.3,增加积压。

如果您使用的是PHP 5.2,请将修补程序反向移植以将其从128增加到backlog大小。

另外,使用unix套接字而不是TCP套接字。 unix:/tmp/php5-cgi.sock(或相关path)

万分感谢

request_terminate_timeout = 30秒

这对我来说是完美的

但是,我不得不在这个文件中插入一行:“/etc/php5/fpm/pool.d/www.conf”,也就是说在“Worker Section”中。

PHP 5.3.21-1 – WordPress的3.5.1

http://php-fpm.org/wiki/Configuration_File

我有同样的问题,我通过完全删除Apache来解决它:

 yum remove httpd 

之后,我build议同时使用PHP和NGINX:

 /etc/init.d/nginx restart /etc/init.d/php-fpm restart 

在我的情况(相同的nginx错误信息),一些有问题的PHP脚本没有结束执行和等待的东西,导致没有更多的php5-fpm孩子nginxselect。

固定:

  1. 添加执行时间限制其他提到这个post。 request_terminate_timeout=30s
  2. 提高孩子数量。 一切都像魅力一样 pm.max_spare_servers=16 pm.min_spare_servers=2

现在一切都像魅力一样工作。

对我来说,从服务器中删除rabbitmq后,发生了同样的问题。 上面没有什么是有用的,重新安装所有php5模块解决了这个问题。 我在该服务器上安装了Debian 8.2。 希望对某人有帮助。

这也可以帮助人们:

根据你的设置,你应该看看fastcgiconfiguration参数以及php …在我的情况下(我使用的是apache2 + php5-fpm),max_execution的时间也取决于fastcgi模块等待响应的时间-空闲超时) …

http://www.fastcgi.com/mod_fastcgi/docs/mod_fastcgi.html#FastCgiExternalServer