X-Sendfile, Rails & Nginx

2.8k views Asked by At

I'm getting frustated about my problem with X-Sendfile,NGinx and Rails. I've read several docs and tutorials but I just don't get the point.

Whatever I'm trying, I'm getting a 404. Here's the X-Sendfile part of NGINX.

 location / {
                    proxy_set_header X-Sendfile-Type X-Accel-Redirect;
                    proxy_set_header X-Accel-Mapping /var/www/cube_storage/uploads/=/cdn;  #maps a real path to the internal location
                    ...
            } # end of location app

            location /cdn {
                    root /var/www/cube_storage/;
                    internal;
                    proxy_set_header X-Sendfile-Type X-Accel-Redirect;
                    proxy_set_header X-Accel-Mapping /var/www/cube_storage/=/cdn/;
                    }

X-Accel-Redirect is enabled in my rails app:

config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx

Here's the code of the controller which is responsible for serving the file:

def image
@activity = AccountActivity.where(:id => params[:id]).first

if !@activity || [email protected]_attachment_is_image? || @activity.file_attachment_name != requested_file_name()
  l = Logger.new("#{Rails.root}/logs/cdn_activity.log")
  l.info(params.inspect)

  render :file => "#{Rails.root}/public/404.html", :layout => false, :status => :not_found
else
  begin
    if !params[:version] || params[:version] == "original"
      t = @activity.file_attachment # thats the original image!
    else
      t = @activity.file_attachment.send(params[:version])
    end

    send_file t.current_path  ,:disposition => 'inline'
  rescue Exception => ex
    l = Logger.new("#{Rails.root}/logs/cdn_activity.log")
    l.info("--------------------------")
    l.info(Time.now.to_s)
    l.info("exception -> #{ex.message}")
    l.info("params: #{params.inspect}")
    render :file => "#{Rails.root}/public/404", :layout => false, :status => :not_found
  end

end

end

I use carrierwave as storage engine. Maybe anyone just sees my mistake, but I don't even after several hours of trying.

One more thing, the request doesn't even show up in the development.log (yes I activated X-Accel for dev too).

Regards, Alex

1

There are 1 answers

1
ghostrifle On

got it: never use any rails route part in your location directive of nginx. with this one, it works:

location / {
  proxy_set_header X-Sendfile-Type X-Accel-Redirect;
  # Maps a real path to the internal location
  proxy_set_header X-Accel-Mapping /var/www/cube_storage/uploads/=/downloads;  

} # end of location app

location /downloads {
  alias /var/www/cube_storage/uploads/;
  internal;
}