Vagrant and NGINX only works on ports other than 80

北慕城南 提交于 2019-12-11 11:24:14

问题


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.


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/31369480/vagrant-and-nginx-only-works-on-ports-other-than-80

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!