現象:
PHP查詢數據庫較慢,大約 60s 后 nginx 返回 504:Sorry, the page you are looking for is currently unavailable.
檢查log:
從 /etc/nginx/nginx.conf 找到 /var/log/nginx/access.log 和 /var/log/nginx/error.log
log 顯示 upstream timed out (110: Connection timed out) while reading response header from upstream
解決辦法:
Nginx upstream timed out (why and how to fix)
添加帶有注釋的語句
location ~ \.php$ { # root html; fastcgi_read_timeout 300; #########https://distinctplace.com/2017/04/22/nginx-upstream-timed-out/ upstream timed out (110: Connection timed out) while reading response header from upstream fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; }
執行 service nginx reload 之后,執行查詢,返回頁面沒有顯示 table,繼續檢查log
"PHP message: PHP Warning: file_get_contents(http://127.0.0.1/xxx): failed to open stream: HTTP request failed! in /home/www/uninum/web.php on line 50
PHP message: PHP Notice: Trying to get property of non-object in xxx.php on line 54" while reading response header from upstream
同步設置 file_get_contents 超時
php file_get_contents 獲取文件超時的處理方法 以及post 和 多次重試
$opts = array( 'http'=>array( 'method'=>"GET", 'timeout'=>300, ) ); $context = stream_context_create($opts); $html =file_get_contents('http://www.example.com', false, $context);