Search code examples
ruby-on-railssendfilex-accel-redirect

X-Accel-Redirect never set in rails 4 app when calling send_file


I'm working with a rails 4 app served with Puma and Nginx. In trying to directly serve certain files I've set

config.action_dispatch.x_sendfile_header = "X-Accel-Redirect" 

in config/environments/production.rb, and I can confirm the setting is there. But when I call send_file like this:

send_file(asset.asset.path(style), disposition: 'inline', type: asset.asset_content_type)

and check for the presence of the X-Accel-Redirect header, either by logging "response.headers.inspect" or examining the headers Puma is sending to Nginx by logging the socket traffic, it's never there.

Any ideas why X-Accel-Redirect header never gets set?


Solution

  • apparently the way it works is that nginx must first send an X-Accel-Mapping header to the app and then it responds with an X-Accel-Redirect header and the value you give send_file.

    So in my nginx config I put something like:

    location @ruby{
      ...
      proxy_set_header X-Accel-Mapping /app/current/private/=/private_files/;
    }
    
    location /private_files/ {
      internal;
      alias /app/current/private/;
    }
    

    and then the app started creating the X-Accel-Redirect header