【问题标题】:Capistrano & X-SendfileCapistrano 和 X-Sendfile
【发布时间】:2014-04-02 15:02:44
【问题描述】:

我正在尝试让 X-Sendfile 工作,以使用 capistrano 为我的繁重附件提供服务。我发现 X-Sendfile 不适用于符号链接。我如何处理 Capistrano 符号链接的文件夹中的文件?

我的网络服务器是 apache2 + 乘客

在我的production.rb中:

config.action_dispatch.x_sendfile_header = "X-Sendfile"

在我的控制器操作中

filename = File.join([Rails.root, "private/videos", @lesson.link_video1 + ".mp4"])
response.headers["X-Sendfile"]=  filename
send_file filename, :disposition => :inline, :stream => true, :x_sendfile => true
render nothing: true

我的文件系统结构(其中“->”代表“符号链接”,缩进表示子文件夹):

/var/www/myproject
  releases/
  ....
  current/ -> /var/www/myproject/releases/xxxxxxxxxxxx
    app/
    public/
    private/
      videos/ -> /home/ftp_user/videos

我的 apache 配置 XSendFile 开启 XSendFilePath / #也试过 /home/ftp_user/videos

我的应用程序能够处理小文件,但是对于大文件,它会给出 NoMemoryError(failed to allocate memory)

我认为它没有使用 x-sendfile,因为如果我不使用它,行为是相同的。

这是我尝试提供的文件的响应头

Accept-Ranges:bytes
Cache-Control:private
Connection:Keep-Alive
Content-Disposition:inline
Content-Range:bytes 0-1265/980720989
Content-Transfer-Encoding:binary
Content-Type:video/mp4
Date:Sat, 01 Mar 2014 13:24:19 GMT
ETag:"70b7da582d090774f6e42d4e44ae3ba5"
Keep-Alive:timeout=5, max=97
Server:Apache/2.4.6 (Ubuntu)
Status:200 OK
Transfer-Encoding:chunked
X-Content-Type-Options:nosniff
X-Frame-Options:SAMEORIGIN
X-Powered-By:Phusion Passenger 4.0.37
X-Request-Id:22ff0a30-c2fa-43fe-87c6-b9a5e7da12f2
X-Runtime:0.008150
X-UA-Compatible:chrome=1
X-XSS-Protection:1; mode=block

我真的不知道如何调试它,如果它是 x-sendfile 问题,或者我正在尝试为符号链接问题做一些不可能的事情

编辑: 按照接受的答案中的建议答案,它“神奇地”开始工作!

我以这种方式创建了一个 capistrano 任务:

 task :storage_links do
    on roles(:web), in: :sequence, wait: 2 do
      #creo i link simbolici alle risorse
      within "/var/www/my_application/current/private" do
        execute :ln, "-nFs", "/home/ftp_user/videos"
      end
    end
  end

在 finalize_update 之后我没有设法运行它,所以我在重新启动后手动运行它。

我以这种方式更正了我的 apache 配置:

XSendFilePath /var/www/my_application

(在我将 x-sendfile 指向 ftp 文件夹之前)

在我的响应标题中,现在 X-Sendfile 也没有出现,我得到了 206 - 部分内容,但一切似乎都正常工作,并且 apache 以正确的方式提供文件(也是非常重的文件)。

我知道这可能是一个安全问题,但我会尝试将其指向我的应用程序的最后一个版本,因为指向当前符号链接不起作用。

【问题讨论】:

  • 对你们的 Rails、Passenger、Apache 和 X-Sendfile 版本感到好奇。还有你的操作系统的类型和版本。我不得不转移到一个自定义的 Apache 服务器二进制文件,并且从来没有让 X-Sendfile 工作,和你的问题一样(不是内存部分,尽管它总是小文件),尽管我总是责怪二进制文件。
  • 我在 Ubuntu 13.10(64 位)上使用 Rails 4.0.0、Passenger 4.0.37、Apache 2.4.6。希望对你有帮助。

标签: ruby-on-rails apache ruby-on-rails-4 video-streaming x-sendfile


【解决方案1】:

也许我找到了解决方案。您是如何制作符号链接的?

也许你做了ln -s,但这还不够

Here 他们建议使用ln -nFs,所以他认出这是您要链接的目录

【讨论】:

  • 哇,这正在工作!!!我将编辑我的问题以解释我做了哪些更改!太感谢了!你拯救了我的一天!
  • 对我来说它不会改变任何东西。 debian 7 上的 Apache 2.2 xsendfile 0.12。除非我将 PassengerAppRoot 指向当前部署的应用程序版本的直接位置,否则 XSendFile 仍然不起作用,路径上没有符号链接。显然这与 Capistrano 的想法不一致。有什么想法吗?
  • 我终于成功了。见这里:*.com/questions/28859649/…