upstream timed out (110: Connection timed out) while reading response header from upstream。/kd/open.php 再次觸發則全站掛掉。netstat -napo | grep php-fpm | wc -l 發現 worker 進程數達上限(10),推測全被該腳本卡住。/kd/open.php 功能:抓取快遞資訊,使用 php_curl。php.ini 有 max_execution_time = 30,但腳本仍卡死。max_execution_time 不包含 系統呼叫、流操作、資料庫查詢、cURL 等外部等待時間。$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); // 連線階段超時 10 秒
curl_setopt($ch, CURLOPT_TIMEOUT, 10); // 整個請求超時 10 秒
$response = curl_exec($ch);
curl_close($ch);
編輯 www.conf:
request_terminate_timeout = 30s ; 請求超過 30 秒強制 kill worker
request_slowlog_timeout = 10s ; 慢請求日誌
slowlog = /var/log/php-fpm/slow.log
重載 PHP-FPM 生效。
$limit = 60;
if (pcntl_fork()) {
// 子進程執行耗時任務
} else {
sleep($limit);
posix_kill(posix_getppid(), SIGKILL); // 父進程超時自殺
}
fastcgi_read_timeout、PHP-FPM request_terminate_timeout 形成多層防護。