代码之家  ›  专栏  ›  技术社区  ›  docwhat

如何更改rails中的公共资产目录?

  •  2
  • docwhat  · 技术社区  · 14 年前

    我的设置: 在端口1234上运行的精简 --prefix /foobar apache在端口80上运行 apache反向代理 /foobar 在端口1234上精简

    我希望静态资产不通过thin代理服务,而是在 /assets 而是直接通过apache。

    我必须使用相对路径,因为在启动之前我不知道rails应用程序的主机名/ip(它是app-in-a-box,应该可以移动)。

    我发现 config.action_controller.asset_host production.rb

    我怎样才能做到这一点?

    3 回复  |  直到 14 年前
        1
  •  1
  •   Geoff Lanotte    14 年前

    您不需要通过环境中的config块调用它,您可以从应用程序控制器调用它,这样您就可以访问request对象。所以你可以:

    class ApplicationController < ActionController::Base
      before_filter :set_asset_url
    
      def set_asset_url
        ActionController::Base.asset_host = "http://#{request.host}"
      end
    end
    

    感觉有点不舒服,但我知道没有更好的办法了。

    ActionController::Base.asset_host = "http#{request.ssl? ? 's' : ''}://#{request.host_with_port}"
    
        2
  •  1
  •   darkliquid    14 年前

    这在某种程度上取决于您的服务器环境,但基本上您需要与这里描述的内容类似的内容: http://blog.codahale.com/2006/06/19/time-for-a-grown-up-server-rails-mongrel-apache-capistrano-and-you/

        3
  •  0
  •   docwhat    14 年前

    笔记:

    • thin 与…一起跑 --prefix '/railsapp' 在端口9999上。
    • 这适用于windows和linux。W00T!
    • 我必须使用 LA-U (向前看)获取apache将使用的最终文件名。
    • 这个 IS_SUBREQ
    • 这个 /railsapp/index.html 重写是必需的,否则我的apacheconf中的另一个规则会将其重写为 /index.html ,这是默认的“here's what's here”页面; 404 不过,其他地方也有卖。

    以下是apache配置的相关部分:

    # Only proxy the thin stuff.
    <Proxy /railsapp/*>
        Order deny,allow
        Allow from all
    </Proxy>
    
    ## Add an alias for filename mapping.
    Alias /railsapp "/website/root/railsapp/public"
    
    ## We need the Rewrite Engine for this.
    RewriteEngine on
    <IfDefine debug>
        ## If debugging, turn on logging.
        RewriteLogLevel 9
        RewriteLog "/website/logs/http.rewrite.log"
    </IfDefine>
    
    ## Check for a static root page.
    RewriteRule ^/railsapp/?$ /railsapp/index.html [QSA]
    
    ## Check for Rails caching.
    RewriteRule ^(/railsapp/[^.]+)$ $1.html [QSA]
    
    ## Redirect all non-static requests to Rails.
    # Don't proxy on sub-requests (needed to make the LA-U work).
    RewriteCond %{IS_SUBREQ} false
    # Use look-ahead to see if the filename exists after all the rewrites.
    RewriteCond %{LA-U:REQUEST_FILENAME} !-f
    # Proxy it to Rails.
    RewriteRule ^/railsapp(.*)$  http://127.0.0.1:9999%{REQUEST_URI} [P,QSA,L]
    
    ## Make sure Rails requests are reversed correctly.
    ProxyPassReverse /railsapp http://127.0.0.1:9999/railsapp
    
    ## Disable keepalive; railsappd doesn't support them.
    SetEnv proxy-nokeepalive 1