http://localhost:3000 works, but http://127.0.0.1:3000 doesn't
In your hosts file, you have both a IPv4 and a IPv6 resolution. Most operating systems today will prefer IPv6 if available. Since localhost is working and 127.0.0.1 is not, this could be your problem.
There are two things I would check in this case:
- Check to see if the server/service is running only on IPv6. If so, change it so it is running on both IPv4 and IPv6.
- Check the firewall rules to ensure they are allowing access to localhost on that port for both IPv4 and IPv6.
TLDL; make sure with sudo lsof -i tcp:<port>
that there is no other process running on the same port. (sudo if necessary) kill -9
it if there is.
I just had this problem and the other answer wasn't too helpful for me. I had an nginx reverse proxy, and frontend and backend servers all running in docker containers.
There was another process running on the same port by user root
which for some reason was not messing up Docker Compose from port binding. When I would hit localhost
in my address bar, it would correctly route to nginx
running from my containers. When I went to 127.0.0.1
it would not.
I noticed this issue when even after shutting down nginx on my homebrew installation and my docker container running nginx, I was still receiving a 502 Bad Gateway from nginx on both Chrome and FireFox.
After checking 8082 (WITH SUDO), I saw that root
had a process running on the same port. After shutting down that process, I was able to connect to my docker services with 127.0.0.1
again along with localhost
.
I assume that 127.0.0.1
was actually navigating to that process (143 below) and not my docker container, so I was actually hitting two different webserver processes with 127.0.0.1
and localhost
. Very odd! If someone smarter than me has any input on how that was even possible please comment :)! Likely something to do with the answer above.