views:

1401

answers:

3

I am very new to Ruby on Rails so 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 :

…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 WEBbrick server use localhost ?

+11  A: 

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
To set up WEBrick to listen only on localhost, which means external connections are ignored: script/server --binding=127.0.0.1
tadman
+4  A: 

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

Michael Krelin - hacker
A: 

Your question indicates you're somewhat unfamiliar with DNS and what it's for.

Host name strings (even "localhost") must be resolved to IP addresses before a connection can be made. A host can be known by many names (and often is). So this is a many-to-one relationship. ALSO - a host can have multiple IP addresses (and almost always does) - for example, it might have a "real" address of 66.116.33.128 (I made that up) and it's almost certainly also known as "127.0.0.1" if it's following the TCP/IP standards. That's also a many-to-one relationship, thus the "0.0.0.0" solution.

So do you see the dilemma? A program listening on "port 3000" really has no idea of what hostname someone might have originally used to identify the host.

see also: http://linux.die.net/man/3/gethostbyaddr

pbr
Technically 'localhost' doesn't exist in DNS. It's an /etc/hosts or hosts.txt type of thing, part of the resolver but not a function of DNS itself.
tadman
Well, as an implementation detail within the calls, yes, it's handled locally - but a program will send the string "localhost" through the DNS "API" regardless. From the programming point of view, there's no difference between resolving the string "localhost" and "abc.com"
pbr