6

I am very new to using Vagrant in my development workflow, however when setting up the box using vagrant up and then accessing it via my host i get a connection refused with my browser.

Is all that needs to be done to work is: vagrant init scotch/box vagrant up

?

  • What OS are you running inside the Vagrant box? If Linux, and it's a Red Hat or CentOS distro, then you probably need to configure iptables to allow incoming web connections. – Brian Showalter Sep 17 '15 at 13:19
  • OK, that's Linux, Ubuntu distro, so iptables is likely not an issue. What port are you hitting with your browser? Since that box is running node.js, it may be that the web server is listening on port 3000, not 80 or 8080, and you'll need to configure your port forwarding accordingly or hit http://192.168.33.10:3000. – Brian Showalter Sep 17 '15 at 13:29

4 Answers4

7

Make sure to forward the 80 port from the guest so you can access the vm from your browser. Edit your Vagrantfile and make sure to have a line like (by default when doing vagrant init I believe this is commented)

config.vm.network "forwarded_port", guest: 80, host: 8080

You can then access your web server (if running on the VM) from http://127.0.0.1:8080 or http://localhost:8080

If you prefer to use a fixed private IP, you will need to add

config.vm.network :private_network, ip: "192.168.33.10"

you will then access the vm server using http://192.168.33.10

note:

  • if you have nothing running on the port 80 nothing will be displayed (obviously). you can run sudo netstat -ant and check you have a process running on port 80

  • Adjust the port number from the example with the service you're running if it runs on another port.

Frederic Henri
  • 51,761
  • 10
  • 113
  • 139
  • i dont mean inside the actual box i mean like outside on my host. Does the same apply? –  Sep 17 '15 at 13:02
  • yes it applies for your host, the goal is not to access the vm directly but accessing from your host – Frederic Henri Sep 17 '15 at 13:05
  • Thanks i will give that a go just doing a postinstall atm –  Sep 17 '15 at 13:08
  • Is it possible to use a specific ip instead of localhost eg 192.168.33.10? –  Sep 17 '15 at 13:25
  • @Elevant giving an example with fixed IP – Frederic Henri Sep 17 '15 at 13:30
  • Thanks for the example of the fixed IP i have tried it with no avail. I vagrant init and then vagrant up and postinstall.sh is there specific setup i need to do in vm or my host? my host already had a past apache/mysql/php setup before vagrant is that interfering? –  Sep 18 '15 at 08:33
  • can you check you can access your site from the vm (curl or something) ? do you have specific http.conf for apache ? – Frederic Henri Sep 18 '15 at 08:35
  • no i did a restore of my http.conf but of note is that on the vm i cannot find apache or nginx just has node and stuff setup? –  Sep 18 '15 at 09:36
  • run `sudo netstat -ant` and check you have something running on port 80, you should have something like `0 0.0.0.0:80` – Frederic Henri Sep 18 '15 at 09:39
  • I think it was just related to the specific box i have it working now. Thanks. –  Sep 18 '15 at 11:49
0

By default, you get a NAT interface that you cannot connect into. You should define a private network in vagrant to make incoming connections available. Then, also check your VM's firewall settings.

Hellmar Becker
  • 2,824
  • 12
  • 18
  • I did uncomment the private network and used that ip in my browser. –  Sep 17 '15 at 13:17
0

I had a similar problem and just wanted to share my solution, maybe it helps someone else. I couldnt reach the localhost:8080 via browser. The connection got interrupted everytime. After a long wasted time and search, I found my problem, it was due to the nginx.conf file.

#nginx config file
server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;
    listen localhost;

    root /usr/share/nginx/html;
    index index.html index.htm;

    server_name localhost;

    location \ {
        try_files $uri $uri/ = 404;
    }
}

i forgot the backslash after location.... after adding it, i could restart my nginx via vagrant ssh and now it's working again

best totem

0

There are some provider-related issues when it comes to networking, especially with Hyper-V, that the get-started docs don't mention. See https://developer.hashicorp.com/vagrant/docs/providers/hyperv/limitations

Soyal7
  • 381
  • 5
  • 10