增加PHP-FPM空闲超时设置 [英] Increase PHP-FPM idle timeout setting

查看:395
本文介绍了增加PHP-FPM空闲超时设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们最近已迁移到PHP-FPM.但是,我们遇到了一些长时间运行的脚本的问题.该代码大致如下:

We have recently migrated to PHP-FPM. However we have encountered a problem with some long running scripts. The code looks roughly like:

foreach ($items as $item) {
     set_time_limit(30);
     proccessThatTakesAround2secs(); 
}

正常的PHP脚本时间限制也是30秒.以前这很好,因为我们将每个项目的剩余时间限制重置为30秒.大约有1000个项目,这意味着该脚本通常总共需要30分钟才能完成.但是,此后我们遇到了以下问题:

The normal PHP script time limit is also 30 secs. This was previously working fine in that we reset the remaining time limit back to 30 secs for each item. There are around 1000 items meaning the script in total would normally take about 30 mins to complete. However we have since reached the following problem:

FastCGI:与服务器"/usr/local/php-5.6.24/sbin/php5-fpm"的通讯已中止:空闲超时(30秒)

FastCGI: comm with server "/usr/local/php-5.6.24/sbin/php5-fpm" aborted: idle timeout (30 sec)

现在我的问题是,将空闲超时增加到一个小时左右是否合理,但是除非我们使用set_time_limit,否则仍然确保PHP脚本的运行时间不超过30秒?有没有一种方法可以按脚本设置空闲超时(类似于set_time_limit?)

Now my question is, is it sensible to increase the idle timeout to something like an hour but still ensure the PHP scripts don't run for longer than 30 secs, unless we use set_time_limit? Is there a way to set the idle timeout on a per script basis (something akin to a set_time_limit?)

这是我们的池配置:

[www]
user = www-data
group = www-data
listen = /var/run/php5-fpm.sock
listen.owner = www-data
listen.group = www-data
pm = static
pm.max_children = 55
pm.max_requests = 10000

php_value[memory_limit] = 128M
php_value[max_execution_time] = 30
php_value[upload_max_filesize] = 20M
php_value[post_max_size] = 20M
php_value[max_input_vars] = 9999

这是我们的fastcgi.conf

And here is our fastcgi.conf

<IfModule mod_fastcgi.c>
    AddType application/x-httpd-fastphp5 .php
    Action application/x-httpd-fastphp5 /php5-fcgi
    Alias /php5-fcgi /usr/local/php-5.6.24/sbin/php5-fpm
    FastCgiExternalServer /usr/local/php-5.6.24/sbin/php5-fpm -socket /var/run/php5-fpm.sock -idle-timeout 30 -pass-header Authorization
    <Directory /usr/local/php-5.6.24/sbin/>
        Require all granted
   </Directory>
</IfModule>

推荐答案

从mod_php迁移后,我发现自己处于类似的情况,进程运行时间较长,并且php-fpm和fastcgi也是如此.

I have found my self in similar situation with long running processes and php-fpm and fastcgi when migrated from mod_php.

您看到的错误是来自Apache的fastcgi代理,该代理终止了与php-fpm池的连接,因为您的脚本在30秒内未输出任何内容.

The error you are seeing is from apache's fastcgi proxy who killed connection to php-fpm pool because your script did not outputted anything for 30 seconds.

您可以在apache配置中更改空闲超时以将其扩展(不能为0):

You can change idle-timeout in your apache config to extend it (cannot be 0):

FastCgiExternalServer /usr/lib/cgi-bin/php7-fcgi -socket /run/php/php7.0-fpm.sock -idle-timeout 1800 -pass-header Authorization

链是这样的:Apache-> FastCgiExternalServer代理-> php-fpm池服务器-> php进程

Chain goes like this: Apache -> FastCgiExternalServer proxy -> php-fpm pool server -> php process

Apache代理会终止与php的连接,因此从php设置max_execution_time或set_time_limit无关紧要.

Apache proxy kills connection to php, so setting max_execution_time or set_time_limit from php doesn't matter.

AFAIK 如果通过mod_fastcgi在Apache上运行php,则无法通过php代码或.user.ini或通过apache(.htaccess)设置每个脚本的时间限制.因此,这意味着通过将其扩展到一个位置,您可以延长超时时间,例如.您的前端和后端用户.另外,您可以通过2个虚拟主机将其分开,并在其中定义不同的超时值.

AFAIK if php is being run on Apache via mod_fastcgi, there is no way to set per script time limits from php code or .user.ini or via apache (.htaccess). So that means that by extending it in one place you are extending timeout for eg. both your frontend and backend users. Alternatively you could separate it via 2 vhosts and define different timeout values there.

这篇关于增加PHP-FPM空闲超时设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆