Nginx + php5-fpm not respecting set_time_limit


Nginx + php5-fpm not respecting set_time_limit

PHP set_time_limit即使小于其他限制也不起作用。例如,以下代码将运行到最后:

<?php
    set_time_limit(5);
    sleep(10);
    echo "Did not work!";
?>

我知道request_terminate_timeoutfastcgi_read_timeout,但这些在这种情况下无关紧要。

如何使nginx+php5-fpm尊重我设置的时间限制?

我对两者都有几乎默认的配置:

嘟嘟:

location ~ '.php$ {
    try_files $uri =404;
    fastcgi_split_path_info ^(.+'.php)(/.+)$;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
}

PHP5-FPM:

Default config, no limit specified (request_terminate_timeout = 0)

.PHP:

max_execution_time = 30

虽然在php-fpm文档中说request_terminate_timeout"This option should be used when the 'max_execution_time' ini option does not stop script execution for some reason."

我仍然想找到解决这个问题的方法...

注意:我使用的是 Ubuntu 14.04、Nginx 1.4.6 和 PHP 5.5。

您需要阅读 set_time_limit() 上的手册:

set_time_limit() 函数和配置指令 max_execution_time只影响脚本的执行时间 本身。在执行之外发生的活动上花费的任何时间 脚本,例如使用 system() 的系统调用、流操作、 确定最大值时不包括数据库查询等 脚本运行的时间。

这也适用于sleep()命令,如手册中的注释中所述。所以睡觉的时间甚至不算在内。在脚本末尾的睡眠命令之后,累积时间几乎为零。

您可以尝试您的时间设置是否有效,例如运行无限循环并测量从脚本开始到结束所花费的时间。