天天看點

php.ini配置max_execution_time和FPM配置request_terminate_timeout

PHP限定腳本執行時長的方式有幾種,下面說下php.ini中的max_execution_time和php-fpm.conf中的request_terminate_timeout

1. php.ini中的max_execution_time

; Maximum execution time of each script, in seconds
; http://php.net/max-execution-time
; Note: This directive is hardcoded to  for the CLI SAPI
max_execution_time = 
           

上面是php.ini配置檔案中的max_execution_time及其說明,上面說了,這個值限定了腳本的最大執行時間(機關是秒)

set_time_limit()

函數和配置指令

max_execution_time

隻影響腳本本身執行的時間。任何發生在諸如使用system()的系統調用,流操作,資料庫操作等的腳本執行的最大時間不包括其中,當該腳本已運作。在測量時間是實值的Windows中,情況就不是如此了。

2. php-fpm.conf中的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: 
;request_terminate_timeout = 
request_terminate_timeout = s
           
設定單個請求的逾時中止時間。該選項可能會對 php.ini 設定中的

max_execution_time

因為某些特殊原因沒有中止運作的腳本有用。設定為 ‘0’ 表示 ‘Off’。可用機關:s(秒),m(分),h(小時)或者 d(天)。預設機關:s(秒)。預設值:0(關閉)。

通過上面兩個說明及實驗驗證得出結論,max_execution_time=1,不一定1s後就會中止腳本,可能是2s、3s甚至更長的時間;而request_terminate_timeout=4則就會在4s後中止腳本的執行。是以在配置逾時時間的時候,最好兩個都配置,max_execution_time時間短一點,而request_terminate_timeout時間長一點