对 heroku 子域使用 rails 域约束

Using rails domain constraints with heroku subdomains

我有一个 rails 应用程序,在 routes.rb

中配置了两个子域
Rails.application.routes.draw do
  constraints subdomain: 'admin' do
    devise_for :admin_users, ActiveAdmin::Devise.config
    ActiveAdmin.routes(self)
  end

  namespace :api, defaults: { format: :json },
            constraints: { subdomain: 'api' }, path: '/' do

    scope module: :v1,
          constraints: ApiConstraints.new(version: 1, default: true) do
      resources :countries, only: [:show, :index]
    end

  end
end

这在本地非常有效。我的/etc/hosts看起来像

admin.localhost.local localhost
api.localhost.local   localhost

但是在 Heroku 中进行配置时,事情并没有按预期进行。

我将我的 DNS 和 Heroku 的自定义域配置如下:

admin.my_project.my_domain.com -> admin.my_project.my_domain.com.herokudns.com
api.my_project.my_domain.com -> api.my_project.my_domain.com.herokudns.com

但是当我访问http://admin.my_project.my_domain.com I get an routing error

日志中有这样的内容:

heroku[router]: at=info method=GET path="/" host=admin.my_project.my_domain.com request_id=XXX fwd="IP.IP.IP.IP" dyno=web.1 connect=0ms service=3ms status=404 bytes=1744 protocol=http
app[web.1]: INFO -- : Started GET "/" for IP.IP.IP.IP at 2017-03-14
app[web.1]: FATAL -- : ActionController::RoutingError (No route matches [GET] "/"):

意思是当收到请求时,Rails 不知道它来自子域。

我怎样才能完成这项工作? 谢谢

好的,我已经解决了

我需要将其添加到 config/environments/production.rb

  config.action_dispatch.tld_length = 2

以便正确选择子域。