【问题标题】:rails subdomain on localhost本地主机上的 rails 子域
【发布时间】:2016-07-30 23:41:29
【问题描述】:

我想在 localhost 上测试子域。我有以下说明: 在启动服务器以运行应用程序之前,应配置主机,以便应用程序可以支持多个用户的子域。为此,请在终端中键入 sudo nano /etc/hosts 进入主机文件,然后在文件末尾添加子域,如下所示:

127.0.0.1 admin.daycare.no
127.0.0.1 daycare.no
127.0.0.1 worker.daycare.no
127.0.0.1 manager.daycare.no
127.0.0.1 parent.daycare.no

我按照上述说明进行操作。尝试检索 URL 时遇到以下错误:http://daycare.no:3000/

Unable to determine IP address from hostname daycare.no
The DNS server returned: Name Error: The domain name does not exist.
This means that the cache was not able to resolve the hostname presented in the URL. 
Check if the address is correct.

请问我该如何解决?

【问题讨论】:

  • 谁添加了关闭标志?下课后见。

标签: ruby-on-rails ruby localhost subdomain


【解决方案1】:

我使用的是 rails 6。您只需遵循 3 个步骤,就可以使用您定义的 URL 访问您的 rails 服务器,而无需提及端口号。

  1. 编辑您的主机文件。 (就像你已经做过的那样)。

    nano /etc/hosts
    

添加您的网址。

   127.0.0.1 admin.daycare.no
   127.0.0.1 daycare.no
   127.0.0.1 worker.daycare.no
   127.0.0.1 manager.daycare.no
   127.0.0.1 parent.daycare.no 
  1. 将所有 URL 添加到环境配置中

    config.hosts << 'admin.daycare.no'
    config.hosts << 'daycare.no'
    config.hosts << 'worker.daycare.no'
    config.hosts << 'manager.daycare.no'
    config.hosts << 'parent.daycare.no'
    
  2. 现在运行

     rvmsudo rails s -p80
    

在浏览器上打开您的网址http://admin.daycare.no

【讨论】:

    【解决方案2】:

    我用www.vcap.me:3000

    在 rails 6 之前无需额外配置。

    使用 rails 6,需要在环境配置中添加www.vcap.me

    config.hosts << 'www.vcap.me'
    

    你可以这样使用它:

    www.vcap.me:3000
    subdomain1.vcap.me:3000
    subdomain2.vcap.me:3000
    ..
    

    【讨论】:

    • 要允许对subdomain.vcap.me的请求,您还需要在配置中添加config.hosts &lt;&lt; "app.vcap.me"
    【解决方案3】:

    保存/etc/hosts文件后,像这样运行你的rails应用

    rails s -p 3000 -b daycare.no
    

    在浏览器中

     http://daycare.no:3000
    

    就我个人而言,我使用lvh.me:3000 只是在运行

    rails s -p 3000 -b lvh.me
    

    无需触摸/etc/hosts 文件。

    默认情况下,lvh.me:3000在没有网络的情况下是无法浏览链接的,解决方法是将127.0.0.1 lvh.me添加到主机文件中。

    # /etc/hosts file
    127.0.0.1   localhost
    127.0.0.1   lvh.me #make sure lvh.me is after localhost otherwise will not work
    

    但是,每次重新启动服务器时都运行它有点烦人。

    设置您的自定义命令:

    sudo nano .bash_profile
    # OR
    sudo nano .bashrc
    # OR
    sudo nano .profile
    

    并将这些行添加到那里:

    alias lvh='rails s -p 3000 -b lvh.me'
    alias lvh_production='RAILS_ENV=production rails s -p 3000 -b lvh.me' #production
    

    不要忘记重新启动您的终端选项卡,关闭并打开新选项卡或在同一选项卡上运行此命令 . ~/.bash_profile 取决于您在顶部使用的内容。


    另一种解决方案是 POW (LINK) 服务器可以为您提供自定义域,例如 daycare.dev

    【讨论】:

    • 原因是 /etc/hosts 文件只映射 IP 地址,而不映射端口。所以:3000是必需的。
    • 为什么我们不能使用 rails s -p 3000 -b 0.0.0.0
    • @DineshSaini 那么,您将如何访问子域?你的服务器无论如何都会运行。
    【解决方案4】:

    编辑你的主机文件是一种非常混乱的方式来做这种事情。这里的问题是某些软件不读取该文件并直接进入DNS进行解析。

    xip.io 这样的服务可以提供帮助。您可以使用以下地址:

    http://test.127.0.0.1.xip.io/
    

    这将始终解析为 127.0.0.1。

    还有像 Puma dev 这样的东西,它们带有自己的 .test 域解析器,因此 site.test 在本地工作,无需编辑任何文件。

    这两个系统的优点是您可以添加任意位并且它仍然有效:subdomain.test.test 和 subdomain.127.0.0.1.xip.io 也以相同的方式解析。

    【讨论】:

      猜你喜欢
      • 2011-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-14
      • 1970-01-01
      • 1970-01-01
      • 2015-04-26
      相关资源
      最近更新 更多