Skip to content
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

cannot bind to 127.0.0.1/0.0.0.0 in Rails 4.2.0 beta 1 #12

Open
brucehsu opened this issue Oct 3, 2014 · 12 comments
Open

cannot bind to 127.0.0.1/0.0.0.0 in Rails 4.2.0 beta 1 #12

brucehsu opened this issue Oct 3, 2014 · 12 comments

Comments

@brucehsu
Copy link

brucehsu commented Oct 3, 2014

After switching to Unicorn on my new project using rails 4.2.0 beta1, I found that I cannot access subdomain via http://lvh.me. With further investigation, the problem appears to be related to a binding issue of unicorn-rails under 4.2.0 environment.

Following tests are done in separate empty rails apps, on my MBPr with OSX 10.9.5, MRI 2.1.3.

Here's the output of bundle exec rails s under rails 4.1.6:

=> Booting Unicorn
=> Rails 4.1.6 application starting in development on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
=> Notice: server is listening on all interfaces (0.0.0.0). Consider using 127.0.0.1 (--binding option)
=> Ctrl-C to shutdown server
I, [2014-10-03T15:29:41.484862 #23405]  INFO -- : listening on addr=0.0.0.0:3000 fd=11

And here's the one under rails 4.2.0 beta 1:

=> Booting Unicorn
=> Rails 4.2.0.beta1 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
I, [2014-10-03T15:31:26.314647 #24701]  INFO -- : listening on addr=[::1]:3000 fd=14

Seems like 4.2.0 changed its default listening behavior?

@klochner
Copy link

This bit me as well because it breaks docker to bind to localhost. You can revert to the prior bind address with -b0.0.0.0

@inkstak
Copy link

inkstak commented Dec 23, 2014

Hi, I came across the same problem. I was not able to connect to my server from virtual machines (and I wasted so much time to realize that it came from here).

What caused this change ? Rails ? Unicorn ? Unicorn-rails ?

@brucehsu
Copy link
Author

I think this is related to this change in Rails 4.2.0: http://guides.rubyonrails.org/4_2_release_notes.html#default-host-for-rails-server

@tostasqb
Copy link

tostasqb commented Jan 7, 2015

@brucehsu is absolutly right.
One workaround is to add the following code to your config/boot.rb

require 'rubygems'
require 'rails/commands/server'

module Rails
  class Server
    alias :default_options_alias :default_options
    def default_options
      default_options_alias.merge!(:Host => '0.0.0.0')
    end
  end
end

@mosinski
Copy link

@tostasqb Thanks! 👍

@jsoohouse
Copy link

@tostasqb Thank You!!!

@Taroka
Copy link

Taroka commented May 29, 2015

Hi I just try to use 'rails server -b 0.0.0.0' but it still cannot work. How should I adjust my firewall? My machine is OS X Yosemite. :)

@lfender6445
Copy link

👍 - noticed rails was binding to localhost on my vps by default, so specifying -b 0.0.0.0 fixed it for me as well

@naveeninja
Copy link

rails s --binding 0.0.0.0
OR
rails s -b 0.0.0.0
OR
rails server --binding 0.0.0.0
OR
rails server -b 0.0.0.0

@adrianotadao
Copy link

Or you can do:

config/boot.rb

require 'rails/commands/server'
module Rails
  class Server
    def default_options
      super.merge(Host: '0.0.0.0', Port: 3000)
    end
  end
end

credits: http://stackoverflow.com/questions/28668436/how-to-change-the-default-binding-ip-of-rails-4-2-development-server

@phlegx
Copy link

phlegx commented Sep 9, 2015

If you put the default options on config/boot.rb then all command attributes for rake and rails fail (example: rake -T or rails g model user)! So, append this to bin/rails after line require_relative '../config/boot' and the code is executed only for the rails server command:

    if ARGV.first == 's' || ARGV.first == 'server'
      require 'rails/commands/server'
      module Rails
        class Server
          def default_options
            super.merge(Host:  '0.0.0.0', Port: 3000)
          end
        end
      end
    end

The bin/rails file loks like this:

    #!/usr/bin/env ruby
    APP_PATH = File.expand_path('../../config/application',  __FILE__)
    require_relative '../config/boot'

    # Set default host and port to rails server
    if ARGV.first == 's' || ARGV.first == 'server'
      require 'rails/commands/server'
      module Rails
        class Server
          def default_options
            super.merge(Host:  '0.0.0.0', Port: 3000)
          end
        end
      end
    end

    require 'rails/commands'

See http://stackoverflow.com/a/32476858/132235

@wembernard
Copy link

wembernard commented Sep 6, 2016

Did the trick for me. Just in case you're looking for a better solution: -b127.0.0.1 will protect you from the out world :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests