Node.js 流浪港口转运不';t工作:由对等方重置连接

Node.js 流浪港口转运不';t工作:由对等方重置连接,node.js,ubuntu,vagrant,portforwarding,vagrantfile,Node.js,Ubuntu,Vagrant,Portforwarding,Vagrantfile,ssh端口转发(默认情况下启用)可以正常工作,但我的自定义端口转发无法正常工作。 我从中获取了下面的配置,但它不起作用:( 主机运行Ubuntu 17.04 vagrant文件是: Vagrant.configure("2") do |config| config.vm.box = "ubuntu/xenial64" # though, I've also tried ubunty/zesty64 and centos/7 - with same results conf

ssh端口转发(默认情况下启用)可以正常工作,但我的自定义端口转发无法正常工作。

我从中获取了下面的配置,但它不起作用:(

主机运行Ubuntu 17.04

vagrant文件
是:

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

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
$ 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
当我运行
vagrant up
时,我看到有两个端口转发:

==> 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 http://127.0.0.1:3003/
curl: (7) Failed to connect to 127.0.0.1 port 3003: Connection refused
-这仅仅意味着这些端口在各自的机器上是关闭的,这是完全正常的

并且只有主机上的端口3001返回对等方的连接重置:

$ 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)

可能是因为那个流浪者出于某种原因将端口转发给了错误的适配器?也许它应该只转发给主机,而转发给nat?我不明白,这只是一个假设。

最后,我能够修复它

原因不是端口转发的错误配置,也不是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, () => {

对这个问题给我带来的不便,我深表歉意。感谢所有帮助过我的人!起初,当我找到解决方案时,我打算删除这个问题,但我决定最好还是自己回答,这样像我这样的其他人将来可以节省他们和其他人在解决此类问题上的时间,所以我决定保留它。

127.0.0.1:3000
切换到
0.0.0:3000
确实会将运行来宾服务的服务暴露给主机网络,但是,我觉得应该存在更好的方法,无论如何,值得一提的是,我注意到在来宾操作系统上运行
netstat-nltp
会显示“外部地址”,它们是将暴露于主机操作系统的网元。

从表面上看,它看起来不错。是我的一个vagrant文件及其类似文件。它一定是您的vagrant/provision.sh?@RussleyShaw中的某个文件,不,它当前只有Node.JS安装,仅此而已:
cd~
curl-sLhttps://deb.nodesource.com/setup_8.x |sudo-E bash-
sudo apt get install-y nodejs
你是否使用VirtualBox作为你的虚拟机提供商?@RussleyShaw,是的。在同一个框中,这个框只支持这个提供商:VirtualBox(至少,这是它页面上说的,我没有检查)@RussleyShaw,我刚刚切换到
ubuntu/zesty64
框-问题仍然存在:(谢谢,尽管我没有尝试对节点执行此操作,但我能够使用与Elasticsearch Kibana中的主机名相同的配置
0.0.0
,在我的host@whyer谢谢你没有删除这个问题。它刚刚解决了我的问题。这太神奇了。我在Ruby应用程序和将web服务器绑定到0.0.0.0 wa时也遇到了同样的问题这是解决办法。谢谢!
const hostname = '0.0.0.0';
const port = 3000;
...
server.listen(port, hostname, () => {