Safari is unable to reach localhost (127.0.0.1)

I've encountered a really strange Mac problem: Safari is unable to connect to localhost whereas all other applications can connect as normal (Firefox, Chrome, ping, etc.).

My /etc/hosts is the standard setup:

$ egrep localhost /etc/hosts
# localhost is used to configure the loopback interface
127.0.0.1   localhost
::1         localhost 
fe80::1%lo0 localhost
127.0.0.1   testing.localhost

Question: Have anyone else encountered this? What could be the problem?

Update #1: I have a server running on localhost:8080. When accessing from Safari I get the error message "Safari could not connect to the server". "http://localhost:8080/" works flawlessly in Firefox and Chrome.

Update #2: Same problem with "http://127.0.0.1:8080/" - so it does not seem to be related to resolving localhost to 127.0.0.1.


I think we need a little more information based on your question.

I'm going to guess you have a web server running to accept requests? I currently don't, and Safari won't connect to 127.0.0.1, because it "Couldn't find 127.0.0.1" - even though I can ping it just fine.

So first, you need to have something running. Secondly, have you tried connecting to testing.localhost with Safari, just incase the two entries are confusing Safari (this is entirely possible).

When you say "applications can connect as normal" with Chrome and Firefox, what are you seeing? The web page you're expecting to see? A different error?

Edit

I downloaded and installed MAMP, and I could not reproduce this error.

Some forum threads suggest IPv6 resolution is causing this issue. I would be inclined to turn off IPv6 and remove the entry from the hosts file (temporarily just for testing)

  1. Apple Menu > System Preferences
  2. Network
  3. Select your Network Connection > Advanced
  4. Configure IPv6 > Off

Apply, restart, and see how it goes.


I've had this happen before -- using http://computername.local/ worked for me, but I'm unsure why it would be different than just using a name in a host file.


Comment out the line:

::1         localhost 

from your /etc/hosts file. Apparently this would be caused by IPv6 support in Safari.


Some applications decide to bind to only a single interface - 192.168.13.37, for example, if that is the address assigned to eth0 or such - instead of using 0.0.0.0 for all interfaces.

Others might be using only IPv6 addresses ::1 (localhost) or :: (all interfaces). Try http://[::1]:8080/ in this case.