El Capitan Safari Can't open localhost but 127.0.0.1 works

 127.0.0.1   localhost
 255.255.255.255 broadcasthost
 ::1             localhost

This is my /etc/hosts configuration. I can use ip address 127.0.0.1 to open a loopback, but 'localhost' fails.... I have read these two posts:El Capitan Safari Can't open localhost and El Capitan Safari Can't open localhost, but none of the cases fits my problem.

The error message I got from Safari is

Safari can’t open the page “‎localhost” because the server unexpectedly dropped the connection. This sometimes occurs when the server is busy. Wait for a few minutes, and then try again.

I also tried the localhost on Chrome, it gave me similar message... Is there something wrong with setting alias?


Solution 1:

After reading this post https://stackoverflow.com/questions/20345132/127-0-0-1-is-working-but-localhost-not-working

I figured it out that I only need to comment out ::1 localhost then everything works fine

Solution 2:

Had a similar issue here. I'm running a local server and try to connect via curl, eg, the following does not work:

curl localhost 8080

but this:

curl 127.0.0.1 8080

works.

File: /etc/hosts

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1   localhost
255.255.255.255 broadcasthost
::1             localhost

My server only binds to ipv4, that's why i have to tell curl to use ipv4 stack:

curl -4 localhost:8080

works.