http://localhost:3000 works, but http://127.0.0.1:3000 doesn't

4

2

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 

LandonSchropp

Posted 2015-04-25T19:28:32.700

Reputation: 169

3Odds 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 – 2015-04-25T19:32:12.347

1Good 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! – LandonSchropp – 2015-04-25T19:42:07.597

Answers

3

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.

YLearn

Posted 2015-04-25T19:28:32.700

Reputation: 1 741