0

I cannot download a zip file (using send_file) when using nginx as a proxy.

def download
  send_file file_path, filename: "file.zip"
end

Running without nginx (webrick only) it works fine but with the nginx proxy it opens this in the browser:enter image description here

Nginx proxy config:

server {
    server_name mydomain.com;

    location / {
        proxy_pass http://localhost:3001;
        proxy_set_header Host $host;
        auth_basic "Restricted";
        auth_basic_user_file /etc/nginx/keys/app.htpasswd;
    }
}

Edit:

Reloading the page results in the document being downloaded, so I looked at the request headers for each request. They should both be the same, but they are not.

Opened in browser

Failed

Successfully downloaded

Successful

The only difference is that the first one I clicked a link (opening link in new tab downloads and opens the file) and I refreshed the page on the second one.

Edit 2:

I tried it again with webrick and it is doing the same thing.

agustaf
  • 683
  • 1
  • 6
  • 19

2 Answers2

0

You should also send the file headers and make sure they are passed through via nginx. you can easily verify that by monitoring your browser's requests and responses in network tab

Miad Abrin
  • 952
  • 7
  • 14
0

Turns out it was turbolinks that was causing the problem. I had to add data: {turbolinks: false} to my link_to tag.

See also: Rails 4, asset pipeline causes user downloadable files to be downloaded twice, rails won't send_data as file, Ruby on Rails send_file, code in controller action running twice, Ruby on Rails send_file doesn't work until i refresh the page?

Community
  • 1
  • 1
agustaf
  • 683
  • 1
  • 6
  • 19