我无法访问 vagrant box shell 之外的转发端口。
我创建了一个带有以下转发端口的流浪盒。
config.vm.network :forwarded_port, guest: 80, host: 8080, auto_correct: true
config.vm.network "forwarded_port", guest: 27017, host: 27017 #mongodb
config.vm.network "forwarded_port", guest: 5000, host: 5000 #python flask port
config.vm.network "forwarded_port", guest: 5050, host: 5050 # NODE Port
config.vm.network "private_network", ip: "192.168.33.10"
我已经安装了python服务器和nodejs服务器。如果我 ssh 到我的 vagrant box 并尝试:
curl http://localhost:5050
它会正确返回我的 nodejs hello world。如果我curl http://localhost:5000
正确的话我的python3 hello world。
但是,我无法在 vagrant 环境之外访问我的本地主机(vagrant 中的服务器仍然处于活动状态)
我做错了什么?
我正在设置一台新机器 - 所以我可能没有在本地机器上安装一些必需的东西,但我已经安装了 vagrant 等的依赖项。
Ping localhost 或 127.0.0.1 可以工作,但只能在没有端口的情况下进行。我在 vagrant 外部的主机的终端中收到以下错误:
$ ping localhost:80
ping: cannot resolve localhost:80: Unknown host
$ ping localhost:8080
ping: cannot resolve localhost:8080: Unknown host
$ curl 'http://localhost:80'
curl: (7) Failed to connect to localhost port 80: Connection refused
$ curl 'http://localhost:8080'
curl: (56) Recv failure: Connection reset by peer
$ curl 'http://localhost:5000'
curl: (56) Recv failure: Connection reset by peer
$ curl 'http://localhost:5050'
curl: (56) Recv failure: Connection reset by peer
$ curl 'http://127.0.0.1:80'
curl: (7) Failed to connect to 127.0.0.1 port 80: Connection refused
$ curl 'http://127.0.0.1:8080'
curl: (56) Recv failure: Connection reset by peer
$ curl 'http://127.0.0.1:5000'
curl: (56) Recv failure: Connection reset by peer
$ curl 'http://127.0.0.1:5050'
curl: (56) Recv failure: Connection reset by peer
& 下面来自静态 IP,每个都经过短暂的延迟...
$ curl 'http://192.168.33.10:80'
curl: (7) Failed to connect to 192.168.33.10 port 80: Connection refused
$ curl 'http://192.168.33.10:8080'
curl: (7) Failed to connect to 192.168.33.10 port 8080: Connection refused
$ curl 'http://192.168.33.10:5000'
curl: (7) Failed to connect to 192.168.33.10 port 5000: Connection refused
$ curl 'http://192.168.33.10:5050'
curl: (7) Failed to connect to 192.168.33.10 port 5050: Connection refused
我使用的是 Mac OS Catalina。我的防火墙已禁用并允许所有连接。
虚拟机内
sudo netstat -ntlp
的输出..
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 469/rpcbind
tcp 0 0 127.0.0.53:53 0.0.0.0:* LISTEN 510/systemd-resolve
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 865/sshd
tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN 790/postgres
tcp 0 0 192.168.33.10:5050 0.0.0.0:* LISTEN 2730/node
tcp 0 0 192.168.33.10:5000 0.0.0.0:* LISTEN 4070/python
tcp6 0 0 :::111 :::* LISTEN 469/rpcbind
tcp6 0 0 :::22 :::* LISTEN 865/sshd
tcp6 0 0 ::1:5432 :::* LISTEN 790/postgres
它似乎与这里的问题类似,但我的 vagrantfile 中有正确的行:Vagrant,nodejs 和 iptable 端口转发
我还仔细检查了大约 10 个问题中指出的所有内容,如下所示: vagrant 端口转发不起作用:连接被对等方重置
config.ssh.extra_args
选项。
这可能会给出类似的内容:
config.ssh.extra_args = ["-L", "80:localhost:8080", "-L", "27017:localhost:27017", "-L", "5000:localhost: 5000", "-L", "5050:localhost: 5050"]
这样做相当于使用 ssh 运行多个端口转发(来自 vagrant 项目的存储库):
ssh -N -n -L 80:localhost:8080 -L 27017:localhost:27017 -L 5000:localhost:5000 -L 5050:localhost:5050 192.168.33.10 -i `pwd`/.vagrant/machines/default/virtualbox/private_key -X