Port forwarding vagrant on windows

2019-05-10 03:00发布

问题:

I am on windows 7. I have a vagrantfile specified to forward ports for passenger, on vagrant up it claims that it has forwarded the ports, however this is not the case, when I start my application I can run

curl 0.0.0.0:3000

and get a result from my rails application, however outside vagrant I cannot connect to the server. How can I access the port from outside vagrant?

The Vagrantfile contains the following lines

config.vm.network :forwarded_port, guest: 3306, host: 3306 # MySQL

config.vm.network :forwarded_port, guest: 3000, host: 3000 # Passenger

回答1:

One possibility is that port collision is occurring, eg. something is running on port 3000 of your host machine. According to the Port Forwarding documentation

you can add the auto_correct: true flag which will output any collision and autocorrect details when you vagrant up or vagrant reload.



回答2:

You can open Virtual Box and look at the network properties of your VM, then select port forwarding. You will see where your ports actually map to.

By default Vagrant uses 10.* IP addresses. Depending on your dev environment, VPN, antivirus software, your local ports may be blocked.

# Create a private network, which allows host-only access to the machine
config.vm.network :public_network, ip: "111.222.33.4", :bridge => 'en0: Wi-Fi (AirPort)'

# Create a forwarded port mapping which allows access to a specific port
config.vm.network "forwarded_port", guest: 5985, host: 15985, auto_correct: true # winrm
config.vm.network "forwarded_port", guest: 3389, host: 13390, auto_correct: true # remote desktop
config.vm.network "forwarded_port", guest: 4000, host: 4000, auto_correct: true # chef-zero

Above has been working for me in many 'hostile' dev environments.