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

允我心安 提交于 2019-12-05 09:39:45

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.

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.

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

Modify your gemfile to something like this and run bundle update. The versions you have specified are explicit. New hello_world worked for me if I did not paste in your gemfile.

gem 'rails', '~> 4.2.0.beta2'
gem 'pg'
gem 'bootstrap-sass', '~> 3.2.0'
gem 'sass-rails', '~> 5.0.0.beta1'
gem 'font-awesome-sass', '~> 4.2.0'
gem 'sprockets-rails', '~> 3.0.0.beta1'
gem 'coffee-rails', '~> 4.0.0'
gem 'uglifier', '>= 1.0.3'
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!