Nginx + php5-fpm 不尊重 set_time_limit

Kri*_*ian 0 php nginx

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

<?php
    set_time_limit(5);
    sleep(10);
    echo "Did not work!";
?>
Run Code Online (Sandbox Code Playgroud)

我知道request_terminate_timeoutfastcgi_read_timeout,但这些与本例无关。

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

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

nginx:

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;
}
Run Code Online (Sandbox Code Playgroud)

PHP5-FPM:

Default config, no limit specified (request_terminate_timeout = 0)
Run Code Online (Sandbox Code Playgroud)

PHP:

max_execution_time = 30
Run Code Online (Sandbox Code Playgroud)

尽管在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。

Mar*_*555 5

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

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

这也适用于sleep()命令,正如手册中的注释中也提到的那样。所以睡觉的时间根本不算在内。在脚本末尾执行 sleep 命令后,累计时间几乎为零。

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