首页 > 解决方案 > 使用 Docker Nginx 长时间上传时出现 504

问题描述

我花了这么多时间试图解决这个问题,这太荒谬了......

我需要上传最大 15Gb 的文件。

nginx.conf:

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    sendfile        on;

    keepalive_timeout  800s;

    include /etc/nginx/conf.d/*.conf;
}

自定义 nginx 配置

server {
    listen   80;
    listen   [::]:80;
    server_name example.com;

    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;

    if ($http_x_forwarded_proto != "https") {
        rewrite ^(.*)$ https://$server_name$REQUEST_URI permanent;
    }

    root   /var/www/webroot;
    index  index.php;

    client_body_timeout 800s;
    client_header_timeout 800s;
    client_max_body_size 15000m;
    client_body_temp_path /store/nginx-tmp 1 2;
    #fastcgi_buffers 8 1600k;
    #fastcgi_buffer_size 3200k;
    add_header X-Frame-Options sameorigin;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        try_files $uri =404;
        include fastcgi_params;
        fastcgi_pass web:9000;
        fastcgi_index index.php;
        fastcgi_intercept_errors on;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_connect_timeout 800s;
        fastcgi_read_timeout 800s;
        fastcgi_send_timeout 800s;
        #proxy_connect_timeout 800s;
        #proxy_send_timeout 800s;
        #proxy_read_timeout 800s;
        #send_timeout 800s;
    }
}

注释行是我尝试过的其他配置。我client_body_temp_path /store/nginx-tmp 1 2;为 Nginx 添加了在需要时使用挂载的卷来存储临时文件,因为 EC2 实例只有一个 8Gb 磁盘。

相关PHP配置:

upload_max_filesize = 15000M
post_max_size = 15000M
max_execution_time = 0
request_terminate_timeout = 800s

我正在运行一个 CakePHP 应用程序,以防有助于破解这种情况。

当我上传一个大文件时,我会在 2 到 2.5 分钟后收到 504。上传不完整 ($_FILES['file']['error'] = 3, UPLOAD_ERR_PARTIAL)

PHP 日志中没有任何内容。Nginx 日志只有这个:

2020/04/17 06:38:04 [warn] 479#479: *943 a client request body is buffered to a temporary file /var/cache/nginx/client_temp/0000000007, client: 172.31.20.155, server: example.com, request: "POST /materials/add/38999 HTTP/1.1", host: "example.com", referrer: "https://example.com/jobs/view/38999"

谁能在这里拯救我的理智?

谢谢,

标签: dockernginx

解决方案


推荐阅读