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

半城伤御伤魂 提交于 2020-01-13 10:29:16

问题


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.


回答1:


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.




回答2:


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.




回答3:


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



回答4:


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'


来源:https://stackoverflow.com/questions/26570609/rails-4-2-0-beta2-cant-connect-to-localhost

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!