昨天,一個程序需要導出500條數據,結果發現到150條是,Nginx報出504 Gateway Timeout錯誤
經觀察,發現大約30秒時超時,php.ini中執行時間配置已經是300秒:
max_execution_time = 300
再查nginx的相關配置,無果。
寫了一個php的測試頁再測:
echo 'aaa';
set_time_limit(0);
sleep(40);
echo 'aa';
依然超時,可以確定set_time_limit這個函數沒生效。
再查php-fcgi的配置php-fpm.conf,下邊這個設置疑似有問題:
<value name="request_terminate_timeout">30s</value>
查官方文檔:http://php-fpm.org/wiki/Configuration_File
request_terminate_timeout - The timeout (in seconds) for serving a single request after which the worker process will be terminated. Should be used when 'max_execution_time' ini option does not stop script execution for some reason. Default: "5s". Note: '0s' means 'off'
大意是php中set_time_limit設置的時間內如果php還沒執行完,則走此處的配置,也就是request_terminate_timeout=30秒。
先把這個參數改的和php中set_time_limit值一樣,都是300秒,還不行,不理解為什么,如果高手知道請賜教。
最終把request_terminate_timeout關閉,程序可以正常執行了,問題解決:
<value name="request_terminate_timeout">0s</value>
補充:如果前端的nginx服務器使用了upstream負載均衡,那個負載均衡配置中以下幾個參數也需要相應修改:
proxy_connect_timeout 300s;
proxy_send_timeout 300s;
proxy_read_timeout 300s;