【发布时间】:2017-12-19 13:59:58
【问题描述】:
ssh 端口转发(默认启用)工作正常,但我的自定义端口转发不行。
我从the official docs 获取了下面的配置,但它不起作用:(
主机运行 Ubuntu 17.04
Vagrantfile 是:
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/xenial64"
# though, I've also tried ubunty/zesty64 and centos/7 - with same results
config.vm.provision :shell, path: "vagrant/provision.sh"
config.vm.network "forwarded_port", guest: 3000, host: 3001
end
http服务器是node.js,在guest机器上监听3000端口
当我运行 vagrant up 时,我看到有 2 个端口转发:
==> default: Forwarding ports...
default: 3000 (guest) => 3001 (host) (adapter 1)
default: 22 (guest) => 2222 (host) (adapter 1)
我通过端口 2222 上的 ssh 成功连接到访客,因此转发 22 (guest) => 2222 (host) 工作正常。
从客户机(通过 ssh)访问端口 3000 也可以正常工作:
(以下是连接到访客机器时来自 ssh 控制台的引述)
ubuntu@ubuntu-xenial:~$ curl -v http://localhost:3000/
* Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 3000 (#0)
> GET / HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/7.47.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Content-Type: text/plain
< Date: Fri, 14 Jul 2017 12:34:29 GMT
< Connection: keep-alive
< Content-Length: 12
<
Hello World
* Connection #0 to host localhost left intact
但是直接从主机请求端口 3001 失败:
(以下是本地主机上常规控制台(不是 ssh)的引用)
$ curl -v http://127.0.0.1:3001/
* Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 3001 (#0)
> GET / HTTP/1.1
> Host: 127.0.0.1:3001
> User-Agent: curl/7.52.1
> Accept: */*
>
* Recv failure: Connection reset by peer
* Curl_http_done: called premature == 1
* Closing connection 0
curl: (56) Recv failure: Connection reset by peer
我还发现这与防火墙无关,因为它被禁用(在两台机器上):
ubuntu@ubuntu-xenial:~$ sudo ufw status
Status: inactive
我还注意到两台机器上的所有其他端口都返回相同的结果:
ubuntu@ubuntu-xenial:~$ curl http://127.0.0.1:3003/
curl: (7) Failed to connect to 127.0.0.1 port 3003: Connection refused
——这仅仅意味着这些端口在各自的机器上是关闭的,这完全没问题。
并且只有主机上的 3001 端口返回Connection reset by peer:
$ curl http://127.0.0.1:3001/
curl: (56) Recv failure: Connection reset by peer
——这意味着网络设置有问题。但究竟是什么?
请帮我正确设置端口转发!
更新 1
也许这很重要:升级 vagrant 时,控制台会打印以下内容:
==> default: Clearing any previously set forwarded ports...
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
default: Adapter 1: nat
default: Adapter 2: hostonly
==> default: Forwarding ports...
default: 3000 (guest) => 3001 (host) (adapter 1)
default: 22 (guest) => 2222 (host) (adapter 1)
也许原因是 Vagrant 出于某种原因转发了错误适配器的端口?也许它应该只转发给host,它转发给nat?我不明白,这只是一个假设。
【问题讨论】:
-
在表面上,它看起来不错。 This 是我的流浪文件之一,它的类似文件。它一定是你的 vagrant/provision.sh 里的东西?
-
@RussleyShaw,不,它目前只有 Node.JS 安装,仅此而已:
cd ~curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -sudo apt-get install -y nodejs -
您是否使用 VirtualBox 作为您的 VM 提供程序?
-
@RussleyShaw,是的,我是。在your Vagrantfile 中使用了相同的框,
ubuntu/xenial64,并且此框仅支持此提供程序:VirtualBox(至少,这是它的页面上所说的,虽然我没有检查它) -
@RussleyShaw,我刚刚切换到
ubuntu/zesty64框-问题仍然存在:(
标签: node.js ubuntu vagrant portforwarding vagrantfile