【问题标题】:Nginx + PHP-FPM 7.1 - 504 Gateway Time-outNginx + PHP-FPM 7.1 - 504 网关超时
【发布时间】:2017-12-28 08:53:45
【问题描述】:

我在 Synology nas 上运行 nginx 1.12 和 php-fpm 7.1 作为单独的 docker 容器,如果 php-script 运行时间超过 60 秒,我会收到 504 Gateway 错误。我已经尝试了几个 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-testscript:

<?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"

有人有想法吗?

【问题讨论】:

  • 恕我直言 php:9000 无法从带有 nginx 的容器访问。

标签: php docker synology


【解决方案1】:

问题可能是为什么您的后端需要这么长时间才能响应?不确定您的用例,但通常等待很长时间才能得到响应并不便于用户使用。

回答您的问题:
我找到了这个链接: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

【讨论】:

  • 感谢您的回复!问题已解决:send_timeout 300 后 nginx 配置中缺少分号
猜你喜欢
  • 2019-01-20
  • 2018-02-25
  • 2021-05-27
  • 2016-11-28
  • 2014-08-19
  • 1970-01-01
  • 1970-01-01
  • 2011-08-30
  • 1970-01-01
相关资源
最近更新 更多