-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unable to connect through 127.0.0.1 when bound to localhost #782
Comments
I had this problem and I was looking for solution. I'm not sure what is exactly the reason of this behaviour but here are things I found out and solution to fix this in my app. When I start
and http://127.0.01:3000 doesn't work. When I start puma explicitly without config file then it listens on tcp://0.0.0.0:3000 and http://127.0.01:3000 does work. Read more https://github.com/puma/puma#configuration-file
When I created my custom config file then it works as well # config/puma.rb
workers Integer(ENV['WEB_CONCURRENCY'] || 2)
threads_count = Integer(ENV['MAX_THREADS'] || 5)
threads threads_count, threads_count
preload_app!
rackup DefaultRackup
port ENV['PORT'] || 3000
environment ENV['RACK_ENV'] || 'development'
on_worker_boot do
# Worker specific setup for Rails 4.1+
# See: https://devcenter.heroku.com/articles/deploying-rails-applications-with-the-puma-web-server#on-worker-boot
ActiveRecord::Base.establish_connection
end |
From my understanding both On other webservers, namely Unicorn and Webrick listening on |
If anyone can replicate the issue, I'd be happy to fix it. I tried a bunch of combos of rails/puma/ruby this morning and couldn't get it to pop up. |
@evanphx I simply started puma with |
@Tonkpils What version of ruby? What OS? Because ruby and the OS translate |
Ruby 2.2.1 and OS X Yosemite. |
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? |
Well, puma passes whatever the host you want to bind to directly to |
I'm assuming In any case, I'll close this as a non-issue since this is not puma's issue. |
Having this issue on FreeBSD as well Commenting out
in /etc/hosts I think it's Puma issue |
@evanphx: Having this same issue on OSX El Capitan, this definitely seems like a puma issue. The trick ^^^ worked, not sure what |
@chadzink |
The same for me. Got this issue after El Capitan upgrade. |
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 |
To clarify, it's still a puma bug if localhost can resolve to both |
Hi @himdel , any plan to fix this? |
@shawzt not sure.. I'd love to, but essentially what happens is If you need a workaround and use puma only from rails, this works... diff --git a/lib/puma/binder.rb b/lib/puma/binder.rb
index 6cb8909..2751484 100644
--- a/lib/puma/binder.rb
+++ b/lib/puma/binder.rb
@@ -236,6 +236,12 @@ module Puma
# allow to accumulate before returning connection refused.
#
def add_tcp_listener(host, port, optimize_for_latency=true, backlog=1024)
+ if host == 'localhost'
+ add_tcp_listener('127.0.0.1', port, optimize_for_latency, backlog)
+ add_tcp_listener('::1', port, optimize_for_latency, backlog)
+ return
+ end
+
host = host[1..-2] if host and host[0..0] == '['
s = TCPServer.new(host, port)
if optimize_for_latency (but it's clearly not the right place to fix this). |
... and according to ext/socket/tcpserver.c, it is supposed to return the first successfully created socket for one of the addresses returned by So I guess I'll leave it to somebody who's already touched |
+1 here. Just want to know how/where will I add those snippet code to @himdel? |
@alecguintu if you look at it carefully, you'll see that it mentions both the file name and the line numbers.. |
Haha. I mean, where do I find the file binder.rb? I've checked on rails app, there is no puma folder under lib folder. |
/.rvm/gems/ruby-2.2.4/gems/puma-3.2.0/lib/puma/binder.rb |
@alecguintu probably the safest way to find the right file would be |
Thanks so much for this @himdel! |
Thank you @himdel! now it works with 127.0.0.1 and ::1 |
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
Looking at what @graudeejs and @himdel said in this comment it seems like puma is matching the first valid address in your
so for IPv4 you get I'll comment back if I figure out anything more or how this potentially effects production. |
Hi guys, This is a very important issue. I was trying to proxy some requests from webpack-dev-server and had a very hard time to figure that I should change localhost:3000 to [::1]:3000. |
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. |
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. |
I'm using: bundle exec rails server Puma -b 0.0.0.0 -p 3000 which works just fine |
I'm using |
This bug has been fixed, so @yohayg: your advice leaves people open to unintended public access. It would be best if you remove your comment. |
I had this same issue that I couldn't connect locally to Puma aka bundle exec rails s -b localhost -p 3000 So the above command works with IPv4 and IPv6. |
@sfcgeorge glad to hear that |
@RobinDaugherty I don't wish to be the harbinger of reopened issues but I believe our app is just using the defaults. My machine is macOS High Sierra (experiencing the issue), whereas my colleague's machine running an older version of macOS is unaffected and seems to work fine. |
And in my case, the default is |
I'm not sure if this issue has been raised and I can't find any reference to it here but stemming from: rails/rails#19815 (comment)
Using
rails s
on WebRick allows usinglocalhost
and127.0.0.1
to connect but when switching topuma
this behavior changes and does not allow127.0.0.1
Is this intended behavior for Puma or would you be open for a fix?
The text was updated successfully, but these errors were encountered: