send_file具有在Rails 3中
:x_sendfile默认使用的参数
true。此功能通过返回带有路径的X-
Sendfile标头的空响应,将流下载卸载到前端服务器-Apache(带有mod_xsendfile)或lighttpd。
Nginx使用
X-Accel-Redirect标头来实现相同的功能,但是您必须在正确的环境文件中正确配置Rails:
config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect'
Rails 3更新 :该行已存在于中
production.rb,只需取消注释即可。
添加
sendfile on;到您的nginx配置中,以利用Rails发送的标头。请记住,必须使用绝对路径,并且nginx必须具有对文件的读取权限。
别名文件的另一种方法:
为了提高安全性,我在nginx中使用别名而不是绝对路径,但是
send_file方法会检查是否存在以别名失败的文件。因此,我将操作更改为:
head( 'X-Accel-Redirect'=> file_item.location, 'Content-Type' => file_item.content_type, 'Content-Disposition' => "attachment; filename="#{file_item.name}""); render :nothing => true;


