[英]Nginx + PHP-FPM 7.1 - 504 Gateway Time-out
我在 Synology nas 上運行 nginx 1.12 和 php-fpm 7.1 作為單獨的 docker 容器,如果 php 腳本運行時間超過 60 秒,我會收到 504 網關錯誤。 我已經嘗試了幾個 nginx 配置參數,但錯誤仍然存在。
這是我的實際 nginx 配置:
#user www-data;
#group http
worker_processes 1;
error_log /opt/data/logs/nginx_error.log notice;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#keepalive_timeout 30s;
sendfile on;
#tcp_nopush off;
tcp_nodelay on;
#gzip off;
send_timeout 300
server {
listen 80;
server_name "";
root /opt/php;
index index.php;
location /data/ {
sendfile on;
root /opt;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
if (!-f $document_root$fastcgi_script_name) {
return 404;
}
# Mitigate https://httpoxy.org/ vulnerabilities
fastcgi_param HTTP_PROXY "";
fastcgi_pass php:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_read_timeout 300;
#fastcgi_buffering off;
#fastcgi_keep_conn on;
#fastcgi_intercept_errors on;
#fastcgi_cache off;
#fastcgi_ignore_client_abort on;
}
location ~ ^/(status|ping)$ {
access_log off;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass php:9000;
}
}
}
php測試腳本:
<?php
sleep(65);
echo "done!";
file_put_contents("/opt/data/timetest.txt", "\nEnd", FILE_APPEND);
60 秒后,瀏覽器顯示 504 網關超時。 php-script 仍在運行,並將文本寫入文件。
Nginx 錯誤日志:
2017/07/22 08:16:32 [error] 8#8: *10 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 172.17.0.1, server: , request: "GET /timetest.php HTTP/1.1", upstream: "fastcgi://172.17.0.3:9000", host: "192.168.0.100:8081"
有人有想法嗎?
問題可能是為什么您的后端需要這么長時間才能響應? 不確定您的用例,但通常等待很長時間才能獲得響應對用戶不友好。
回答你的問題:
我找到了這個鏈接: https : //easyengine.io/tutorials/php/increase-script-execution-time/
加入/etc/php5/fpm/php.ini
max_execution_time = 300
在 /etc/php5/fpm/pool.d/www.conf 中設置
request_terminate_timeout = 300
在 /etc/nginx/nginx.conf 中設置
http { #... fastcgi_read_timeout 300; #... }
在你的配置中:
location ~ \\.php$ { include /etc/nginx/fastcgi_params; fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_read_timeout 300; }
並重新加載服務
service php5-fpm reload service nginx reload
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.