X-Accel-Redirect 调用 send_file 时从不在 rails 4 应用程序中设置

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

我正在使用 Puma 和 Nginx 提供的 rails 4 应用程序。在尝试直接提供某些文件时,我设置了

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

in config/environments/production.rb,我可以确认设置在那里。但是当我这样调用 send_file 时:

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

并检查 X-Accel-Redirect header 是否存在,通过记录 "response.headers.inspect" 或检查 headers Puma 通过记录套接字流量发送到 Nginx ,它永远不存在。

知道为什么 X-Accel-Redirect header 永远无法设置吗?

显然它的工作方式是 nginx 必须首先向应用程序发送一个 X-Accel-Mapping header 然后它用一个 X-Accel-Redirect header 和你的值来响应给send_file。

所以在我的 nginx 配置中我放了类似的东西:

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

location /private_files/ {
  internal;
  alias /app/current/private/;
}

然后应用开始创建 X-Accel-Redirect header