Rails 3, apache &乘客,send_file 发送零字节文件 [英] Rails 3, apache & passenger, send_file sends zero byte files

查看:42
本文介绍了Rails 3, apache &乘客,send_file 发送零字节文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 send_file 苦苦挣扎,rails 3.0.9 运行 ruby​​ 1.9,乘客 3.0.8 在 ubuntu lucid 上运行 apachexsendfile模块安装并加载到apache

I'm struggling with send_file with rails 3.0.9 running ruby 1.9, passenger 3.0.8 on apache on ubuntu lucid The xsendfile module is installed and loaded into apache

root~# a2enmod xsendfile
Module xsendfile already enabled

它在启用 mods 的情况下正确符号链接

Its symlinked correctly in mods-enabled

lrwxrwxrwx 1 root root   32 Aug  8 11:20 xsendfile.load -> ../mods-available/xsendfile.load

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

config.action_dispatch.x_sendfile_header = "X-Sendfile" is set in my production.rb

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

using send_file results in zero byte files being sent to the browser

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

推荐答案

我相信之前的答案不是正确的方法,因为据我所知,Apache 根本不处理下载应用了解决方案,而不是 Rails 过程.这就是为什么不应该工作的 nginx 指令出现的原因.通过注释掉 config 指令可以获得相同的结果.

I believe the previous answer isn't the right way to go because, as far as I can tell, Apache isn't handling the downloads at all when this solution is applied, instead the rails process is. That's why the nginx directive, which shouldn't work, appears to. You get the same result by commenting out the config directive.

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

Another drawback (aside from tying up a rails process for too long) is that when the data streaming is handled by the rails process the response doesn't seem to send the content length header. So a user doesn't know how large the file they're downloading is, nor how long it will take (a usability problem).

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

I was able to get it to work by ensuring that mod_sendfile was properly included and loaded in my apache config, like so (this will be dependent on your apache install, etc.):

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

# enable mod_x_sendfile for offloading zip file downloads from rails 
XSendFile on 
XSendFilePath /

这篇关于Rails 3, apache &乘客,send_file 发送零字节文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆