为什么 Ruby on Rails 使用 http://0.0.0.0:3000 而不是 http://localhost:3000?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1478747/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:48:09  来源:igfitidea点击:

Why does Ruby on Rails use http://0.0.0.0:3000 instead of http://localhost:3000?

ruby-on-railsrubylocalhostwebrick

提问by Lou

I am very new to Ruby on Railsso when I tried to follow the official "Getting Started" ruby on rails tutorial, I was a bit disappointed because it went wrong very quickly. Basically it said :

我对Ruby on Rails非常陌生,所以当我尝试遵循官方的“入门”ruby on Rails 教程时,我有点失望,因为它很快就出错了。基本上它说:

…navigate to http://localhost:3000. You should see Rails' default information page.

...导航到http://localhost:3000。您应该会看到 Rails 的默认信息页面。

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:3000instead of http://localhost:3000?

在尝试了这两个地址后,我知道它们指向同一件事,但是有人可以向我解释为什么 Ruby on Rails 使用http://0.0.0.0:3000而不是http://localhost:3000

Is there a way to always have the WEBbrick server use localhost ?

有没有办法让 WEBbrick 服务器始终使用 localhost ?

回答by andri

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.

Localhost 的字面意思是“您的本地主机”,通常由 127.0.0.1 标识,所有到该地址的流量都通过环回接口路由。如果您的 Web 服务器正在侦听 127.0.0.1 上的连接,这意味着它只接受来自同一主机的请求。

0.0.0.0 means that Rails is listening on allinterfaces, not just the loopback interface.

0.0.0.0 意味着 Rails 正在侦听所有接口,而不仅仅是环回接口。

回答by Michael Krelin - hacker

0.0.0.0means all interfaces. Including 127.0.0.1a.k.a. localhost.

0.0.0.0表示所有接口。包括127.0.0.1又名localhost

回答by Joshua Pinter

Rails 4.1 Warning Message.

Rails 4.1 警告消息。

FYI, on Rails 4.1 you will get a warning message on boot that looks like this:

仅供参考,在 Rails 4.1 上,您将在启动时收到一条警告消息,如下所示:

=> Run `rails server -h` for more startup options
=> Notice: server is listening on all interfaces (0.0.0.0). Consider using 127.0.0.1 (--binding option)

This indicates that binding to 0.0.0.0is not recommended and instead you should use 127.0.0.1.

这表明0.0.0.0不推荐绑定到,而应该使用127.0.0.1.

In Rails 4.2+ the Rails server default binding is to localhostinstead of 0.0.0.0or even 127.0.0.1.

在 Rails 4.2+ 中,Rails 服务器默认绑定是 tolocalhost而不是 ,0.0.0.0甚至是127.0.0.1

回答by ecoologic

If you want localhost, one quick way is to specify the binding rails s -blocalhost(and the port with -pNNNN, more options with rails s --help).

如果需要localhost,一种快速的方法是指定绑定rails s -blocalhost(以及带有 的端口,带有 的-pNNNN更多选项rails s --help)。

My server started running by default on localhostfor reasons to be investigated. As a result lvh.mestopped working, preventing me from specifying subdomains (eg: www.lvh.me:3000).

localhost由于需要调查的原因,我的服务器默认开始运行。结果lvh.me停止工作,阻止我指定子域(例如:)www.lvh.me:3000

I "solved" this specifying the binding:

我“解决”了这个指定绑定:

rails s -b0.0.0.0 # will work with lvh.me

回答by John Vandivier

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.

众所周知,如果我访问,我的 Firefox 浏览器会正确显示本地托管的服务器, http://localhost:3000/但当我http://0.0.0.0:3000/按照 Ruby 的建议尝试访问时,它不会显示 。显然,在某种意义上,它们并不等价。

I'm on Windows btw.

顺便说一句,我在 Windows 上。

回答by Rob R

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.

对于我们这些使用 Nitrous.io 虚拟服务器环境进行开发的人,我相信我们必须绑定到 0.0.0.0,因为本身没有 localhost。

回答by jellohead

Actually rails has different configuration options about if it listens to something specific or all interfaces.

实际上,rails 有不同的配置选项,关于它是侦听特定接口还是所有接口。

回答by Omer Aslam

Restarted the os works for me. (On Mac v 10.12)

重新启动操作系统对我有用。(在 Mac v 10.12 上)