Puma: How can I make puma listening to all interfaces?

Created on 11 Feb 2020  路  4Comments  路  Source: puma/puma

Describe the bug
My rails server in production is rejecting connections for reasons I do not understand. When I try to connect via browser I receive the following error message from Chromium:

This site can鈥檛 be reached. cinemeeting.eu refused to connect. ERR_CONNECTION_REFUSED.

Puma config:
I copied and pasted config/puma.rb from Heroku documentation, although I have deployed my application to NetCup, a German hosting provider, via dokku, following instructions in the Dokku documentation:

workers Integer(ENV['WEB_CONCURRENCY'] || 2)
threads_count = Integer(ENV['RAILS_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

The only help I received so far is from josegonzalez at GitHub who suggested my application should listen to all interfaces and respect the PORT environment variable. How can I make puma listening to all interfaces with the bind statement? Is it necessary?

question

Most helpful comment

You need to bind to the unspecified address, 0.0.0.0. Rails will bind to localhost (I think) by default. Puma listens to 0.0.0.0 by default, but Rails overrides that.

Instead of port ENV['PORT']:

bind "tcp://0.0.0.0:#{ENV['PORT'] || 3000}"

All 4 comments

You need to bind to the unspecified address, 0.0.0.0. Rails will bind to localhost (I think) by default. Puma listens to 0.0.0.0 by default, but Rails overrides that.

Instead of port ENV['PORT']:

bind "tcp://0.0.0.0:#{ENV['PORT'] || 3000}"

@nateberkopec so will the following config file be all right?

workers Integer(ENV['WEB_CONCURRENCY'] || 2)
threads_count = Integer(ENV['RAILS_MAX_THREADS'] || 5)
threads threads_count, threads_count

preload_app!

rackup      DefaultRackup
bind "tcp://0.0.0.0:#{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

or I have to preserve the port ENV['PORT'] line of code, like the following:

workers Integer(ENV['WEB_CONCURRENCY'] || 2)
threads_count = Integer(ENV['RAILS_MAX_THREADS'] || 5)
threads threads_count, threads_count

preload_app!

rackup      DefaultRackup
port        ENV['PORT']     || 3000
bind "tcp://0.0.0.0:#{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

Don鈥檛 use the second. Port calls bind underneath.

You need to bind to the unspecified address, 0.0.0.0. Rails will bind to localhost (I think) by default. Puma listens to 0.0.0.0 by default, but Rails overrides that.

Instead of port ENV['PORT']:

bind "tcp://0.0.0.0:#{ENV['PORT'] || 3000}"

solved my problem and saved my day, thanks a lot!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sanjibukai picture sanjibukai  路  3Comments

banister picture banister  路  3Comments

junaruga picture junaruga  路  4Comments

jchristie55332 picture jchristie55332  路  4Comments

mvz picture mvz  路  4Comments