Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 4.2.0.beta2 - Can't connect to LocalHost?

I installed Rails 4.2.0.beta2 per the instructions in RailsTutorial.org 3rd Edition (the one that just came out). I'm not using the cloudIDE and am instead using Ubuntu Trusty 32 via Vagrant on a Windows 7 host with RVM.

Did rails _4.2.0.beta2_ new hello_app and then pasted in his gemfile sample.

After that, I ran:

$ bundle install
$ rails s

Server starts fine, however when I try to connect to localhost:3000 I get "Server Not Found"

Weirder still, I have a couple other Rails starter projects I've been tinkering with that use Rails 4.0.3 and 4.1.6 and I'm able to connect to the server there just fine.

What am I missing here? Why can't my browser connect when I've created a new Rails project with the latest version, but it works fine with older versions?

Also, I tried wget http://0.0.0.0:3000 and while it connected and received a 200 response, the length was unspecified, whereas in another brand new Rails app under an old version, I would get the actual file size of whatever index.html was.

like image 349
Haltingpoint Avatar asked Oct 26 '14 06:10

Haltingpoint


3 Answers

Regarding inaccessible server, from the Rails 4.2 release notes:

3.3 Default host for rails server

Due to a change in Rack, rails server now listens on localhost instead of 0.0.0.0 by default. This should have minimal impact on the standard development workflow as both http://127.0.0.1:3000 and http://localhost:3000 would continue to work as before on your own machine.

However, with this change you would no longer be able to access the Rails server from a different machine (e.g. your development environment is in a virtual machine and you would like to access it from the host machine), you would need to start the server with rails server -b 0.0.0.0 to restore the old behavior.

If you do this, be sure to configure your firewall properly such that only trusted machines on your network can access your development server.

127.0.0.1:3000 will only allow connections from that address on port 3000, whereas 0.0.0.0:3000 will allow connections from any address at port 3000.

Since Rails 4.2 only accepts connections from localhost by default, you can only access the server from localhost (eg. inside the VM); connections from another machine (eg. VM's host) will not work.

You must use the "old behavior" method described above to allow connections from the VM host.


Regarding unspecified content length, that depends on the web server in use. I assume it is using chunked encoding which does not send content length. Assets will have content length, but not HTML.

like image 120
Substantial Avatar answered Oct 19 '22 20:10

Substantial


Rails 4.2 by default binds to 127.0.0.1:3000, instead of 0.0.0.0:3000 in earlier versions. If you have other Rails project working with your configuration, try to start a server with explicit host: rails s -b 0.0.0.0.

like image 4
katafrakt Avatar answered Oct 19 '22 20:10

katafrakt


A guy named tostasqb posted a very interesting workaround on github to make the old behavior (Rails version < 4.2) the default.

Just edit your config/boot.rb file and add these lines:

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
like image 3
Fernando Vieira Avatar answered Oct 19 '22 21:10

Fernando Vieira