rails 本地主机上的子域

rails subdomain on localhost

我想在本地主机上测试子域。我有以下说明:在启动服务器以 [​​=22=] 应用程序之前,应配置主机,以便应用程序可以支持多个用户的子域。为此,请在终端中键入 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.

请问我该如何解决?

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

rails s -p 3000 -b daycare.no

在浏览器中

 http://daycare.no:3000

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

rails s -p 3000 -b lvh.me

无需触摸 /etc/hosts 文件。

默认情况下,您无法在没有互联网连接的情况下浏览 link 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.

编辑主机文件是执行此类操作的一种极其混乱的方式。这里的问题是某些软件不读取该文件并直接转到 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 也以相同的方式解析。

我用www.vcap.me:3000

在rails 6.

之前无需额外配置

和rails6,需要在环境配置中添加www.vcap.me

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

你可以这样使用它:

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

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

  1. 编辑您的主机文件。 (正如您已经完成的那样)。

    nano /etc/hosts
    

添加你的 URL。

   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
    

在浏览器上打开 url http://admin.daycare.no