2010-07-09 63 views
2

我的设置: 在1234端口上薄运行在端口--prefix /foobar 运行Apache 80个 apache的反向代理/foobar薄端口1234上如何更改目录公共资产是从轨道服务?

我想静态资产不通过代理薄送达,但而是直接通过apache服务于/assets

我必须使用相对路径,因为在启动之前我不知道rails应用程序的主机名/ ip(它是应该能够移动的应用程序框)。

我发现config.action_controller.asset_hostproduction.rb,但我不能将它设置为相对路径。当我这样做的时候会感到困惑并创建真正的虚假网址。

我该如何做这项工作?

回答

0

首先,我要感谢Geoff和darkliquid。我采取了darkliquid的链接,并努力使其适用于我的案件。最大的挑战是我没有在Web服务器的根目录下提供Rails应用程序。

注:

  • thin--prefix '/railsapp'在端口9999
  • 这适用于Windows和Linux上运行。 W00T!
  • 我必须使用LA-U(预见)才能得到apache使用的最终文件名。
  • IS_SUBREQ检查是为了防止从前返回代理的前瞻(一个子请求)。
  • /railsapp/index.html重写是必需的,否则我的apache conf中的另一个规则会将其重写为/index.html,这是默认的“这是这里的内容”页面;尽管如此,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 
1

您不需要通过环境中的配置块调用它,您可以从应用程序控制器调用它,从而使您可以访问请求对象。所以你可以这样做:

class ApplicationController < ActionController::Base 
    before_filter :set_asset_url 

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

这感觉有点骇人,但我知道没有更好的办法。

,如果你需要担心SSL和不同的端口,你可以去疯狂:

ActionController::Base.asset_host = "http#{request.ssl? ? 's' : ''}://#{request.host_with_port}" 
+0

感谢。这绝对是另一种方式。我希望Rails有办法在本地执行此操作,但是apache解决方案也非常好。 – 2010-07-12 16:28:08

+0

我说它感觉很ha,,我想你是走对了路。 – 2010-07-12 16:38:25