小兔网

今天发现了一个很神奇的事情,php日志中有一条超时的日志,但是我request_terminate_timeout中设置的是0,理论上应该没有超时时间才对。

PHP Fatal error: Maximum execution time of 30 seconds exceeded in ...

OK,先列出现在的配置:

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 适用于,当max_execution_time由于某种原因无法终止脚本的时候,会把这个php-fpm请求干掉。
再看看max_execution_time的注释:这设置了脚本被解析器中止之前允许的最大执行时间,默认是30s。看样子,我这个请求应该是被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执行时间受到2方面控制,一个是php.ini的max_execution_time(要注意的是sleep,http请求等待响应的时间是不算的,这里算的是真正的执行时间),另一个是php-fpm request_terminate_timeout 设置,这个算的是请求开始n秒。