Search code examples
ruby-on-railsnginxrails-activestorage

Rails + Nginx + activestorage + directupload with large files


I'm not able to upload files larger than 4.7gb, larger than that fails with a 404 not found. It works just fine in development without nginx, so I assume nginx is the source of the problem here.

Nginx config:

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

Solution

  • The problem wasn't nginx. The service_url activestorage creates when you start the upload is only valid for 5 minutes by default, so if your files takes more than 5minutes to upload.. you're out of luck.

    config.active_storage.service_urls_expire_in = 1.hour