PHP-FPM偶尔会出现“FastCGI:无法连接到服务器”的错误

Ubuntu 14.04.3 LTS Apache 2.4.7 PHP 5.5.9 

两周前我从mod_php切换到了PHP-FPM。 一切,大部分运行顺利。 除了现在两次我有一个情况下,Apache / PHP将变得没有反应。 重启会解决这个问题,但是我想知道为什么会发生这种情况。 这里是错误日志,其中充满了数百种类似的错误。

 # /var/log/apache2/error.log . . [Wed Dec 16 23:19:21.476641 2015] [fastcgi:error] [pid 32523] (104)Connection reset by peer: [client xx.xx.xx.xx:43676] FastCGI: comm with server "/usr/lib/cgi-bin/php5-fcgi" aborted: read failed [Wed Dec 16 23:19:21.476866 2015] [fastcgi:error] [pid 32411] (2)No such file or directory: [client xx.xx.xx.xx:63082] FastCGI: failed to connect to server "/usr/lib/cgi-bin/php5-fcgi": connect() failed [Wed Dec 16 23:19:21.477489 2015] [fastcgi:error] [pid 32527] (104)Connection reset by peer: [client xx.xx.xx.xx:49675] FastCGI: comm with server "/usr/lib/cgi-bin/php5-fcgi" aborted: read failed [Wed Dec 16 23:19:21.478270 2015] [fastcgi:error] [pid 32548] (2)No such file or directory: [client xx.xx.xx.xx:59140] FastCGI: failed to connect to server "/usr/lib/cgi-bin/php5-fcgi": connect() failed . . 

Apacheconfiguration

 # /etc/apache2/conf-available/php5-fpm.conf <IfModule mod_fastcgi.c> AddHandler php5-fcgi .php Action php5-fcgi /php5-fcgi Alias /php5-fcgi /usr/lib/cgi-bin/php5-fcgi FastCgiExternalServer /usr/lib/cgi-bin/php5-fcgi -socket /var/run/php5-fpm.sock -pass-header Authorization <Directory /usr/lib/cgi-bin> Require all granted </Directory> </IfModule> 

PHP5-FPMconfiguration(包括我认为可能有用的)

 # /etc/php5/fpm/pool.d/www.conf listen = /var/run/php5-fpm.sock . . user = www-data group = www-data . . pm.max_children = 10 pm.start_servers = 5 pm.min_spare_servers = 5 pm.max_spare_servers = 1 pm.max_requests = 500 . . listen.owner = www-data listen.group = www-data listen.mode = 0660 . . 

连接到套接字

 # lsof -U | grep php php5-fpm 14373 www-data 0u unix 0xffff8801ff42bb80 0t0 1721 /var/run/php5-fpm.sock php5-fpm 17084 www-data 0u unix 0xffff8801ff42bb80 0t0 1721 /var/run/php5-fpm.sock php5-fpm 18544 www-data 0u unix 0xffff8801ff42bb80 0t0 1721 /var/run/php5-fpm.sock php5-fpm 18544 www-data 4u unix 0xffff8800da1e7700 0t0 701371 /var/run/php5-fpm.sock php5-fpm 18649 www-data 0u unix 0xffff8801ff42bb80 0t0 1721 /var/run/php5-fpm.sock php5-fpm 19672 www-data 0u unix 0xffff8801ff42bb80 0t0 1721 /var/run/php5-fpm.sock 

请让我知道是否有任何其他信息需要帮助解决这个问题。 正如我所说,现在只发生过两次。 谢谢

为此不要使用套接字。 它们阻塞太多,甚至不能支持有大量请求的中等负载站点。使用TCP套接字来连接httpd到php-fpm。

有关说明,请参阅: https : //wiki.apache.org/httpd/PHP-FPM (请参阅标题为“TCP套接字(IP和端口)方法”的小节)。

你也没有很多的工人build立。 我通常推荐每个可用CPU核心大约两个(假设大多数Web请求将需要500毫秒)。 你可以根据你的平均响应时间做你自己的math。 如果您需要更多的员工来支持您的负载,请获得更多的服务器。

我已经能够解决连接错误。 我没有足够的资源分配给php-fpm。 我增加了工人的数量(感谢Joel),设置了超时限制(感谢Froggiz)并设置了max_requests值。 以下文章详细解释了我遇到的问题。

https://stackoverflow.com/questions/18009479/random-php-fastcgi-connection-reset-by-peer-incomplete-headers

谢谢!