现象:
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);