使用 x-sendfile 通过 Nginx via Rails 2.3 提供大文件
假设我有一个由 nginx 前端并由 mongrel 提供服务的 Rails 2.3.2 应用程序,其中我需要通过 Rails 提供一个大型静态文件(以控制对其的访问)。 我希望 Rails 应用程序将文件传输委托给 nginx,以避免阻塞杂种实例。
现有信息似乎相互矛盾且不完整。 这篇文章展示了如何使用 Apache 进行此操作,以及暗示也可以使用 ngninx 来完成 - 但没有示例。 这篇文章和这篇文章展示了如何做到这一点使用显然 Rails 2.3 不再需要的插件。 这篇文章< /a> 表明 nginx 可能不支持 x-sendfile。
我不想用插件来处理 Rails 现在可以自己完成的事情。
有没有人在不使用插件和 Rails 2.3/nginx/mongrel 的情况下获得类似 x-sendfile 的行为? 如果没有,让它与插件(和/或monkeypatch)和Rails 2.3/nginx/mongrel一起工作的最佳文档是什么?
Let's say I have a Rails 2.3.2 application fronted by nginx and served by mongrel in which I need to serve a large static file through Rails (to control access to it). I want the Rails app to delegate the transfer of the file to nginx, to avoid blocking the mongrel instance.
The available information seems contradictory and incomplete. This post shows how to do it with Apache, and hints that it can also be done with ngninx - but no examples. This post and this post show how to do it using the a plugin that apparently Rails 2.3 makes uncessary. This post suggests that maybe there isn't support for x-sendfile with nginx after all.
I'd rather not muck around with plugins for things Rails can now do by itself.
Has anybody gotten x-sendfile-like behavior to work using no plugins and Rails 2.3/nginx/mongrel? If not, what's the best documentation for getting it to work with a plugin (and/or monkeypatch) and Rails 2.3/nginx/mongrel?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
主要思想:控制器所做的就是设置 nginx x-accel-redirect 标头。 一旦你的控制器方法返回(这将非常快),nginx 将查看你的 Rails 应用程序设置的标头。 如果设置了 x-accel-redirect,则 nginx 提供静态文件。
你的控制器看起来像这样:
仅凭这一点是不够的。 您还需要告诉 nginx 有关位于 $RAILS_ROOT/files 的文件的信息。 将其添加到服务器块内 nginx 配置的末尾:
将静态文件放入 $RAILS_ROOT/files 中,它应该可以工作。 无需插件或猴子补丁已使用 Rails 2.3.2 和 2.3.14 进行测试。
The main idea: all your controller does is to set the nginx x-accel-redirect header. Once your controller method returns (which will be very fast), nginx will look at the header your Rails app set. If x-accel-redirect is set, then nginx serves the static file.
Your controller will look something like:
This alone won't do the trick. You need to also tell nginx about the files located at $RAILS_ROOT/files. Add this to the end of your nginx config inside the server block:
Put the static file into $RAILS_ROOT/files and it should work. No need for plugins or monkeypatching Tested with Rails 2.3.2 and 2.3.14.