Search code examples
ruby-on-railsvagrantubuntu-14.04portforwarding

Vagrant: Cannot access Rails server via localhost:8080


My Vagrantfile contains:

config.vm.box = "ubuntu/trusty64"
config.vm.network "forwarded_port", guest: 3000, host: 8080

vagrant up reports:

Bringing machine 'default' up with 'virtualbox' provider...
==> default: Forwarding ports...
    default: 3000 => 8080 (adapter 1)
...

Using a non-privileged user (deploy') I runrails server` which reports what you'd expect:

Booting WEBrick => Rails 4.2.5 application starting in development on http://localhost:3000
[2015-12-01 13:34:41] INFO  WEBrick::HTTPServer#start: pid=1638 port=3000

Inside of the vagrant vm $ netstat -ntlp reports:

tcp        0      0 127.0.0.1:3000          0.0.0.0:*               LISTEN      1638/ruby

so I know that the Rails server is definitely listening on port 3000

When I browse to http://localhost:8080 on my host machine (OS X) I get an ERR_EMPTY_RESPONSE.

I've tried stopping/starting the vm, reloading it, etc. Am I missing something obvious?

Thank you in advance


Solution

  • In the /etc/hosts, associate 0.0.0.0 with localhost. When a service is listening on 0.0.0.0 this means the service is listening on all the configured network interfaces, when listening on 127.0.0.1 the service is only bound to the loopback interface (only available on the local machine).

    In /etc/hosts on the guest machine, replace 127.0.0.1 localhost with 0.0.0.0 localhost.

    Then restart the rails server.