简体   繁体   English

Rails + Heroku + Cloudflare - 消除www作为子域

[英]Rails + Heroku + Cloudflare - Eliminate www as a subdomain

I have a multi-tenant Rails 4 app that uses subdomains with PostgreSQL schemas to 'tenant' the application. 我有一个多租户Rails 4应用程序,它使用带有PostgreSQL模式的子域来“租用”应用程序。 I have the subdomain wildcards "*" set up in Heroku and Cloudflare and the site loads, but it keeps "thinking" that the www is a valid subdomain. 我在Heroku和Cloudflare中设置了子域通配符“*”并加载了网站,但它仍然“认为”www是一个有效的子域。 Because of this, normally valid links (that work in development) do not work in production because www.mysite.com/accounts/new etc. is the same as asdf.mysite.com/accounts/new. 因此,通常有效的链接(在开发中工作)在生产中不起作用,因为www.mysite.com/accounts/new等与asdf.mysite.com/accounts/new相同。 Is there any way I can get my app to pretty much ignore www as a subdomain? 有什么方法可以让我的应用程序几乎忽略www作为子域? I'd still like mysite.com to re-direct to www.mysite.com, but pretty much just set the subdomain to false when it is www? 我仍然希望mysite.com重新定向到www.mysite.com,但是当它是www时,只是将子域设置为false?

To make things a little more concrete, in my application_controller, I have some code that does the following. 为了使事情更具体一点,在我的application_controller中,我有一些代码可以执行以下操作。 Notice the comments. 注意评论。 In development mode, the redirect_to root_url(subdomain: false) works, but in production, it just infinitely re-directs and will not load. 在开发模式下,redirect_to root_url(subdomain:false)可以工作,但在生产中,它只是无限重定向并且不会加载。 To get it to load, I simply render the page, but doing so never sets the subdomain to false. 为了加载它,我只是渲染页面,但这样做永远不会将子域设置为false。

  def load_schema
    Apartment::Tenant.switch!('public')
    return unless request.subdomain.present?
    if current_account
      Apartment::Tenant.switch!(current_account.subdomain)
    else
      redirect_to root_url(subdomain: false) # Works locally, but infinite re-direct in production
      #render 'welcome/index' # - Renders the page in production, but nothing else works.
    end
  end

Basically, everything works as expected in development mode, but in production, there appear to be some DNS issues that need ironed out, but I can't seem to figure out what to do. 基本上,一切都在开发模式中按预期工作,但在生产中,似乎有一些DNS问题需要解决,但我似乎无法弄清楚该怎么做。 Any thoughts? 有什么想法吗?

I had to remove the A name record in CloudFlare and replace it with a CNAME record. 我不得不删除CloudFlare中的A名称记录并将其替换为CNAME记录。 I also added 2 additional CNAME records: one wildcard and one for www. 我还添加了2个额外的CNAME记录:一个通配符和一个用于www。

My suggestion is to force www.mysite.com to redirect to mysite.com where you probably have the main site. 我的建议是强迫www.mysite.com重定向到你可能拥有主站点的mysite.com。

You can either accomplish it using pointing the hostname to a redirect service, or using a specific route with constraint in the routes.rb file. 您可以使用将主机名指向重定向服务,或使用routes.rb文件中具有约束的特定路由来完成它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM