10

I have a download link that goes to a method in a controller which uses send_file so that I may rename the file (it is an MP3 with a uuid as a filename). After clicking on the link I see the request in the NGINX logs and Rails logs, however it takes up to 90 seconds before the download beings. I have tried various settings with proxy_buffers and client_*_buffers with no affect. I have an HTML5 audio player that uses the real URL for the file and it streams the file right away with no delay.

My NGINX config:

upstream app {
  server unix:/home/archives/app/tmp/unicorn.sock fail_timeout=0;
}

server {
  listen      80 default deferred;
  server_name archives.example.com;
  root        /home/archives/app/public/;

  client_max_body_size 200M;
  client_body_buffer_size 100M;
  proxy_buffers 2 100M;
  proxy_buffer_size 100M;
  proxy_busy_buffers_size 100M;

  try_files /maintenance.html $uri/index.html $uri.html $uri @production;

  location @production {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-Sendfile-Type X-Accel-Redirect;
    proxy_set_header X-Accel-Mapping /home/archives/app/public/uploads/audio/=/uploads/audio/;
    proxy_redirect off;
    proxy_pass http://app;
  }

  location ~ "^/assets/*" {
    gzip_static on;
    expires     max;
    add_header  Cache-Control public;
  }

  location ~ (?:/\..*|~)$ {
    access_log off;
    log_not_found off;
    deny all;
  }

  error_page 500 502 503 504 /500.html;
  location = /500.html {
    root /home/archives/app/public;
  }
}

Rails controller:

def download
  send_file @audio.path, type: @audio_content_type, filename: "#{@audio.title} - #{@audio.speaker.name}"
end
2
  • do you have config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for NGINX commented out in your environment configuration?
    – hamitron
    Oct 12, 2015 at 18:57
  • If you move your mp3 in the public folder you can access it directly via the browser, just for testing performance (bypassing controller logic). If its still slow its likely top be a NGINX config issue. Also try running your app with webbrick (rails s -b 0.0.0.0) see if that makes any difference?
    – Roger
    Dec 6, 2016 at 8:43

2 Answers 2

0

Maybe it is slow because you have set an overly large proxy buffer? 100M proxy buffer means that your server will download 100M from origin data before starting to send it to the destination. Default is 32kB and something like 512kB would be already a nice number.

0

After testing I found out it was turbolinks causing the issue. It was doing a XHR request in the background, downloading the file first then allowing the browser to actually download the file. After adding 'data-no-turbolink'='true' to my link, do files download instantly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.