【问题标题】:Heroku, custom subdomain and a custom constraintHeroku,自定义子域和自定义约束
【发布时间】:2019-06-13 05:13:00
【问题描述】:

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

Ruby on rails 5.1

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

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

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

  3. 在部署并将这个团队添加到生产应用程序数据库('pincity' 的 slug,以便 pincity.mydomain.com 工作)之后,我重新启动了 dynos。

我认为这应该是我需要做的。

我的路线文件相当简单

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 路由器在处理路由代码之前进行了 301 重定向,这意味着子域约束永远没有机会触发。

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

【问题讨论】:

    标签: ruby-on-rails ruby heroku routing subdomain


    【解决方案1】:

    我同意重定向似乎在请求到达 Rails 应用程序之前发生,这意味着它可能是 Heroku 或 之前 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
    

    【讨论】:

    • 嗯...我使用 dnssimple。在你提出这个问题后,我确实注意到我在根目录上有一个指向 www (support.dnsimple.com/articles/url-record) 的 url 记录,旨在强制使用 www。但是,这个记录在到达heroku 路由器之前不会被重定向吗?我会假设如果在 heroku 之前,heroku 永远不会在其日志中首先收到子域请求?
    • Doh - 我忘记了机架规范主机安装,它在 rails 应用程序之前触发。我将把这个答案标记为正确,因为它确实在我的应用程序前面,比我想象的离家更近一点。
    猜你喜欢
    • 2012-08-22
    • 1970-01-01
    • 1970-01-01
    • 2014-11-22
    • 2016-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-12
    相关资源
    最近更新 更多