简体   繁体   中英

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

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?

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 means all interfaces. Including 127.0.0.1 aka localhost .

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.

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 ).

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

I "solved" this specifying the binding:

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

Rails 4.1 Warning Message.

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

=> 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.0 is not recommended and instead you should use 127.0.0.1 .

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

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

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

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

use this: rails server -u webrick

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM