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

4

5

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.

Rarefy answered 26/10, 2014 at 6:59 Comment(2)
Is your host forwarding port 3000 to your Vagrant box? These are, technically, different machines, hence different network nodes. If not, you're hitting the wrong machine.Mireillemireles
Yes @D-side. Other projects within the guest environment are able to work just fine. This seems mostly related to the highest rated response below, but trying to find a less clunky solution now.Rarefy
C
17

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.

Charley answered 26/10, 2014 at 12:32 Comment(4)
Thanks for the additional information. Per your post and katafrakt's earlier post, this seems to be the issue. That said, the workaround is a PITA. Any shortcuts for making it do the rails s -b 0.0.0.0 by default when I do rails s?Rarefy
Update: Looks like if I edit Rack's /lib/rack/server.rb file to revert this commit rails s works like normal. Is there a "safer" way to override this bit with something in my actual project folder or within RVM? I feel a bit dirty modifying the main Rack file like that, and I'm guessing it would easily get overwritten.Rarefy
See this question, although it is for Rails 3. Try with Host instead of Port.Charley
This is actually a very nice security feature, especially for those who like to work in cafes and other public places. It insures no one else can access your Rails app when it is running on localhost.Mercurate
H
4

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.

Hiles answered 26/10, 2014 at 9:53 Comment(2)
Trying rails s -b 0.0.0.0 did the trick with letting me access it via browser. However that is kind of a pain to have to type every time. Couldn't find this in a quick Googling, but is there an easy way to change this default? Alternatively, is there any way to access http://127.0.0.1:3000 from inside a VM?Rarefy
I'm not aware of any non-hackish way to accomplish it.Hiles
A
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
Anarthria answered 17/2, 2015 at 1:18 Comment(0)
L
0

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'
Luteous answered 26/10, 2014 at 7:12 Comment(4)
I'm having problems with the 'pg' gem, and when I try this without it, I get the same issue as before. PG gives me a bunch of errors that seem to relate to PostgreSQL. I was planning on using sqlite3.Rarefy
Also, presumably Hartl tested things before he published, and others seem to be able to follow his instructions fine on that step. So I'm curious why you think the explicit version declarations are the problem, and changing that + introducing new gems is the solution...Rarefy
If Postgres is not installed, remove the gem.Luteous
The whole idea of a gemfile is that it keeps itself up to date with bundle. I am just guessing that gem conflict is your problem.Luteous

© 2022 - 2024 — McMap. All rights reserved.