Rails 3, apache & passenger, send_file sends zero byte files

时间:2023-02-06 00:15:00

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

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

root~# a2enmod xsendfile
Module xsendfile already enabled

Its symlinked correctly in mods-enabled

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

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

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

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

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

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

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

1 个解决方案

#1


11  

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.

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

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).

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

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.):

我能够通过确保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 /

#1


11  

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.

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

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).

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

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.):

我能够通过确保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 /