2015-08-09 30 views
0

我有一個Rails應用程序(http://example.org),其中多個租戶可以有一個簡單的CMS。他們得到一個前端,其位於http://example.org/frontend/clients/:client_name,相關的子路徑如/posts/media在Nginx反向代理後面更改應用程序的Rails鏈接路徑

現在我想允許租戶使用自定義域,因此應用程序將例如對http://example.com/posts的請求做出迴應,內容爲http://example.org/clients/example.com/posts

我設法寫一個Nginx的proxy_pass規則來獲得工作[見下文]。現在的問題是,在http://example.com/posts(例如frontend_client_media_path)上服務的相對Rails鏈接助手仍然指向Rails中定義的路徑,例如, http://example.com/clients/example.com/media

只要站點被自定義域訪問,是否有可能通過忽略/clients/example.com部分來告訴Rails以不同方式構建路徑?

附錄

Nginx的規則(它的肉)

server { 
    server_name _; # allow all domains 

    location/{ 
    proxy_pass http://upstream/frontend/clients/$host$request_uri; # proxy to client-specific subfolder 
    } 
} 

回答

1

您可以使用條件中,檢查主機路由,然後加載自定義路徑。

Constraints based on domain/host Or Complex Parsing/Redirecting

constraints(host: /^(?!.*example.org)/) do 
    # Routing 
end 

# http://foo.tld?x=y redirects to http://bar.tld?x=y 
constraints(:host => /foo.tld/) do 
    match '/(*path)' => redirect { |params, req| 
     query_params = req.params.except(:path) 
     "http://bar.tld/#{params[:path]}#{query_params.keys.any? ? "?" + query_params.to_query : ""}" 
    }, via: [:get, :post] 
end 

注意:如果你處理的全域而不是僅僅子域,使用:域名,而不是:主機。

You can also include other logic to fine tune it for ex:

你可以使用CONTROLLER_NAME或controller路徑:

<% if controller_name.match(/^posts/) %> 
    # Routing 
<% end %> 

<% if controller_path.match(/^posts/i) %> 
    # Routing 
<% end %> 
相關問題