This is a DNS setup tutorial, but first we will have a look at how names can be resolved using /etc/hosts instead of running a DNS server such as BIND.
0.0.0.0 and 0 refer to the local host, and 127.0.0.1 refers to the loopback interface.
The difference between 0.0.0.0 and 127.0.0.1 is that the former refers to the local host's physical interface(s), while the latter refers to a software interface.
So on a computer with no NIC, only 127.0.0.1 will work.
From the man page of the loopback interface, man lo, we can read:
"The loop interface is a software loopback mechanism which may be used for performance analysis, software testing, and/or local communication."
So, in the end, 127.0.0.1 also refers to the local host.
The loop interface acts as any network interface, which means you can perform the following operations on it, for example:
ifconfig lo0 down (you can't ping 127.0.0.1 now)
ifconfig lo0 up
ifconfig lo0 1.2.3.4 (your loopback IP changed to 1.2.3.4, try to ping 1.2.3.4)
ifconfig lo0 127.0.0.1
which brings down, up, and sets lo0's IP, respectively.
Conclusion: It isn't surprising that you can see a local host's web page using the IPs in the examples above.
But what about localhost?
Why does localhost resolve to a local IP?
No DNS server is configured to resolve localhost to your computer's IP (you may even be disconnected from the Net, unable to query a DNS server).
Let's have a look at /etc/host.conf:
# First try the /etc/hosts file
hosts
# Now try the nameserver next.
bind
Let get the details from man /etc/host.conf:
hosts When this service name is used in host.conf, name and
address lookups are done by using the local hosts database.
The local hosts database is described in hosts(5).
bind This service makes the resolver use Internet Domain System
(DNS) for resolving hostnames and addresses. The DNS
resolver configuration is described in resolver(5).
This means that before even trying to query a DNS server, address lookups are done using /etc/hosts:
Here is where 127.0.0.1 resolves to localhost.
As you see, it also resolves to localhost.my.domain and to myname.my.domain, so you can also access you web documents these ways: