Safari cannot connect to localhost but can connect to 127.0.0.1
For some reason, all of a sudden my Safari cannot connect to localhost because it cannot establish a secure connection. However, it is able to connect 127.0.0.1. Oddly enough Firefox is able to connect to localhost and 127.0.0.1. I believe it's some sort of certificate issue, but that's just a guess. What can I do so I can connect to localhost on Safari again?
Thanks
22 Answers
You probably installed something on your machine that acts as a web server and uses HTTP Strict Transport Security (HSTS) to make sure your browser knows it should always use TLS (HTTPS) when connecting. Then you connected to it by pointing your browser at localhost, and now your browser believes it should only use HTTPS when connecting to localhost.
Apparently Safari stores its list of HSTS websites in ~/Library/Cookies/HSTS.plist, so you should be able to fix it by editing that plist file to remove the entry for localhost, or by deleting that plist altogether if you don't mind resetting the whole list of sites Safari has learned to strictly require TLS for.
Note: This might not solve everyone's problem.
I am a MAMP user and noticed in my hosts settings that 127.0.0.1 localhost was removed somehow. Type sudo nano /etc/hosts, type in your password, and then add these default lines back:
127.0.0.1 localhost
::1 localhost
This worked for me. I hope others find it helpful.