php-fpm超时时间设置request_terminate_timeout资源问题分析
php  /  管理员 发布于 7年前   196
php日志中有一条超时的日志,但是我request_terminate_timeout中设置的是0,理论上应该没有超时时间才对。 PHP Fatal error: Maximum execution time of 30 seconds exceeded in ... OK,先列出现在的配置: php-fpm: 先查阅了一下php-fpm文件中关于request_terminate_timeout的注释 ; The timeout for serving a single request after which the worker process will 这个注释说明了,request_terminate_timeout 适用于,当max_execution_time由于某种原因无法终止脚本的时候,会把这个php-fpm请求干掉。 再看看max_execution_time的注释:这设置了脚本被解析器中止之前允许的最大执行时间,默认是30s。看样子,我这个请求应该是被max_execution_time这个设置干掉了。 好吧,不死心,做了一个实验: 好吧,结论是web请求php执行时间受到2方面控制,一个是php.ini的max_execution_time(要注意的是sleep,http请求等待响应的时间是不算的,这里算的是真正的执行时间),另一个是php-fpm request_terminate_timeout 设置,这个算的是请求开始n秒。 request_terminate_timeout引起的资源问题 request_terminate_timeout的值如果设置为0或者过长的时间,可能会引起file_get_contents的资源问题。 真正能够控制 PHP 脚本最大执行时间的是 php-fpm.conf 配置文件中的request_terminate_timeout参数。 Nginx 将给用户返回“502 Bad Gateway”。修改该参数,设置一个 PHP 脚本最大执行时间是必要的, 解决办法是:request_terminate_timeout设置为10s或者一个合理的值, php-fpm中的request_terminate_timeout最好不要设置 刚转到php-fpm没几天就发现,进入我的joomla后台,firefox偶尔会给我白屏的那种http 503,这种情况仅出现在天翼云的服务器上,而我在国外的同样配置的服务器一点问题都没有,后来发现是request_terminate_timeout的问题。 每次登陆joomla后台,joomla都会去检查是否有更新(检查成功后cache,默认保存该cache 6小时),而且分为joomla主程序和joomla扩展两个部分,如下图: 不出意外的话,服务器会发起两个php进程,分别分配给两个php-fpm children,去连接joomla的官方update服务器。好,问题就来了,我的request_terminate_timeout = 30s,30秒不完成则超时,参见天翼云主机的国际出口相当蛋疼!没错,30秒内,天翼云主机根本无法完成连接joomla更新服务器并检查是否有更新这整个过程。这也很好解释了为什么同样配置的国外服务器就没有问题,因为它们完成上述更细过程仅需要在2~5秒左右。 我的apache超时设置是30秒,php.ini中最长执行时间野是30秒,多年来都没有任何问题,没有30秒还打不开的网页,所以我就没多想给php-fpm的request_terminate_timeout = 30s。经过这次的事情发现此30秒非鄙30秒啊…… php-fpm设置request_terminate_timeout后,php.ini中的max_execution_time和max_input_time都会失效,以php-fpm中的设置为准; [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] NOTICE: [pool www] child 1886 started 像joomla这种全php的网站,每个连接都需要apache+php-fpm协同运作。即便php-fpm中的request_terminate_timeout时间设置很长,apache中的timeout时间设置略短,只要apache的timeout到了,php-fpm照样在后面杀进程……
request_terminate_timeout = 0
php.ini:
max_execution_time = 30
; 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: 0php-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日志中有杀掉子进程日志
如果file_get_contents请求的远程资源如果反应过慢,file_get_contents就会一直卡在那里不会超时。我们知道php.ini 里面max_execution_time 可以设置 PHP 脚本的最大执行时间,但是,在 php-cgi(php-fpm) 中,该参数不会起效。
request_terminate_timeout默认值为 0 秒,也就是说,PHP 脚本会一直执行下去。
这样,当所有的 php-cgi 进程都卡在 file_get_contents() 函数时,这台 Nginx+PHP 的 WebServer 已经无法再处理新的 PHP 请求了,
但是,治标不治本。例如改成 30s,如果发生 file_get_contents() 获取网页内容较慢的情况,这就意味着 150 个 php-cgi 进程,每秒钟只能处理 5 个请求,WebServer 同样很难避免”502 Bad Gateway”。
或者给file_get_contents加一个超时参数。$ctx = stream_context_create(array( 'http' => array( 'timeout' => 10 //设置一个超时时间,单位为秒 ))); file_get_contents($str, 0, $ctx);
apache+mod_php在timeout后,只会在日志中记录一下,仅此而已。php-fpm中的request_terminate_timeout超时之后,日志中记录http 503的同时,最要命的,它还会直接杀死造成这个http 503的php-fpm child,并生成新的child。
在我的joomla更新这个实例中,就会有两个php-fpm children同时被杀死。而我的天翼云主机是低配,只有一个cpu核心,我也只启动了两个php-fpm children,两个同时死了,我的firefox这边也就http 503 Service Unavailable的白屏了。php-fpm的error_log如下:
[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] 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
如果网站的访问者比较多,php-fpm的child是被许多访问者共用的,杀一个child,就有可能导致几个用户同时http 503 Service Unavailable。所以,我的建议是――php-fpm中的request_terminate_timeout最好不要设置,只给apache一个timeout就够了。
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号