Heroku,自定义子域和自定义约束

Heroku, custom subdomain and a custom constraint

根据我对其他问题的阅读,我觉得这应该是有效的,但到目前为止我发现还没有这样做。我有一个应用程序可以让俱乐部运动队托管一个网站(理论上)。使用 lvh.me 时,在我的本地机器上一切正常;但是部署到 heroku 会破坏路由。

Ruby 在 rails 5.1

到目前为止我已采取的步骤:

  1. 将子域添加到 heroku 域。对于 heroku 应用程序,我现在将根域作为 ALIAS 或 ANAME,将 www 作为 cname,将新添加的子域 (pincity) 作为 cname。

  2. 将 cname 添加到我的 dns 提供商。使用 dig returns 子域返回正确的 {crazy-heroku-name}.herokudns.com 地址

  3. 部署并将该团队添加到生产应用程序数据库后('pincity' 的一部分,以便 pincity.mydomain.com 工作),我重新启动了测功机。

我认为我需要做的就是这些。

我的路由文件相当简单

class TeamWebsiteConstraint
  def matches?(request)
    Rails.logger.info "subdomain is #{request.subdomain}"
    Team.where(slug: request.subdomain).any?
  end
end

Rails.application.routes.draw do
  # all other routes

  # Club team custom websites
  constraints TeamWebsiteConstraint.new do
    root 'team_website#home', as: :team_website_root
    get 'about', to: 'team_website#about', as: :team_website_about
    get 'schedule', to: 'team_website#schedule', as: :team_website_schedule
    get 'faqs', to: 'team_website#resources', as: :team_website_resources
    get 'contact', to: 'team_website#contact', as: :team_website_contact
  end

  root 'marketing#home'
end

现在,当转到 heroku 时,子域将我重定向到根域。并输入 pincity.mydomain.com/about 结果是 404

编辑: 我在 teamconstraint 路由中添加了一些日志记录。这是日志中的一些内容。

at=info method=GET path="/about" host=pincity.wrestlingiq.com request_id=b004b8cc-08e4-4bc9-a87a-d4b37deaa29c fwd="71.202.0.175" dyno=web.1 connect=1ms service=4ms status=301 bytes=391 protocol=https 
subdomain is www

似乎 heroku router 在处理路由代码之前进行了 301 重定向,这意味着子域约束永远没有机会触发。

编辑 2: 我在 DNSSimple 中发现了一条 URL 记录,它将根域重定向到 www 版本。我已经删除了它,并添加了应用程序逻辑来暂时处理该重定向。希望有所帮助。

我同意重定向似乎发生在请求到达您的 Rails 应用程序之前,这意味着它要么是 Heroku,要么是 before Heroku。你的 Heroku 应用程序前面有 CDN 吗?

我可以看到像 Cloudflare redirect page rule 这样的东西导致了这种行为。我对此特别怀疑,因为您的根域也会重定向到 www:

❯ curl -I https://wrestlingiq.com      
HTTP/1.1 301 Moved Permanently
Server: Cowboy
Date: Sun, 20 Jan 2019 14:20:54 GMT
Connection: keep-alive
Cache-Control: max-age=3600
Content-Type: text/html
Location: https://www.wrestlingiq.com/
Content-Length: 215
Via: 1.1 vegur