【问题标题】:Can I make Rails / WEBrick recognize entries in /etc/hosts as subdomains (instead of domains)?我可以让 Rails / WEBrick 将 /etc/hosts 中的条目识别为子域(而不是域)吗?
【发布时间】:2014-08-19 16:54:28
【问题描述】:

我正在尝试在本地为 Rails 应用程序使用子域,因此我将以下行添加到我的 /etc/hosts 文件中:

# add 'test' subdomain for localhost
127.0.0.1 test.localhost

现在我可以将我的浏览器指向test.localhost:3000,它会点击我的 Rails 应用程序。

但是,Rails 或 WEBrick 将整个 darn thang 解释为域:

# logging in the controller
logger.debug("domain: '#{request.domain}', subdomain: '#{request.subdomain}'")

# output in the console
domain: 'test.localhost', subdomain: ''

有没有简单的方法让 WEBrick Rails 将 test 解释为子域?

谢谢!

更新

我最终制作了一个before_action 作为解决方法。

def set_domain_and_subdomain
  @domain = request.domain
  @subdomain = request.subdomain

  # HACK: force /etc/hosts subdomains
  if Rails.env.development?
    if m = request.domain.match(/([^\.]+).localhost/)
      @subdomain = m[1]
      @domain = 'localhost'
    end
  end
end

但是我仍然很好奇是否有一种方法可以在我的计算机上通用地执行此操作(即在 `/etc/hosts 或其他东西中)

【问题讨论】:

  • 我知道像 lvh.me 这样的服务,但我不知道如何让它与 localhost 共享相同的会话,所以它迫使我每次都在 Rails 应用程序中进行 facebook auth :(

标签: ruby-on-rails subdomain webrick


【解决方案1】:

很晚才找到这篇文章,但为了后代:https://github.com/rails/rails/issues/12438

设置顶级域长度 (TLD) 允许 request.subdomain 以您期望的方式定位子域。

我将config.action_dispatch.tld_length = 0 放入config/environments/development.rb 中,一切顺利。

记得重启你的服务器

【讨论】:

    猜你喜欢
    • 2013-11-14
    • 1970-01-01
    • 2012-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-06
    • 2018-04-23
    • 2015-04-22
    相关资源
    最近更新 更多