【问题标题】:Nginx + php5-fpm not respecting set_time_limitNginx + php5-fpm 不尊重 set_time_limit
【发布时间】:2016-06-11 09:49:34
【问题描述】:

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

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

我知道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;
}

PHP5-FPM:

Default config, no limit specified (request_terminate_timeout = 0)

PHP:

max_execution_time = 30

虽然在php-fpm documentation 中提到了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。

【问题讨论】:

    标签: php nginx


    【解决方案1】:

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

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

    这也适用于sleep() 命令,如手册中的 cmets 中所述。所以连睡觉的时间都不算。在脚本末尾你的 sleep 命令之后,累计时间几乎为零。

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

    【讨论】:

    • 看来你是对的。使用while(true); 它可以按预期工作:)
    猜你喜欢
    • 2016-02-24
    • 1970-01-01
    • 2012-12-05
    • 2013-08-28
    • 2012-05-15
    • 2014-08-19
    • 1970-01-01
    • 2013-06-02
    • 2014-08-17
    相关资源
    最近更新 更多