connection refused from host machine - nginx

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

Related

How to know the forwarding port

I have brought a VM using vagrant.In the config vagrant file, I have given
config.vm.network "forwarded_port", guest: 830, host: 8300.
I'm able to ssh in to the VM by
ssh -p 2223 vagrant#localhost
What if i want to spawn multiple VMs of same kind.How to configure the forwarding ports
You can use auto_correct parameter (see https://www.vagrantup.com/docs/networking/forwarded_ports.html); in case of port collision vagrant can assign another port
config.vm.network "forwarded_port", guest: 830, host: 8300, auto_correct: true
you can check vagrant port to displays information about guest port mappings. The command makes a warning about the value
The forwarded ports for the machine are listed below. Please note that
these values may differ from values configured in the Vagrantfile if
the provider supports automatic port collision detection and
resolution.
just use vagrant ssh to ssh-in into the VM, vagrant will know which port to use.

vagrant Multipe networking

I have installed magento 2 in vagrant with in docker machine, this docker machine have port forwarding concepts, I set private network, with nat and host-only, Now only access magento 2 in hostmachine.
I need to access locally connected remote machine also so, i try to change private network to public network with bridge.
Vagrant File:
Vagrant.configure("2") do |config|
config.vm.box = "machine"
config.ssh.username = "vagrant"
config.vm.hostname = "www.myhost.net"
config.ssh.forward_agent = "true"
config.vm.network "public_network", ip: "192.168.56.40"
config.vm.provider :virtualbox do |vb|
vb.customize ["modifyvm", :id, "--memory", "2048"]
end
if Vagrant::Util::Platform.windows?
config.vm.synced_folder ".", "/vagrant", :mount_options => ["dmode=777", "fmode=777"]
else
config.vm.synced_folder ".", "/vagrant", :nfs => { :mount_options => ["dmode=777", "fmode=777"] }
end
end
But, throw
NFS requires a host-only network to be created.
Please add a host-only network to the machine (with either DHCP or a
static IP) for NFS to work.
I need to add Multiple Network to vagrant
nat
host-onloy(for nfs)
bridge (for access remote machine)
Suggest me How to resolve this.
You need to change your public_network to private_network for nfs to work
If you are using the VirtualBox provider, you will also need to make sure you have a private network set up. This is due to a limitation of VirtualBox's built-in networking. With VMware, you do not need this.
so :
you can change to VMWare (but you have some additional fees)
you do not use nfs
you can setup another network interface for bridge and use this network interface if you need to connect to the remote machine, you should be able to ping (ping -I ethX mylocalmachine) but I am not sure how to work to get connection in

ubuntu VM with wordpress install redirects unexpectedly ( port forwarding 80 to 8080?)

I've got an Ubuntu 14 VM running on a windows 10 host.
I've installed nginx and using
config.vm.network "forwarded_port", guest: 80, host: 8080
in my Vagrantfile I can see the default nginx page when I visit
127.0.0.1:8080 in my browser on the host machine.
With that working I installed wordpress in a folder so that it would appear as
http://127.0.0.1:8080/aqua/
that worked as well and the wordpress site is visible at the http://127.0.0.1:8080/aqua/ address in Windows 10
But a strange thing happens in my VM. I can wget the root of the server without an issue.
wget localhost/
--2016-03-23 21:30:54-- http://localhost/
Resolving localhost (localhost)... 127.0.0.1
Connecting to localhost (localhost)|127.0.0.1|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 12 [text/html]
Saving to: ‘index.html.3’
100%[======================================>] 12 --.-K/s in 0s
2016-03-23 21:30:54 (295 KB/s) - ‘index.html.3’ saved [12/12]
If I wget the 'aqua' subfolder where the wordpress is installed
wget localhost/aqua
--2016-03-23 21:32:28-- http://localhost/aqua
Resolving localhost (localhost)... 127.0.0.1
Connecting to localhost (localhost)|127.0.0.1|:80... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: http://localhost/aqua/ [following]
--2016-03-23 21:32:28-- http://localhost/aqua/
Reusing existing connection to localhost:80.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: http://localhost:8080/aqua/ [following]
--2016-03-23 21:32:28-- http://localhost:8080/aqua/
Connecting to localhost (localhost)|127.0.0.1|:8080... failed: Connection refused.
Resolving localhost (localhost)... 127.0.0.1
Connecting to localhost (localhost)|127.0.0.1|:8080... failed: Connection refused.
I get redirected to port 8080.
both of these wget's are on the guest ubuntu machine.
Could this be a symptom of my Vagrantfile?
# -*- mode: ruby -*-
# vi: set ft=ruby :
# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure(2) do |config|
# The most common configuration options are documented and commented below.
# For a complete reference, please see the online documentation at
# https://docs.vagrantup.com.
# Every Vagrant development environment requires a box. You can search for
# boxes at https://atlas.hashicorp.com/search.
config.vm.box = "ubuntu/trusty64"
# Disable automatic box update checking. If you disable this, then
# boxes will only be checked for updates when the user runs
# `vagrant box outdated`. This is not recommended.
# config.vm.box_check_update = false
# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine. In the example below,
# accessing "localhost:8080" will access port 80 on the guest machine.
config.vm.network "forwarded_port", guest: 80, host: 8080
# Create a private network, which allows host-only access to the machine
# using a specific IP.
# config.vm.network "private_network", ip: "192.168.33.10"
# Create a public network, which generally matched to bridged network.
# Bridged networks make the machine appear as another physical device on
# your network.
# config.vm.network "public_network"
# Share an additional folder to the guest VM. The first argument is
# the path on the host to the actual folder. The second argument is
# the path on the guest to mount the folder. And the optional third
# argument is a set of non-required options.
# config.vm.synced_folder "../data", "/vagrant_data"
# Provider-specific configuration so you can fine-tune various
# backing providers for Vagrant. These expose provider-specific options.
# Example for VirtualBox:
#
# config.vm.provider "virtualbox" do |vb|
# # Display the VirtualBox GUI when booting the machine
# vb.gui = true
#
# # Customize the amount of memory on the VM:
# vb.memory = "1024"
# end
#
# View the documentation for the provider you are using for more
# information on available options.
# Define a Vagrant Push strategy for pushing to Atlas. Other push strategies
# such as FTP and Heroku are also available. See the documentation at
# https://docs.vagrantup.com/v2/push/atlas.html for more information.
# config.push.define "atlas" do |push|
# push.app = "YOUR_ATLAS_USERNAME/YOUR_APPLICATION_NAME"
# end
# Enable provisioning with a shell script. Additional provisioners such as
# Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
# documentation for more information about their specific syntax and use.
# config.vm.provision "shell", inline: <<-SHELL
# sudo apt-get update
# sudo apt-get install -y apache2
# SHELL
end
I can't see anything there that would cause a redirect on the guest machine.
Wordpress is causing a redirect on the guest machine but I'm still able to see the site on the host machine.
In my experience this is due to wordpress and its rewrite rules and wp_config DB table. The site_url and home rows probably have the port specified in them, which is causing this unexpected behaviour.

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.

Can Multiple Vagrant VMs communicate by VM hostname?

I am managing multiple VMs with Vagrant. Networks are configured as private, ip addresses have been set and hostnames are assigned. As shown in the Vagrantfile below.
The VMs can communicate with each other via the IP address, but I would like to know how to allow VMs to communicate using their assigned hostname. I.e. How to make ping comtest2 work from comtest1?
Vagrantfile:
Vagrant.configure("2") do |config|
config.vm.define "comtest1" do |comtest1|
comtest1.vm.box = "precise32"
comtest1.vm.hostname = "comtest1"
comtest1.vm.network "private_network", ip: "192.168.10.21"
end
config.vm.define "comtest2" do |comtest2|
comtest2.vm.box = "precise32"
comtest2.vm.hostname = "comtest2"
comtest2.vm.network "private_network", ip: "192.168.10.22"
end
end
Cheat the dns resolution with https://github.com/adrienthebo/vagrant-hosts ?
You can use Zeroconf. It broadcasts the host name in network and makes it available to the other hosts on the local network. That way you can access your hosts using test1.local, test2.local, etc.
Just install avahi-daemon and libnss-mdns!
Example
Vagrantfile:
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/bionic64"
config.vm.define "vm1" do |machine|
machine.vm.hostname = "vm1"
machine.vm.network "private_network", type: "dhcp"
end
config.vm.define "vm2" do |machine|
machine.vm.hostname = "vm2"
machine.vm.network "private_network", type: "dhcp"
end
# allow guests to reach each other by hostname
config.vm.provision "allow_guest_host_resolution",
type: "shell",
inline: <<-SHELL
apt update
apt install -y avahi-daemon libnss-mdns
SHELL
end
Test
$ vagrant up
...
$ vagrant ssh vm1 -- ping -c 1 vm2.local
PING vm2.local (172.28.128.8) 56(84) bytes of data.
64 bytes from 172.28.128.8 (172.28.128.8): icmp_seq=1 ttl=64 time=0.333 ms
$ vagrant ssh vm2 -- ping -c 1 vm1.local
PING vm1.local (172.28.128.7) 56(84) bytes of data.
64 bytes from 172.28.128.7 (172.28.128.7): icmp_seq=1 ttl=64 time=0.254 ms
It isn't the most elegant solution in the world but it is very simple, how about something like:
Vagrant.configure("2") do |config|
config.vm.define "comtest1" do |comtest1|
comtest1.vm.box = "precise32"
comtest1.vm.hostname = "comtest1"
comtest1.vm.network "private_network", ip: "192.168.10.21"
comtest1.vm.provision "shell", inline: <<-SHELL
sed -i '$ a 192.168.10.22 comtest2' /etc/hosts
SHELL
end
config.vm.define "comtest2" do |comtest2|
comtest2.vm.box = "precise32"
comtest2.vm.hostname = "comtest2"
comtest2.vm.network "private_network", ip: "192.168.10.22"
end
end
If the host resolves DNS correctly, then you can configure Virtualbox to use the host as the DNS resolver.
config.vm.provider :virtualbox do |vb|
vb.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
end
See https://serverfault.com/a/506206/250071
We use a local Ansible task to automatically add the provisioned box to the host /etc/hosts file. It is a little awkward, but has been very robust.
- setup:
gather_subset: [network]
- name: Add host mapping to local /etc/hosts
delegate_to: 127.0.0.1
lineinfile: dest=/etc/hosts regexp=".+{{ vm.hostname }}$" line="{{ ansible_all_ipv4_addresses|sort|last }} {{ vm.hostname }}"
Check out landrush on Github.
It will setup a DNS for your vagrant private network.

Resources