【问题标题】:Rails 3.1, Unicorn and Apache: static filesRails 3.1、Unicorn 和 Apache:静态文件
【发布时间】:2011-12-28 07:21:15
【问题描述】:

我安装了 Rails 3.1、Unicorn 和 Apache。我的 Apache 设置如下,production.rb 看起来像 this。我喜欢使用 h264 流,但由于 Rails 提供这些视频文件,Apache Mod 将无法工作。

DocumentRoot /blabla/current/public

RewriteEngine On
Options FollowSymLinks

<Proxy balancer://unicornservers>
  BalancerMember http://127.0.0.1:4000
</Proxy>

# Redirect all non-static requests to rails
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^/(.*)$ balancer://unicornservers%{REQUEST_URI} [P,QSA,L]

ProxyPass / balancer://unicornservers/
ProxyPassReverse / balancer://unicornservers/
ProxyPreserveHost on

<Proxy *>
 Order deny,allow
 Allow from all
</Proxy>

XSendFile On
XSendFileAllowAbove on

我必须启用 serve_static_assets 否则我无法下载任何静态内容。我也预编译了资产,但它不会有任何区别,因为公共目录中没有可用的文件,除非 Rails(我猜是 Rack)正在提供服务。

我应该使用 config.action_controller.asset_host 还是我的 Apache 配置有问题。

【问题讨论】:

    标签: ruby-on-rails apache unicorn static-files


    【解决方案1】:

    我有一个post 来解决这个问题(是的,它也发生在我身上),希望它会有所帮助。

    关键是要删除ProxyPass / balancer://unicornservers/ 模式,因为它会覆盖你的Rewrite Rule

    这是我的 apache 服务器配置。

    <VirtualHost *:80>
    
      ServerName example.org
      DocumentRoot /dir/of/your/project
    
      RewriteEngine On
    
      # Redirect all non-static requests to unicorn
      RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
      RewriteRule ^/(.*)$ balancer://unicornservers%{REQUEST_URI} [P,QSA,L]
    
      <Proxy balancer://unicornservers>
        BalancerMember http://127.0.0.1:2007
      </Proxy>
    
    </VirtualHost>
    

    【讨论】:

    • 这行得通,谢谢!顺便提一句。您的文章链接指向 localhost。
    • 完美,完全有道理,我为 rails 3 + apache + unicon/thin 找到的所有指南都有这个问题
    【解决方案2】:

    仅来自您的 production.rb 代码:

    # Specifies the header that your server uses for sending files
    # config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache
    # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx
    

    尝试取消注释带有“X-Sendfile”标头的行,重新启动 Unicorn 的池并重试。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-25
      • 2011-11-13
      • 1970-01-01
      • 2011-03-15
      • 2016-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多