fiv*_*six 23 linux ruby-on-rails nginx
我无法使send_file(Model.attachment.path)工作.它不会失败,相反,它会向客户端发送一个0字节大小的文件,但文件名是正确的.
在我从Rails 2.3.8迁移到3之后,这个问题开始发生了.
此次迁移中还发生了很多其他事情,我会尽力详细说明所有这些事情.
我通过ftp移动附件,因为它们不是我的git存储库的一部分,因此它们通过cap deploy发布,而不是手动ftp远程(RHEL5)到本地(Win7)然后本地(Win7)到远程(Ubuntu10).
我知道FTPing不会通过传输保留文件权限,所以我所做的就是模仿我之前服务器上看到的chmod,因此它们几乎相同.(用户/组不同,设置为root:root而不是olduser:olduser).
从生产日志下载附件的请求的片段.
Started GET "/attachments/replies/1410?1277105698" for 218.102.140.205 at 2010-09-16 09:44:31 +0000
Processing by AttachmentsController#replies as HTML
Parameters: {"1277105698"=>nil, "id"=>"1410"}
Sent file /srv/app/releases/20100916094249/attachments/replies/UE0003-Requisition_For_Compensation_Leave.doc (0.2ms)
Completed 200 OK in 78ms
Run Code Online (Sandbox Code Playgroud)
一切都还好.我还要排除本地问题,我尝试在Win7和Ubuntu(在Vbox上)通过Chrome下载.
我还要向你保证,这条道路确实是正确的.
root@li162-41:/srv/app/current# tail /srv/app/releases/20100916094249/attachments/replies/UE0003-Requisition_For_Compensation_Leave.doc
#
#
%17nw
HQ??+1ae????
%33333333333(??QR???HX?"%%??@9
??@?p4??#P@??Unknown????????????G??z ?Times New Roman5??Symbol3&?
?z ?Arial5&?
Run Code Online (Sandbox Code Playgroud)
总结一下这个问题,如何让send_file实际发送文件而不是假的0字节垃圾.
ger*_*tas 45
send_file具有:x_sendfilePARAM默认为true在导轨3此功能卸载流下载到前服务器-阿帕奇(与mod_xsendfile)或lighttpd的,通过返回用X-SENDFILE头与路径空响应.
Nginx使用X-Accel-Redirect标头来实现相同的功能,但您必须在适当的环境文件中正确配置Rails:
config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect'
Run Code Online (Sandbox Code Playgroud)
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;
Run Code Online (Sandbox Code Playgroud)
ash*_*ai_ 18
在Rails 3,只是取消对线config.action_dispatch.x_sendfile_header ="X-阿塞尔-重定向"在production.rb内部环境中的文件夹.
小智 14
是的,我在Rails 3中默认启用了X-sendfile也遇到了同样的问题.
如果你有大量的"send_file"调用,你可以在config/environments/production.rb中注释掉以下行:
#config.action_dispatch.x_sendfile_header = "X-Sendfile"
Run Code Online (Sandbox Code Playgroud)
然后send_file方法开始工作完美.
因为我无法在Apache上安装x-sendfile扩展,所以我只是搜索了一下,发现了这一点.
我希望它有所帮助.
| 归档时间: |
|
| 查看次数: |
11876 次 |
| 最近记录: |