Content-Length header missing from Nginx-supported Rails application

I have a rails application that serves large static files for registered users. I was able to implement it by following the excellent tutorial here: Secure downloads using nginx, Rails 3.0 and #send_file . Downloading and everything else works fine, but there is only this problem - the header is Content-Lengthnot sent.

No Content Length Header

This is normal for small files, but downloading large files becomes very unpleasant, as download managers and browsers show no progress. How can i fix this? Should I add something to my configuration nginxor do I need to pass another function to a method send_filein my rails controller? I searched the Internet for quite some time, but to no avail. Please help! Thank!

Here's mine nginx.conf:

upstream unicorn {
  server unix:/tmp/unicorn.awesomeapp.sock fail_timeout=0;
}

server {
  listen 80 default_server deferred;
  # server_name example.com;
  root /home/deploy/apps/awesomeapp/current/public;

  location ~ /downloads/(.*) {
    internal;
    alias /home/deploy/uploads/$1;
  }

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

  try_files $uri/index.html $uri @unicorn;
  location @unicorn {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;

    proxy_set_header X-Sendfile-Type X-Accel-Redirect;
    proxy_set_header X-Accel-Mapping /downloads/=/home/deploy/uploads/;

    proxy_pass http://unicorn;
  }

  error_page 500 502 503 504 /500.html;
  client_max_body_size 20M;
  keepalive_timeout 10;
}
+3
source share
1 answer

Ok, here's something. I don't know if this is correct or not, but I was able to fix the problem by manually sending the header Content-Lengthfrom my Rails Controller. That's what I'm doing:

def download
    @file = Attachment.find(params[:id])

    response.headers['Content-Length'] = @file.size.to_s
    send_file(@file.path, x_sendfile: true)
end

nginx . , -, ; "" , , .

P.S: -, .to_s

+13

All Articles