Rails + Nginx + activestorage + 大文件直接上传

Rails + Nginx + activestorage + directupload with large files

我无法上传大于 4.7gb 的文件,大于 4.7gb 的文件失败并显示 404 not found。 它在没有 nginx 的开发中工作得很好,所以我认为 nginx 是问题的根源。

Nginx 配置:

server {
upstream bench {
     server 127.0.0.1:3001;
}

  server_name servername;
  proxy_http_version 1.1;

  root /path/to/server;
  proxy_max_temp_file_size 10024m;
  client_body_in_file_only   on;
  client_body_buffer_size    1M;
  client_max_body_size 100G;
}

  location / {
    try_files $uri @bench;
  }

  location /cable {
    proxy_http_version 1.1;
    proxy_pass http://bench/cable;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "Upgrade";
  }

location @bench {
        proxy_set_header   X-Real-IP $remote_addr;
        proxy_set_header   Host      $http_host;
        proxy_http_version 1.1;
        proxy_set_header Connection "";
        proxy_pass http://bench;
        proxy_headers_hash_max_size 512;
        proxy_headers_hash_bucket_size 128;
        proxy_buffering off;
        proxy_request_buffering off;
}

  error_page 500 502 503 504 /500.html;
  keepalive_timeout 10;

routes.rb

Rails.application.routes.draw do
  resources :benches, :path => "benchmarks"
  root 'benches#index'
end

问题不在于 nginx。 service_url 当您开始上传时创建的 activestorage 默认仅在 5 分钟内有效,因此如果您的文件上传时间超过 5 分钟..那您就不走运了。

config.active_storage.service_urls_expire_in = 1.hour