Rails Rails4 实现子域名 (subdomain)

cn_boris · 2013年09月10日 · 最后由 karmue 回复于 2013年09月11日 · 5684 次阅读

之前有看到了 guanMac 的文章 http://my.eoe.cn/guanmac/archive/6105.html 但后来翻 guides 时候,发现其实有更 easy 的方法来实现。

3.9 Request-Based Constraints You can also constrain a route based on any method on the Request object that returns a String. You specify a request-based constraint the same way that you specify a segment constraint:

get 'photos', constraints: {subdomain: 'admin'}

You can also specify constraints in a block form:

namespace :admin do
  constraints subdomain: 'admin' do
    resources :photos
  end
end

Railscasts 有一期就是用的这个方法

constraints 里面都能用哪些变量呢,我看 guides 上面说是 request,但是看给的例子有 subdomain, 还有 id 这些

这样子和用 nginx 设置多个二级域名 host 加 unicorn 启动多个服务相比更快吗?我现在访问次级域名速度很慢 比如访问 xxx.com 很快 vvv.xxx.xom 这个二级域名就很慢 这个二级域名是单独一个 rails 网站 求解

I wish I have read this two month ago. I implemented the multi-tenants feature using postgresql schema, but it turned out be a very bad design and eventually switched to using scoping like the railscast did

需要 登录 后方可回复, 如果你还没有账号请 注册新账号