阅读(6381)

浅谈PHP-FPM参数

最后一次修改 2018年05月05日

process_control_timeout

英文解释

process_control_timeout
 mixed
Time limit for child processes to wait for a reaction on signals from master. Available units: s(econds), m(inutes), h(ours), or d(ays) Default Unit: seconds. Default value: 0.

中文翻译

process_control_timeout
 mixed
设置子进程接受主进程复用信号的超时时间。可用单位:s(秒),m(分),h(小时)或者 d(天)。默认单位:s(秒)。默认值:0(关闭)。

中文翻译有个不恰当的地方,英文解释里并没有指明该信号是复用信号。

我的理解:

处理请求

原则上,php-fpm会选择空闲的fastcgi进程去处理请求,在处理之前,php-fpm会给fastcgi发送信号,用来让fastcgi进程准备好接受请求处理。但是fastcgi进程并不总是能够处理请求,也就是不能总是响应该信号(比如出现假死的情况),这时候就需要设定php-fpm留给fastcgi进程响应信号的时间,如果超时了,php-fpm会想其他办法(例如选择其他fastcgi进程),这个就是process_control_timeout参数的作用

php-fpm进行reload

process_control_timeout = 10

 
<?php 
sleep(50);echo 1;sleep(20);    
//没有这个sleep,reload会立即生效echo 2;

当浏览器访问http://localhost时,进行php-fpm平滑reload,fastcgi信号收到关闭进程信号后,第一个sleep函数会直接返回,但是第二个sleep仍在执行。因此,php-fpm会被这一个旧fastcgi进程卡10s,超过后才能完成平滑重启

request_terminate_timeout

在php-fpm.conf文件中的描述如下:

; 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 = 0

翻译过来就是:
request_terminate_timeout参数设置了处理单个请求的超时时间,过了之后,该worker进程就会被kill掉。这个选项应该在php.ini文件的max_execution_time选项由于某种原因没有停止脚本的运行下使用。默认值为0,表示该选项为关闭状态。

正如上面所说,request_terminate_timeout设定的是请求的超时时间,而php.ini配置中的max_execution_time根据手册如下的解释,是脚本被允许的最大执行时间。

max_execution_time
 integer
这设置了脚本被解析器中止之前允许的最大执行时间,单位秒。 这有助于防止写得不好的脚本占尽服务器资源。 默认设置为 30。 从命令行运行 PHP 时,默认设置为 0。
最大执行时间不会影响系统调用和系统操作等。更多细节参见 set_time_limit()。
在 安全模式 下你不能通过 ini_set() 来修改此设置。 唯一的解决方法是关闭安全模式或者在 php.ini中修改时间限制。
你的 web 服务器也可以有其他超时设置,也有可能中断 PHP 的执行。 Apache 有一个 Timeout 指令,IIS 有一个 CGI 超时功能。 他们默认都是 300 秒。更多具体信息参见你的 web 服务器的文档。

区别如下:

超时后,request_terminate_timeout会返回502Bad Gateway了,而max_execution_time会抛出Fatal Error。

max_execution_time不包括诸如使用system(),sleep()的系统调用、流操作、数据库操作等的时间,所以比较鸡肋,而request_terminate_timeout会包含程序的完整请求时间。

另外,开启request_terminate_timeout并不会让max_execution_time失效,先到达谁的超时时间谁起作用。