增加 PHP-FPM 空闲超时设置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40360922/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:09:42  来源:igfitidea点击:

Increase PHP-FPM idle timeout setting

phpapachemod-fastcgi

提问by apokryfos

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

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

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

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:

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

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

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

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?)

现在我的问题是,将空闲超时增加到一个小时左右但仍然确保 PHP 脚本运行时间不超过 30 秒是否明智,除非我们使用set_time_limit? 有没有办法在每个脚本的基础上设置空闲超时(类似于set_time_limit?)

Here is our pool configuration:

这是我们的池配置:

[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

And here is our fastcgi.conf

这是我们的 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>

回答by seven

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

当从 mod_php 迁移时,我发现自己处于类似的情况下,长时间运行的进程和 php-fpm 和 fastcgi。

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 的 fastcgi 代理,该代理终止了与 php-fpm 池的连接,因为您的脚本在 30 秒内没有输出任何内容。

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

您可以在 apache 配置中更改 idle-timeout 以扩展它(不能为 0):

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

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

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

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

Apache 代理会终止与 php 的连接,因此从 php 设置 max_execution_time 或 set_time_limit 并不重要。

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.

AFAIK如果 php 通过 mod_fastcgi 在 Apache 上运行,则无法从 php 代码或 .user.ini 或通过 apache (.htaccess) 设置每个脚本的时间限制。所以这意味着通过在一个地方扩展它,你可以延长超时时间。您的前端和后端用户。或者,您可以通过 2 个虚拟主机将其分开并在那里定义不同的超时值。

回答by Machavity

While this doesn't necessarily fit the OP's configuration, most people are going to be running PHP-FPM under a proxy setting. As such, you can set the timeout for a proxy setup like so (this is my php.conf)

虽然这不一定适合 OP 的配置,但大多数人将在代理设置下运行 PHP-FPM。因此,您可以像这样设置代理设置的超时(这是我的php.conf

<Proxy "fcgi://127.0.0.1:9000">
   ProxySet timeout=300
</Proxy>

<FilesMatch \.php$>
    SetHandler application/x-httpd-php
    SetHandler "proxy:fcgi://127.0.0.1:9000"
</FilesMatch>

If you're using a .sockfile instead, just replace both instances of fcgi://127.0.0.1:9000with the command to use the sock file

如果您使用的是.sock文件,只需用fcgi://127.0.0.1:9000命令替换两个实例即可使用 sock 文件