通过 nginx X-Accel 和 rails send_file 安全地提供静态内容的问题

通过 nginx X-Accel 和 rails send_file 安全地提供静态内容的问题

我正在尝试使用 nginx 和 rails 提供静态内容X-Accel-Redirect。 我的实际静态内容目录位于 rails 根文件夹中,如下所示rails_root\books。 这是我的 nginx 配置文件,将 url 与实际文件位置进行映射

server {
        listen       3000;
        server_name  127.0.0.1;
        passenger_enabled on;
        root    /home/voodoo/work/reader/public;

        location ~ /read/*./.* {
          internal;
          alias /home/voodoo/work/reader/books/sources/$1/$2;
        }

        location / {
          proxy_redirect    off;

          proxy_set_header  Host              $http_host;
          proxy_set_header  X-Real-IP         $remote_addr;
          proxy_set_header  X-Forwarded-For   $proxy_add_x_forwarded_for;

          passenger_pass_header X-Accel-Redirect;
          passenger_set_cgi_param  HTTP_X_ACCEL_MAPPING   /read/=/home/voodoo/work/reader/books/sources/;

          proxy_pass http://127.0.0.1:3001/;
        }

    }

以及相应的轨道控制器

 class ReaderController < ApplicationController
        def resource
           send_file "#{Rails.root}/books/sources/"+ params[:id] + "/" + params[:resource] + "." + params[:format]
        end
 end

nginx成功触发 controller X-Accel-redirect,conroller 返回数据,这是日志

Started GET "/reader/229/OPS/cover.xml" for 127.0.0.1 at 2013-01-13 00:17:10 +0530
Processing by ReaderController#resource as XML
  Parameters: {"id"=>"229", "resource"=>"OPS/cover"}
Client Ip Address 127.0.0.1
Sent file /home/voodoo/work/reader/books/sources/229/OPS/cover.xml (0.2ms)
Completed 200 OK in 7ms (ActiveRecord: 0.5ms)
cache: [GET /read/229/OPS/cover.xml] miss

但下一个日志完全不同,请求就在那里停止

Started GET "/home/voodoo/work/reader/books/sources/229/OPS/cover.xml" for 127.0.0.1 at 2013-01-13 00:17:10 +0530

ActionController::RoutingError (No route matches [GET] "/home/voodoo/work/reader/books/sources/229/OPS/cover.xml"):
  actionpack (3.2.2) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  actionpack (3.2.2) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
  railties (3.2.2) lib/rails/rack/logger.rb:26:in `call_app'
  railties (3.2.2) lib/rails/rack/logger.rb:16:in `call'
  actionpack (3.2.2) lib/action_dispatch/middleware/request_id.rb:22:in `call'

它会抛出一个错误RoutingError(没有路由匹配[GET](实际文件路径)经过一番挖掘,我发现,这个错误可能是因为 config.serve_static_assets = falseproduction.rb 中存在错误。但在我的配置中确实存在错误。

我已经测试过在独立模式下和使用 nginx 更改乘客。我总是得到相同的错误。我完全被迷住了。有人能帮我吗?

相关内容