TheJediCowboy
TheJediCowboy

Reputation: 9222

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.

Upvotes: 1

Views: 929

Answers (2)

blacklabelops
blacklabelops

Reputation: 4958

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

Upvotes: 4

Ruaidhrí Primrose
Ruaidhrí Primrose

Reputation: 1250

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.

Upvotes: 1

Related Questions