【问题标题】:Using rails domain constraints with heroku subdomains将 rails 域约束与 heroku 子域一起使用
【发布时间】:2017-08-05 00:39:21
【问题描述】:

我有一个 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 时出现路由错误

日志有这样的:

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 不知道它来自子域。

我怎样才能做到这一点? 谢谢

【问题讨论】:

    标签: ruby-on-rails heroku routing subdomain


    【解决方案1】:

    好的,我已经解决了。

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

      config.action_dispatch.tld_length = 2
    

    以便正确选择子域。

    【讨论】:

      猜你喜欢
      • 2014-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-07
      • 2015-03-13
      • 2019-06-13
      • 2018-02-09
      相关资源
      最近更新 更多