Rails 3,apache和passenger,send_file发送零字节文件

Rob*_*Rob 13 ruby-on-rails passenger ruby-on-rails-3

我正在努力使用send_filerails 3.0.9在ubuntu lucid上运行ruby 1.9,apache上的乘客3.0.8 xsendfile模块已安装并加载到apache

root~# a2enmod xsendfile
Module xsendfile already enabled
Run Code Online (Sandbox Code Playgroud)

它在启用mods时正确符号链接

lrwxrwxrwx 1 root root   32 Aug  8 11:20 xsendfile.load -> ../mods-available/xsendfile.load
Run Code Online (Sandbox Code Playgroud)

config.action_dispatch.x_sendfile_header = "X-Sendfile" 在我的production.rb中设置

使用send_file会导致将零字节文件发送到浏览器

filepath = Rails.root.join('export',"#{filename}.csv")
if File.exists?(filepath)
  send_file filepath, :type => 'text/csv'
end
Run Code Online (Sandbox Code Playgroud)

Sea*_*ara 11

我相信前面的答案不是正确的方法,因为据我所知,当应用此解决方案时,Apache根本不处理下载,而是rails进程.这就是为什么nginx指令看起来不行的原因.通过注释掉config指令得到相同的结果.

另一个缺点(除了将轨道进程占用太长时间)是当数据流由rails进程处理时,响应似乎不发送内容长度头.因此,用户不知道他们下载的文件有多大,也不知道需要多长时间(可用性问题).

我能够通过确保mod_sendfile被正确包含并加载到我的apache配置中来实现它,就像这样(这将取决于你的apache安装等):

LoadModule xsendfile_module   /usr/lib64/httpd/modules/mod_xsendfile.so
...

# enable mod_x_sendfile for offloading zip file downloads from rails 
XSendFile on 
XSendFilePath /
Run Code Online (Sandbox Code Playgroud)