流浪汉端口转发不起作用:由同行重置连接

时间:2017-07-14 12:49:40

标签: node.js ubuntu vagrant portforwarding vagrantfile

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,客户机器上的侦听端口3000

当我运行vagrant up时,我发现有2个端口转发:

==> default: Forwarding ports...
    default: 3000 (guest) => 3001 (host) (adapter 1)
    default: 22 (guest) => 2222 (host) (adapter 1)

我通过端口2222上的ssh成功连接到guest虚拟机,因此转发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

也许这很重要:当增加流浪汉时,会在控制台中打印以下内容:

==> 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出于某种原因转发错误适配器的端口?也许它应该转发为hostonly,它转发nat?我不明白,这只是一个假设。

3 个答案:

答案 0 :(得分:10)

最后,我能够解决它!

原因不是端口转发配置错误,也不是Vagrant的某些设置,甚至VirtualBox,Guest也不是Host OS。

原因是我错误地设置了Node.JS的端口侦听。

我做了什么(不正确):我已将其设置为在来宾计算机上侦听端口3000。我认为客户机本身的IP本身就像往常一样,是127.0.0.1。下面是我的Node.JS服务器代码:

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,在使用时可以在内部使用连同端口转发。

因此,我的Vagrantfile是正确的;这是我在Node.JS服务器中修复的内容:

const hostname = '0.0.0.0';
const port = 3000;
...
server.listen(port, hostname, () => {

P上。 S.我为这个问题造成的不便深表歉意。谢谢所有帮助过我的人!起初,当我找到解决方案时,我打算删除这个问题,但我决定自己更好地回答这个问题,以便像我这样的其他人可以节省他们和其他人花在修复此类案件上的时间。未来,所以我决定保留它。

答案 1 :(得分:0)

你要做的事情是行不通的。下面的配置意味着

config.vm.network "forwarded_port", guest: 3000, host: 3001

将vagrant box上的端口3000转发到我localhost上的端口3001,这样可以通过localhost的端口3001访问vagrant机器上的端口3000。

如果要直接访问流浪汉机器上的3000,则必须创建专用网络以将IP附加到流浪汉机器,并通过此IP访问端口3001。例如

config.vm.network "private_network", ip: "192.168.33.10"
config.vm.network "forwarded_port", guest: 3000, host: 3001

现在,您可以通过curl -v http://192.168.33.10:3000/

访问端口3000

答案 2 :(得分:0)

127.0.0.1:3000切换到0.0.0.0:3000确实将来宾运行的服务暴露给主机网络,但是,我觉得应该存在更好的方法,无论如何,值得一提的是,我注意到运行{{1 }}在访客OS上将显示“外部地址”,这些地址将公开给主机OS。