2015-08-09 75 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 %> 
相关问题