puma: Unable to connect through 127.0.0.1 when bound to localhost

I’m not sure if this issue has been raised and I can’t find any reference to it here but stemming from: https://github.com/rails/rails/issues/19815#issuecomment-111159194

Using rails s on WebRick allows using localhost and 127.0.0.1 to connect but when switching to puma this behavior changes and does not allow 127.0.0.1

Is this intended behavior for Puma or would you be open for a fix?

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Reactions: 9
  • Comments: 36 (8 by maintainers)

Most upvoted comments

Having this issue on FreeBSD as well

Commenting out

::1         localhost

in /etc/hosts seams to fix this issue.

I think it’s Puma issue

I’m using: bundle exec rails server Puma -b 0.0.0.0 -p 3000 which works just fine

I just want to mention why this is such an issue. Issues like this cause developers to turn off IPv6 on their machine rather than a more complicated workaround. This is bad for the community and the Internet at large. We need to encourage IPv6 adoption, and every “minor” issue like this is a blocker.

I had this same issue that I couldn’t connect locally to Puma aka curl [::1]:3000 failed. Fixed it without disabling IPv6 by forcing Puma to listen on localhost rather than 127.0.0.1 which it was for some reason using by default.

bundle exec rails s -b localhost -p 3000 

So the above command works with IPv4 and IPv6.

Hi y’all,

I’m going to go ahead and release @himdel’s fix, I think it’s a perfectly acceptable way to manage this issue. Sorry for the delay, the release will be out later today.

So, it seems that accessing ‘[::1]:3000’ works instead of 127.0.0.1. So correct me if I’m wrong, Puma is being asked to connect to localhost but Ruby or the OS translates that to IPv6 or IPv4 and puma then uses that to connect?

If you’re visiting this thread you may be forgetting that you might now, by default, be using IPv6 networking. As a result your resolution of localhost turns to ::1 rather than 127.0.0.1. So any aliases you had on that line in your /etc/hosts need to be duplicated or moved to the ::1 line.

For those coming here who access their Rails server via an alias you can simply duplicate your alias as @whistlerbrk said. For example, if your alias is www.fakewebsite.com you’ll want the following two lines in your /etc/hosts file:

127.0.0.1    www.fakewebsite.com
::1    www.fakewebsite.com

Looking at what @graudeejs and @himdel said in this comment it seems like puma is matching the first valid address in your /etc/hosts file. If yours is like mine it’ll start with:

127.0.0.1 localhost
::1    localhost

so for IPv4 you get 127.0.0.1 and for IPv6 you get ::1. Removing the latter doesn’t seem to force IPv4 for me as it may have for @graudeejs so I just added the second alias for ::1.

I’ll comment back if I figure out anything more or how this potentially effects production.

This bug has been fixed, so localhost should work correctly with or without IPv6. Using 0.0.0.0 makes the server available to everyone on every wireless network you connect to, and 127.0.0.1 does not work with IPv6. Please leave it the default or use localhost.

@yohayg: your advice leaves people open to unintended public access. It would be best if you remove your comment.

I’m using rackup -p 7000 -o 127.0.0.1

To clarify, it’s still a puma bug if localhost can resolve to both 127.0.0.1 and ::1 - which it does on some systems.