5

I'm building a Rails application on my local dev machine. When running my local server, I can access http://localhost:3000 without a problem. However, when I try to navigate to http://127.0.0.1:3000, I get an ERR_CONNECTION_REFUSED. Normally, I won't care about this problem, but when I try to access a tunnel using ngrok, I get this error:

Tunnel <URL> unavailable

Unable to initiate connection to 127.0.0.1:3000. A web server must be running on port 127.0.0.1:3000 to complete the tunnel.

I did a clean install of OS X this morning, and the only things running on my system are what's in my dotfiles readme. Here's my complete /etc/hosts file:

##
# 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 
2
  • 3
    Odds are, when you are using localhost it isle fearing IPv6. So perhaps the server is not running on IPv4 or more likely only the IPv6 port is allowed through firewall.
    – YLearn
    Apr 25, 2015 at 19:32
  • 1
    Good call! I rebooted the Rails server using -b :: to listen for IPv4 and IPv6 and it worked! I didn't realize Puma wouldn't do this by default. Can you toss your comment in an answer so I can mark it as the accepted answer? Thanks for the help! Apr 25, 2015 at 19:42

2 Answers 2

4

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:

  1. 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.
  2. Check the firewall rules to ensure they are allowing access to localhost on that port for both IPv4 and IPv6.
3

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.

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .