PHP-Fcgi下PHP的執行時間設定方法_php技巧

來源:互聯網
上載者:User

一般情況下設定PHP指令碼執行逾時的時間

一、在php.ini裡面設定

max_execution_time = 1800;
二、通過PHP的ini_set 函數設定

ini_set("max_execution_time", "1800");
三、通過set_time_limit 函數設定

set_time_limit(1800);

PHP-Fcgi下PHP的執行時間設定方法

昨天,一個程式需要匯出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;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.