samuelkadolph / unicorn-rails

unicorn-rails is a simple gem that sets the default server for rack (and rails) to unicorn.
MIT License
127 stars 22 forks source link

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

Open brucehsu opened 9 years ago

brucehsu commented 9 years ago

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 commented 9 years ago

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 commented 9 years ago

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 commented 9 years ago

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 commented 9 years ago

@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 commented 9 years ago

@tostasqb Thanks! :+1:

jsoohouse commented 9 years ago

@tostasqb Thank You!!!

Taroka commented 9 years ago

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 commented 9 years ago

:+1: - 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 commented 9 years ago

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 commented 9 years ago

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 commented 8 years ago

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 commented 8 years ago

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 :)