44

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?

desertnaut
  • 57,590
  • 26
  • 140
  • 166
Lou
  • 695
  • 2
  • 6
  • 10

7 Answers7

62

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.

andri
  • 11,171
  • 2
  • 38
  • 49
  • 7
    To set up WEBrick to listen only on localhost, which means external connections are ignored: script/server --binding=127.0.0.1 – tadman Sep 25 '09 at 20:29
  • 6
    FYI, specifying the binding to 127.0.0.1 when booting up WEBrick for me made my local rails connection (and, thus, external web connnections as well) much faster. So if your local connection is slow, I recommend what @tadman suggested. In Rails 3, it's **rails server --binding=127.0.0.1** – Steph Rose Oct 25 '11 at 13:41
21

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

Michael Krelin - hacker
  • 138,757
  • 24
  • 193
  • 173
2

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.

John Vandivier
  • 2,158
  • 1
  • 17
  • 23
  • 2
    `0.0.0.0` is not an IP address you can actually access using a client. It's simply a shortcut for the system binding call to use _all_ available IP addresses. – Tobias J Dec 10 '14 at 18:50
2

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
ecoologic
  • 10,202
  • 3
  • 62
  • 66
  • 1
    The reason for the `localhost` binding preventing `lvh.me` to work was Puma issue #782, now fixed. More details [here](https://stackoverflow.com/a/53678798). – tanius Dec 08 '18 at 03:13
2

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.

Joshua Pinter
  • 45,245
  • 23
  • 243
  • 245
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.

Rob R
  • 1
0

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

Omer Aslam
  • 4,534
  • 6
  • 25
  • 24