PHP-FPM超时机制的差异与配置现状
在查看PHP日志时,发现了一条关于超时的记录。然而,检查php-fpm的配置,其中request_terminate_timeout被设置为0。按照理论,该值设为0表示关闭超时限制,因此不应出现超时情况。
实际报错信息显示为:PHP Fatal error: Maximum execution time of 30 seconds exceeded in ...
首先梳理当前的相关配置如下:
php-fpm: request_terminate_timeout = 0 php.ini: max_execution_time = 30
查阅php-fpm配置文件源码中关于request_terminate_timeout参数的注释说明。
; The timeout for serving a single request after which the worker process will ; be killed. This option should be used when the 'max_execution_time' ini option ; does not stop script execution for some reason. A value of '0' means 'off'. ; Available units: s(econds)(default), m(inutes), h(ours), or d(ays) ; Default Value: 0
根据上述注释可知,request_terminate_timeout的作用是:当php.ini中的max_execution_time因某些原因未能成功终止脚本执行时,php-fpm将强制杀死该工作进程。若值为'0',则代表禁用此功能。
接着查看max_execution_time的注释:它定义了脚本在被解析器中止前允许的最大执行时间,默认值为30秒。结合之前的报错信息推测,此次请求很可能是被max_execution_time这一设置所终止的。
为了验证猜想,进行了一项对比实验。
| php-fpm request_terminate_timeout 设置 | 0 | 15 |
| php.ini max_execution_time 设置 | 30 | 30 |
| 执行结果 | php有Fatal error超时日志,http状态码为500 | php无Fatal error超时日志,http状态码为502,php-fpm日志中有杀掉子进程日志 |
实验结论表明,Web请求中PHP的执行时间受到两个层面的控制:一是php.ini中的max_execution_time,需注意sleep函数耗时及HTTP响应等待时间不计入内,仅统计真正的代码执行时间;二是php-fpm中的request_terminate_timeout,它计算的是从请求开始到结束的总时长。
request_terminate_timeout引发的资源耗尽与502错误
接下来分析由request_terminate_timeout配置不当引发的资源耗尽问题。
如果将request_terminate_timeout设置为0或过长的时间,可能会在使用file_get_contents获取远程资源时引发资源阻塞。若远程服务器响应缓慢,file_get_contents会一直挂起等待。虽然php.ini中的max_execution_time旨在限制脚本最大执行时间,但在php-cgi(即php-fpm)模式下,该参数在某些IO场景下可能无法生效。
实际上,真正能强制控制PHP脚本最大存活时间的是php-fpm.conf中的request_terminate_timeout参数。其默认值为0秒,意味着脚本理论上可以无限期执行。当所有php-cgi进程都卡在file_get_contents()函数等待响应时,Nginx+PHP架构的Web服务器将无法处理新的PHP请求。
此时,Nginx会向用户返回“502 Bad Gateway”错误。虽然可以通过修改该参数来设定一个最大执行时间,但这只是治标不治本。例如设置为30秒,若file_get_contents()获取内容依然缓慢,以150个php-cgi进程为例,每秒仅能处理约5个请求,Web服务器仍难以避免“502 Bad Gateway”。
推荐的解决办法有两种:一是将request_terminate_timeout设置为10秒或一个合理的数值;二是在代码中给file_get_contents添加超时参数。
$ctx = stream_context_create(array(
'http' => array(
'timeout' => 10 //设置一个超时时间,单位为秒
)
));
file_get_contents($str, 0, $ctx);
实战案例:Joomla更新导致的503白屏问题分析
进一步探讨为何不建议随意设置php-fpm中的request_terminate_timeout。
在迁移至php-fpm初期,访问Joomla后台时偶尔会出现Firefox白屏并提示HTTP 503错误。该现象仅在特定云服务商(天翼云)的服务器上出现,而在同等配置的国外服务器上正常。经排查,问题根源在于request_terminate_timeout的设置。
每次登录Joomla后台,系统都会检查是否有更新(检查成功后缓存,默认保存6小时)。更新检查分为Joomla主程序和扩展两部分,涉及多次外部连接。

正常情况下,服务器会发起两个PHP进程,分别分配给两个php-fpm子进程去连接Joomla官方更新服务器。问题在于,我的服务器设置了request_terminate_timeout = 30秒,超过30秒未完成则视为超时。由于天翼云主机国际出口网络状况不佳,30秒内根本无法完成连接Joomla更新服务器并检查更新的整个过程。这也解释了为何相同配置的国外服务器没有问题,因为其完成上述过程仅需2~5秒左右。
此前Apache的超时设置和php.ini中的最长执行时间均为30秒,多年来运行稳定,从未出现过网页无法打开的情况,因此未对php-fpm的request_terminate_timeout做额外调整,直接沿用30秒。此次事件表明,不同环境下的30秒含义截然不同。
当php-fpm设置了request_terminate_timeout后,php.ini中的max_execution_time和max_input_time将失效,以php-fpm的设置为准。相比之下,Apache+mod_php在超时后仅记录日志,不会中断服务。而php-fpm在request_terminate_timeout超时后,不仅记录HTTP 503日志,更致命的是会直接杀死导致超时的php-fpm child进程,并生成新的child。在Joomla更新实例中,两个php-fpm children同时被杀死。由于天翼云主机配置较低,仅有一个CPU核心且只启动了两个php-fpm children,两者同时死亡导致前端用户看到HTTP 503 Service Unavailable的白屏。php-fpm的error_log记录了以下信息:
[27-Sep-2014 10:41:06] WARNING: [pool www] child 1882, script '/home/onepx/public_html/administrator/index.php' (request: "POST /administrator/index.php") execution timed out (30.004534 sec), terminating [27-Sep-2014 10:41:06] WARNING: [pool www] child 1882 exited on signal 15 (SIGTERM) after 164.717323 seconds from start
[27-Sep-2014 10:41:06] NOTICE: [pool www] child 1886 started [27-Sep-2014 10:41:06] WARNING: [pool www] child 1883, script '/home/onepx/public_html/administrator/index.php' (request: "POST /administrator/index.php") execution timed out (30.005201 sec), terminating [27-Sep-2014 10:41:06] WARNING: [pool www] child 1883 exited on signal 15 (SIGTERM) after 166.718162 seconds from start [27-Sep-2014 10:41:06] NOTICE: [pool www] child 1887 started
对于像Joomla这样全PHP的网站,每个连接都需要Apache与php-fpm协同运作。即使php-fpm中的request_terminate_timeout设置较长,只要Apache的timeout先到达,Apache也会在后端终止进程。如果网站访问量较大,php-fpm的child进程被众多访问者共享,杀死一个child可能导致多个用户同时遭遇HTTP 503 Service Unavailable。因此,建议是——最好不要在php-fpm中设置request_terminate_timeout,仅依靠Apache的timeout即可。