欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php-fpm超時(shí)時(shí)間設(shè)置request_terminate_timeout資源問(wèn)題分析

 更新時(shí)間:2019年09月27日 12:44:58   作者:龜仙人  
之前發(fā)現(xiàn)一個(gè)php配置之后關(guān)于返回500和502的問(wèn)題,今天看到一個(gè)兄弟寫(xiě)的非常不錯(cuò),記錄一下

php日志中有一條超時(shí)的日志,但是我request_terminate_timeout中設(shè)置的是0,理論上應(yīng)該沒(méi)有超時(shí)時(shí)間才對(duì)。

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

OK,先列出現(xiàn)在的配置:

php-fpm:
request_terminate_timeout = 0
php.ini:
max_execution_time = 30

先查閱了一下php-fpm文件中關(guān)于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

這個(gè)注釋說(shuō)明了,request_terminate_timeout 適用于,當(dāng)max_execution_time由于某種原因無(wú)法終止腳本的時(shí)候,會(huì)把這個(gè)php-fpm請(qǐng)求干掉。

再看看max_execution_time的注釋?zhuān)哼@設(shè)置了腳本被解析器中止之前允許的最大執(zhí)行時(shí)間,默認(rèn)是30s??礃幼?,我這個(gè)請(qǐng)求應(yīng)該是被max_execution_time這個(gè)設(shè)置干掉了。

好吧,不死心,做了一個(gè)實(shí)驗(yàn):

php-fpm request_terminate_timeout 設(shè)置 0 15
php.ini max_execution_time 設(shè)置 30 30
執(zhí)行結(jié)果 php有Fatal error超時(shí)日志,http狀態(tài)碼為500 php無(wú)Fatal error超時(shí)日志,http狀態(tài)碼為502,php-fpm日志中有殺掉子進(jìn)程日志

好吧,結(jié)論是web請(qǐng)求php執(zhí)行時(shí)間受到2方面控制,一個(gè)是php.ini的max_execution_time(要注意的是sleep,http請(qǐng)求等待響應(yīng)的時(shí)間是不算的,這里算的是真正的執(zhí)行時(shí)間),另一個(gè)是php-fpm request_terminate_timeout 設(shè)置,這個(gè)算的是請(qǐng)求開(kāi)始n秒。

request_terminate_timeout引起的資源問(wèn)題

request_terminate_timeout的值如果設(shè)置為0或者過(guò)長(zhǎng)的時(shí)間,可能會(huì)引起file_get_contents的資源問(wèn)題。
如果file_get_contents請(qǐng)求的遠(yuǎn)程資源如果反應(yīng)過(guò)慢,file_get_contents就會(huì)一直卡在那里不會(huì)超時(shí)。我們知道php.ini 里面max_execution_time 可以設(shè)置 PHP 腳本的最大執(zhí)行時(shí)間,但是,在 php-cgi(php-fpm) 中,該參數(shù)不會(huì)起效。

真正能夠控制 PHP 腳本最大執(zhí)行時(shí)間的是 php-fpm.conf 配置文件中的request_terminate_timeout參數(shù)。
request_terminate_timeout默認(rèn)值為 0 秒,也就是說(shuō),PHP 腳本會(huì)一直執(zhí)行下去。
這樣,當(dāng)所有的 php-cgi 進(jìn)程都卡在 file_get_contents() 函數(shù)時(shí),這臺(tái) Nginx+PHP 的 WebServer 已經(jīng)無(wú)法再處理新的 PHP 請(qǐng)求了,

Nginx 將給用戶(hù)返回“502 Bad Gateway”。修改該參數(shù),設(shè)置一個(gè) PHP 腳本最大執(zhí)行時(shí)間是必要的,
但是,治標(biāo)不治本。例如改成 30s,如果發(fā)生 file_get_contents() 獲取網(wǎng)頁(yè)內(nèi)容較慢的情況,這就意味著 150 個(gè) php-cgi 進(jìn)程,每秒鐘只能處理 5 個(gè)請(qǐng)求,WebServer 同樣很難避免”502 Bad Gateway”。

解決辦法是:request_terminate_timeout設(shè)置為10s或者一個(gè)合理的值,
或者給file_get_contents加一個(gè)超時(shí)參數(shù)。

$ctx = stream_context_create(array(
  'http' => array(
    'timeout' => 10  //設(shè)置一個(gè)超時(shí)時(shí)間,單位為秒
  )
));
 
file_get_contents($str, 0, $ctx);

php-fpm中的request_terminate_timeout最好不要設(shè)置

剛轉(zhuǎn)到php-fpm沒(méi)幾天就發(fā)現(xiàn),進(jìn)入我的joomla后臺(tái),firefox偶爾會(huì)給我白屏的那種http 503,這種情況僅出現(xiàn)在天翼云的服務(wù)器上,而我在國(guó)外的同樣配置的服務(wù)器一點(diǎn)問(wèn)題都沒(méi)有,后來(lái)發(fā)現(xiàn)是request_terminate_timeout的問(wèn)題。

每次登陸joomla后臺(tái),joomla都會(huì)去檢查是否有更新(檢查成功后cache,默認(rèn)保存該cache 6小時(shí)),而且分為joomla主程序和joomla擴(kuò)展兩個(gè)部分,如下圖:

不出意外的話,服務(wù)器會(huì)發(fā)起兩個(gè)php進(jìn)程,分別分配給兩個(gè)php-fpm children,去連接joomla的官方update服務(wù)器。好,問(wèn)題就來(lái)了,我的request_terminate_timeout = 30s,30秒不完成則超時(shí),參見(jiàn)天翼云主機(jī)的國(guó)際出口相當(dāng)?shù)疤郏](méi)錯(cuò),30秒內(nèi),天翼云主機(jī)根本無(wú)法完成連接joomla更新服務(wù)器并檢查是否有更新這整個(gè)過(guò)程。這也很好解釋了為什么同樣配置的國(guó)外服務(wù)器就沒(méi)有問(wèn)題,因?yàn)樗鼈兺瓿缮鲜龈?xì)過(guò)程僅需要在2~5秒左右。

我的apache超時(shí)設(shè)置是30秒,php.ini中最長(zhǎng)執(zhí)行時(shí)間野是30秒,多年來(lái)都沒(méi)有任何問(wèn)題,沒(méi)有30秒還打不開(kāi)的網(wǎng)頁(yè),所以我就沒(méi)多想給php-fpm的request_terminate_timeout = 30s。經(jīng)過(guò)這次的事情發(fā)現(xiàn)此30秒非鄙30秒啊……

php-fpm設(shè)置request_terminate_timeout后,php.ini中的max_execution_time和max_input_time都會(huì)失效,以php-fpm中的設(shè)置為準(zhǔn);
apache+mod_php在timeout后,只會(huì)在日志中記錄一下,僅此而已。php-fpm中的request_terminate_timeout超時(shí)之后,日志中記錄http 503的同時(shí),最要命的,它還會(huì)直接殺死造成這個(gè)http 503的php-fpm child,并生成新的child。
在我的joomla更新這個(gè)實(shí)例中,就會(huì)有兩個(gè)php-fpm children同時(shí)被殺死。而我的天翼云主機(jī)是低配,只有一個(gè)cpu核心,我也只啟動(dòng)了兩個(gè)php-fpm children,兩個(gè)同時(shí)死了,我的firefox這邊也就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的網(wǎng)站,每個(gè)連接都需要apache+php-fpm協(xié)同運(yùn)作。即便php-fpm中的request_terminate_timeout時(shí)間設(shè)置很長(zhǎng),apache中的timeout時(shí)間設(shè)置略短,只要apache的timeout到了,php-fpm照樣在后面殺進(jìn)程……
如果網(wǎng)站的訪問(wèn)者比較多,php-fpm的child是被許多訪問(wèn)者共用的,殺一個(gè)child,就有可能導(dǎo)致幾個(gè)用戶(hù)同時(shí)http 503 Service Unavailable。所以,我的建議是——php-fpm中的request_terminate_timeout最好不要設(shè)置,只給apache一個(gè)timeout就夠了。

相關(guān)文章

最新評(píng)論