Vagrant network - networking

I have a vagrant box:
Vagrant.configure(2) do |config|
config.vm.box = "parallels/centos-7.1"
config.vm.box_check_update = false
config.vm.hostname = "vagranthost"
config.vm.network "forwarded_port", guest: 80, host: 8080
end
Guest machine gets ip address 10.211.55.17. Sometimes last number changes, but that doesn't matter.
Host machine is always 10.211.55.2.
There is nginx inside the box:
server {
listen 80;
server_name mill.localhost;
...
}
And simple php script: echo $_SERVER['REMOTE_ADDR'];
So, I'm accessing this box from my host machine.
If I access that script from the browser as http://mill.localhost:8080/, I'm getting 10.211.55.1.
I can make curl -H "Host:mill.localhost" http://10.211.55.17/, and I'll get excpected result: 10.211.55.2.
So, the questions are: why is this happening? What is that node 10.211.55.1 in the network? Is there any way, I can get 10.211.55.2 from the browser (http://mill.localhost:8080/)?

Related

vagrant how to access web server (nginx) with public ip

I have tried doing it like this in my vagrantfile
config.vm.network "private_network", ip : "192.168.33.15"
so when I start my box, I could access nginx through my browser 'locally'.
what I want is to access it using public ip. I have tried this, (found in documentation)
config.vm.network "public_network", ip: "202.137.x.x", netmask: "255.255.x.x"
config.vm.provision "shell",
run: "always",
inline: "route add default gw 202.137.x.x"
config.vm.provision "shell",
run: "always",
inline: "eval `route -n | awk '{ if ($8 ==\"eth0\" && $2 != \"0.0.0.0\") print \"route del default gw \" $2; }'`"
this still doesn't work. any ideas?
*edit
I've also tried port forwarding,
config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "202.137.x.x"
I have to do this: 202.137.x.x:8080, I wanted to access without the port. Also, host port cannot be < 1024, so I cannot put port 80..
I've tried doing port forwarding in windows (host), so this is how I solved it..
netsh interface portproxy add v4tov4 listenport=80 listenaddress=202.137.x.x connectport=8080 connectaddress=202.137.x.x
This forwards any requests to the host on port 80 to port 8080 on the host.
So, the port forwarding flow becomes:
host:80 => host:8080 => guest:80
source here

connection refused from host machine

I am using vagrant with virtualbox as provider. Within my guest system I have nginx installed and configured.
nginx is serving some static files from a folder and exposing them on port 80. That works fine. If I call curl localhost within the guest machine I get the answer I was supposed to receive.
I have a very simple vagrantfile, which you can see below. I forward port 80 to port 8080, but from the host machine I cant access that page via localhost:8080.
I already disabled the firewall in the guest machine without any success.
Vagrant.configure("2") do |config|
# VirtualBox Settings: Give it a little bit more memory
config.vm.provider :virtualbox do |vb|
vb.customize ["modifyvm", :id, "--memory", "768"]
end
# Base Image: CentOS 7.0 x86_64
config.vm.box = "jayunit100/centos7"
# Use Vagrant's default insecure key (~/.vagrant.d/insecure_private_key)
config.ssh.insert_key = false
# Add port forwarding for node-inspector
config.vm.network :forwarded_port, guest: 80, host: 8080 # node-inspector
# Map project directory
config.vm.synced_folder ".", "/server/"
# Provisioning Shell Script
config.vm.provision :shell, :path => "vagrant-setup/base.sh"
end
If I call curl -v 'http://localhost:8080' from the host system I get told that the connection got refused. Any idea what I could do?
I had to disable my firewall on the host machine with iptables -F

Vagrant and NGINX only works on ports other than 80

For the purpose of this post, I am using Vagrant to launch NGINX (through Docker, but that is not important I don't think).
My Vagrant looks like the following:
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
#Assign Box and VM Properties
config.vm.box = "ubuntu/trusty64"
config.vm.provider "virtualbox" do |v|
v.memory = 1024
v.cpus = 2
end
# Network
config.vm.network "forwarded_port", guest:80, host: 80 #--> DOESN'T WORK localhost
config.vm.network "forwarded_port", guest:80, host:8391 #--> WORKS localhost:8391
# Provision
config.vm.provision :shell, inline: "sudo apt-get update"
config.vm.provision :docker
end
The goal is to be able to hist NGINX on localhost and not localhost:8391
I KNOW that NGINX is listening on 80 because of the mapping, and from running CURL within Vagrant.
You can use setcap to enable to use ports under 1024 for non-root users for specific binaries.
This only works under Linux and must be applied to the Vagrant box, to use Port 80 inside the box, and your host, to use Port 80 on your host.
You need the package libcap2-bin, e.g. with apt:
sudo apt-get install libcap2-bin
sudo setcap cap_net_bind_service=+ep /path/to/nginx-binary
Afterwards NGINX is allowed to use Port 80 inside the box as user vagrant. Now enable setup for Vagrant on your host.
sudo setcap cap_net_bind_service=+ep /path/to/vagrant-binary
In general you can't bind to ports 1024 or under on the host when using Vagrant, unless you run it as root. (As with other apps, it's obviously not recommended to run Vagrant as root.)
As an alternative, if you don't need to connect to "localhost" specifically you could try setting up a private network so your Vagrant box has a separate IP address. See http://docs.vagrantup.com/v2/networking/private_network.html for more info. That should let you connect to port 80 on that IP fine.

Vagrant::Errors::NetworkCollision: The specified host network collides with a non-hostonly network

I'm using vagrant with multiple machine, it's ever worked normally, but then it just doesn't work again.
My part of Vagrantfile that defining the network:
config.vm.define "app" do |layer|
layer.vm.provision "chef_solo", id:"chef" do |chef|
.....
end
# Forward port 80 so we can see our work
layer.vm.network "forwarded_port", guest: 80, host: 9999
layer.vm.network "private_network", ip: "10.10.10.10"
end
It's a standar configuration and I only have that vm. But when I tried to vagrant up, it shows error:
Vagrant::Errors::NetworkCollision: The specified host network collides with a non-hostonly network!
This will cause your specified IP to be inaccessible. Please change
the IP or name of your host only network so that it no longer matches that of
a bridged or non-hostonly network
How to fix it?
Apparently my office just changed the network configuration. By changing 10.10.10.10. to other blok (e.g 10.0.0.100), it workedagain. Sorry.

browser in host can not see vagrant box, portforward does not work

I have installed Vagrant in my Window XP, and in my Vagrantfile I have:
Vagrant::Config.run do |config|
# Setup the box
config.vm.box = "lucid32"
config.vm.forward_port 80, 8080
config.vm.network :hostonly, "192.168.10.200"
end
But I see no sign of my vagrant box when I type "http://192.168.10.200:8080" in browser.
IP address of the virtual box is correct, because from within the vbox, I have:
vagrant#lucid32:~$ ifconfig
....
eth1 Link encap:Ethernet HWaddr 08:00:27:79:c5:4b
inet addr:192.168.10.200 Bcast:192.168.10.255 Mask:255.255.255.0
There seem to be no firewall problem because if I type
vagrant#lucid32:~$ curl 'http://google.com'
it works fine.
I have read Vagrant's port forwarding not working
and tried:
vagrant#lucid32:~$ curl 'http://localhost:80'
curl: (7) couldn't connect to host
and also
vagrant#lucid32:~$ curl 'http://localhost:8080'
curl: (7) couldn't connect to host
So, looks like port forward is not working...
If you know what I can do so I can access my vbox from host browser, can you help me?
Thanks in advance
If you just started a Vagrant box with this Vagrantfile, there is nothing more than an empty Ubuntu Lucid, which does not run any service yet. So there is nothing served on port 80, this is why there is nothing to see either from inside the box on port 80 or the host machine on 8080.
For you Vagrant machine to provide some services (such as a web server on port 80), you have to do some provisioning. You can do it manually or using Chef or Puppet which are hooked into Vagrant's up process.
I had a similar problem. Sometimes using port forwarding for ports below 2000 is a problem. What worked for me is choosing ports that are above 2000. So my vagrantfile now looks like:
config.vm.network :forwarded_port, host: 4500, guest: 9000
Typing localhost:4500 on my host machine now just works fine. It seems like you are on an older version of vagrant than mine, so you can edit your vagrant file to something like
config.vm.forward_port 9000, 4500
Now typing localhost:4500 on your host machine should work fine.
Good luck,

Resources