流浪者端口转发不起作用:连接被对等方重置 [英] vagrant port forwarding doesn't work: Connection reset by peer
问题描述
ssh端口转发(默认情况下已启用)可以正常工作,但是我的自定义端口转发却不能.
我从官方文档中获取了以下配置,但这不起作用:(
I took the config below from the official docs, but it doesn't work :(
主机运行Ubuntu 17.04
host machine runs 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,在来宾计算机上侦听端口3000
http server is node.js, listening port 3000 at guest machine
当我运行vagrant up
时,我看到有2个端口转发:
when I run vagrant up
I see that there are 2 port forwardings:
==> default: Forwarding ports...
default: 3000 (guest) => 3001 (host) (adapter 1)
default: 22 (guest) => 2222 (host) (adapter 1)
我通过端口2222上的ssh成功连接到guest虚拟机,因此转发22 (guest) => 2222 (host)
工作正常.
I successfully connect to guest via ssh on port 2222, thus the forwarding 22 (guest) => 2222 (host)
works fine.
从来宾计算机访问端口3000(通过ssh)也可以正常工作:
(以下是连接到来宾计算机时ssh控制台的引用)
Accessing port 3000 from the guest machine (via ssh) also works fine:
(the following is a quote from ssh console when connected to guest machine)
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)的引文)
But requesting port 3001 directly from host machine fails:
(the following is a quote from a regular console (not ssh) on local host machine)
$ 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
我还发现这与防火墙无关,因为(在两台机器上)它都被禁用了:
I also found out this has nothing to do with firewall, 'cause it's disabled (on both machines):
ubuntu@ubuntu-xenial:~$ sudo ufw status
Status: inactive
我还注意到两台计算机上的所有其他端口都返回相同的端口:
I also noticed that all other ports on both machines return the same:
ubuntu@ubuntu-xenial:~$ curl http://127.0.0.1:3003/
curl: (7) Failed to connect to 127.0.0.1 port 3003: Connection refused
-只是意味着这些端口在相应的计算机上都是关闭的,这完全可以.
— which simply means that those ports are closed on respective machine, and that is completely OK.
只有主机上的端口3001返回Connection reset by peer
:
And only the port 3001 on host machine returns Connection reset by peer
:
$ curl http://127.0.0.1:3001/
curl: (56) Recv failure: Connection reset by peer
-表示网络设置有问题.但是到底是什么?
— that means that something's wrong in network settings. But what exactly?
请帮助我正确设置端口转发!
这也许很重要:当升级无业游民时,控制台中将显示以下内容:
Maybe this will be important: when upping vagrant, the following is printed in console:
==> 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?我不明白,这只是一个假设.
Maybe the reason is that Vagrant for some reason forwards ports for the wrong adapter? Maybe it should forward for hostonly, and it forwards for nat? I don't understand it, it's just an assumption.
推荐答案
最后,我能够修复它!
原因不是端口转发配置错误,既不是Vagrant的某些设置,也不是VirtualBox,Guest或Host OS.
The reason wasn't a misconfiguration of port forwarding, neither some settings of Vagrant, nor even VirtualBox, nor Guest, nor Host OS.
原因是我没有正确设置Node.JS的端口监听.
The reason was I've incorrectly set up Node.JS' port listening.
我做了什么(不正确):我将其设置为侦听来宾计算机上的端口3000.我认为来宾计算机的本地IP像往常一样是127.0.0.1.下面是我的Node.JS服务器代码:
What I've done (incorrect): I've set it up to listen port 3000 on guest machine. I thought that guest machine's local IP, available from inside itself, is, as usual, 127.0.0.1. Below is my Node.JS server code:
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
我必须做的(正确的):将其设置为侦听相同的端口,但是在其他IP上:0.0.0.0-这是客户机的正确本地IP,使用时可以在内部对其自身使用连同端口转发.
What I had to do (correct): set it up to listen that same port, but on other IP: 0.0.0.0 — this is the correct local IP of guest machine, that is available to it itself internally, when using together with port forwarding.
因此,我的Vagrantfile是正确的;这是我在Node.JS服务器中修复的问题:
Thus, my Vagrantfile is correct; here is what I've fixed in my Node.JS server:
const hostname = '0.0.0.0';
const port = 3000;
...
server.listen(port, hostname, () => {
P. S.对于由此给您带来的不便,我深表歉意.谢谢所有帮助我的人!起初,当我找到解决方案时,我打算删除此问题,但我决定自己改个问题会更好,这样像我这样的其他人可以节省他们和其他人在解决此类问题上所花费的时间未来,所以我决定保留它.
P. S. I apologize for the inconvenience I caused by this question. Thank you to everyone who helped me! At first, when I found the solutuion, I was going to delete this question, but decided that it'd be better if I answer it myself instead, so that others like me can save their and other people's time spent on fixing such cases in future, so I decided to keep it.
这篇关于流浪者端口转发不起作用:连接被对等方重置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!