2016-02-28 63 views
0

即使它小于其他限制,PHP set_time_limit也不起作用。 例如,下面的代码将运行到结束:Nginx + php5-fpm不尊重set_time_limit

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

我知道request_terminate_timeoutfastcgi_read_timeout,但这些都不是在这种情况下,相关的。

我该如何让nginx + php5-fpm尊重我设定的时间限制?

我有两个非常默认configurigation:

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。

回答

3

你需要阅读的手册set_time_limit()

的set_time_limit()函数的功能和配置指令 的max_execution_time只影响脚本本身 的执行时间。任何时间花费在脚本的执行 以外的任何时间,例如使用system(),流操作, 数据库查询等的系统调用,不包含当确定脚本已运行的最大 时间。

这也适用于sleep()命令,也在手册的注释中提到。所以睡眠时间甚至没有计算在内。在脚本结尾的睡眠命令之后,累计时间几乎为零。

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

+0

因为看起来你是对的。用'while(true);'它按预期工作:) – Kristian