为什么 Ruby on Rails 使用 http://0.0.0.0:3000 而不是 http://localhost:3000?
当我尝试遵循官方的“入门”Ruby on Rails 教程时,很快就出错了。基本上它说:
...导航到 http://localhost:3000。您应该会看到 Rails 的默认信息页面。
但是当我按照说明操作时,我得到了
=> Rails 2.3.4 application starting on http://0.0.0.0:3000
尝试两个地址后,我知道它们指向同一件事,但是有人可以向我解释为什么 Ruby on Rails 使用 http://0.0.0.0:3000
而不是 http://localhost:3000
?
有没有办法让 WEBrick 服务器始终使用 localhost?
When I tried to follow the official "Getting Started" Ruby on Rails tutorial, it went wrong very quickly. Basically it said :
…navigate to http://localhost:3000. You should see Rails’ default information page.
But when I follow the instructions, I get
=> Rails 2.3.4 application starting on http://0.0.0.0:3000
After trying both addresses, I know that they point to the same thing, but can someone explain to me why Ruby on Rails uses http://0.0.0.0:3000
instead of http://localhost:3000
?
Is there a way to always have the WEBrick server use localhost?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
本地主机的字面意思是“您的本地主机”,通常由 127.0.0.1 标识,并且到该地址的所有流量都通过环回接口路由。如果您的 Web 服务器正在侦听 127.0.0.1 上的连接,这意味着它只接受来自同一主机的请求。
0.0.0.0 意味着 Rails 正在侦听所有接口,而不仅仅是环回接口。
Localhost means quite literally "your local host", usually identified by 127.0.0.1 and all traffic to that address is routed via a loopback interface. If your Web server is listening for connections on 127.0.0.1, this means that it only accepts requests coming from the same host.
0.0.0.0 means that Rails is listening on all interfaces, not just the loopback interface.
0.0.0.0
表示所有接口。包括127.0.0.1
又名localhost
。0.0.0.0
means all interfaces. Including127.0.0.1
a.k.a.localhost
.众所周知,如果我访问,我的 Firefox 浏览器会正确显示本地托管服务器
http://localhost:3000/
但当我尝试访问时它不会显示
http://0.0.0.0:3000/
正如鲁比推荐的那样。显然,从某种意义上来说,它们并不等同。
顺便说一句,我在 Windows 上。
Just so everyone knows, my firefox browser correctly displays the locally hosted server if I access
http://localhost:3000/
but it does NOT display when I attempt to access
http://0.0.0.0:3000/
as recommended by Ruby. Clearly, in some sense, they are not equivalent.
I'm on Windows btw.
如果您想要
localhost
,一种快速方法是指定绑定rails s -blocalhost
(以及使用-pNNNN
的端口,使用-pNNNN
指定更多选项代码>rails --help)。我的服务器默认开始在
localhost
上运行,原因有待调查。结果,lvh.me
停止工作,导致我无法指定子域(例如:www.lvh.me:3000
)。我“解决”了这个指定绑定:
If you want
localhost
, one quick way is to specify the bindingrails s -blocalhost
(and the port with-pNNNN
, more options withrails s --help
).My server started running by default on
localhost
for reasons to be investigated. As a resultlvh.me
stopped working, preventing me from specifying subdomains (eg:www.lvh.me:3000
).I "solved" this specifying the binding:
Rails 4.1 警告消息。
仅供参考,在 Rails 4.1 上,您将在启动时收到一条警告消息,如下所示:
这表明不建议绑定到
0.0.0.0
,而是应使用127.0.0.1
>。在 Rails 4.2+ 中,Rails 服务器默认绑定到
localhost
,而不是0.0.0.0
甚至127.0.0.1
。Rails 4.1 Warning Message.
FYI, on Rails 4.1 you will get a warning message on boot that looks like this:
This indicates that binding to
0.0.0.0
is not recommended and instead you should use127.0.0.1
.In Rails 4.2+ the Rails server default binding is to
localhost
instead of0.0.0.0
or even127.0.0.1
.对于我们这些使用 Nitrous.io 虚拟服务器环境进行开发的人来说,我相信我们必须绑定到 0.0.0.0,因为本身没有本地主机。
For those of us using Nitrous.io virtual server envrionment for development, I believe we have to bind to 0.0.0.0 as there is no localhost per se.
重新启动操作系统对我有用。 (在 Mac 版本 10.12 上)
Restarted the os works for me. (On Mac v 10.12)