46

I have a Vagrant VM with Rails installed with a sample app. The VM is configured to forward the port 3000 (of Rails Webrick server) to my host 3000 port.

config.vm.network "forwarded_port", guest: 3000, host: 3000

Everything is configured as seen in a lot of examples.

But, when I try to access http://localhost:3000 nothing happens. I've also tried to forward to other random ports like 8081, 25600, without success. Doing a curl request also does not get anything (just a Connection reset by the peer message), and a curl request inside VM works perfectly (as expected).

Both my PC and my VM runs Ubuntu 12.04. I'm using Ruby 2.2.0 and Rails 4.2.0.

An important point is that Apache works normally. I forwarded the port 80 to port 8080 and everything works. It seems that the problem is just with the Rails server, even if when I use other ports (rails server -p 4000, for example)

Max Woolf
  • 3,988
  • 1
  • 26
  • 39
Victor Leal
  • 1,055
  • 2
  • 12
  • 28

5 Answers5

91

Rails 4.2 now binds to 127.0.0.1 by default and not 0.0.0.0.

Start the server using bin/rails server -b 0.0.0.0 and that should sort it.

Max Woolf
  • 3,988
  • 1
  • 26
  • 39
  • I'm gonna do it! Every server should use the 0.0.0.0 when inside a VM? I didn't know about that... – Victor Leal Jan 06 '15 at 13:18
  • No, this isn't the most secure way to do it. 0.0.0.0 just means listen for any address. The change means that it will only respond to requests from the VM, not your host machine. It's fine for development, do not do it in production. – Max Woolf Jan 06 '15 at 13:19
  • I've encountered the same problem. What recommendations do you have for setting up the production environment? Is there a specific IP rails should bind to? – helsont Jan 14 '15 at 01:29
  • You shouldn't be using webbrick in production, it's only designed for development. – Max Woolf Jan 14 '15 at 08:43
  • Thanks! Has this problem using PHP Cake 3.X, and this solved it! – Jonas Lomholdt Jul 06 '16 at 14:42
  • Thanks! Had this issue using Symphony and this solved i! – rnpd Oct 17 '17 at 14:35
10

To run on a specific port :

rails server -b 0.0.0.0 -p 8520
errakeshpd
  • 2,544
  • 2
  • 28
  • 35
4

Use:

rails s -b 0.0.0.0

or

Add to config/boot.rb:

require 'rails/commands/server'

module Rails
  class Server
    new_defaults = Module.new do
      def default_options        
        default_host = Rails.env == 'development' ? '0.0.0.0' : '127.0.0.1'
        super.merge( Host: default_host )
      end
    end

    # Note: Module#prepend requires Ruby 2.0 or later
    prepend new_defaults
  end
end

and work with rails s

shilovk
  • 11,718
  • 17
  • 75
  • 74
0

You can use an alias, on Ubuntu put it in ~/.bash_aliases
I use:
alias rs="rails server -b 0.0.0.0"

You have to reload the terminal before you can use it

noname
  • 1
0

Really nice explanation found here: Rails 4.2.0.beta2 - Can't connect to LocalHost?

I had exactly the same problem, with the exception that my PC is Mac machine. I've used this vagrantfile to get it working (with virtualbox 4.3.36)

# -*- mode: ruby -*-
# vi: set ft=ruby :

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  # Use Ubuntu 14.04 Trusty Tahr 64-bit as our operating system
  config.vm.box = "ubuntu/trusty64"

  # Configurate the virtual machine to use 2GB of RAM
  config.vm.provider :virtualbox do |vb|
    vb.customize ["modifyvm", :id, "--memory", "2048"]
  end

  config.vm.provision "shell", inline: <<-SHELL
    ## Install necessary dependencies
    sudo apt-get --assume-yes install libsqlite3-dev libcurl4-openssl-dev git

    ## Install GPG keys and download rvm, ruby and rails
    curl -sSL https://rvm.io/mpapis.asc | gpg --import -
    curl -L https://get.rvm.io | bash -s stable --ruby
    curl -L https://get.rvm.io | bash -s stable --rails
    echo "[[ ls \"$HOME/.rvm/scripts/rvm\" ]] && . \"$HOME/.rvm/scripts/rvm\"" >> ~/.profile
    ## Adding vagrant user to the group that can access rvm
    usermod -G rvm vagrant
  SHELL

  # Forward the Rails server default port to the host
  config.vm.network :forwarded_port, guest: 3000, host: 3000

end

after having the VM up and running, I would run bundle install in my project repo and then rails server -b 0.0.0.0. As pointed out in linked answer above:

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.

Community
  • 1
  • 1
da-na
  • 250
  • 2
  • 8