How to troubleshoot a Vagrant VM running a NestJS server returning ECONNREFUSED error when hitting an endpoint?

huangapple go评论57阅读模式
英文:

How to troubleshoot a Vagrant VM running a NestJS server returning ECONNREFUSED error when hitting an endpoint?

问题

我已经设置了通过Vagrant创建的多个虚拟机(VMs),每个都有自己的私有网络IP。问题出现在其中一个VM上,我将其命名为netcap_backend,我设置它启动了一个nestjs服务器,但当我尝试访问主机计算机上的一个端点时,返回ECONNREFUSED错误。我在192.168.56.101:3000/上直接设置了一个简单的GET端点。我的主机计算机运行Arch,这是针对特定VM的配置:

  config.vm.define "netcap_backend" do |back|
    back.vm.hostname = "netcap-backend"
    
    back.vm.provider :virtualbox do |vb|
      vb.customize ["modifyvm", :id, "--memory", env['MEM_BACKEND']] 
      vb.customize ["modifyvm", :id, "--cpus", env['CPU_BACKEND']]
    end
  
    back.vm.network "private_network", ip: env['BACKEND_IP'] # 192.165.56.101
    back.vm.provision "ScriptRunAsVagrantUser", privileged: false, type:"shell", path: "#{env['PROVISION_PATH']}/prov_netcap_back.sh"
  end

这是我的nestjs服务器中的main.ts

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.enableCors({
    allowedHeaders: '*',
    origin: '*',
    credentials: true,
  });

  await app.listen(3000);
}
bootstrap();

奇怪的是,这个配置与我的前端VM配置相同,而前端VM运行的是React,我的主机计算机连接它没有问题,尽管使用不同的IP。

一开始我在Firefox上尝试了一个简单的fetch,我期望fetch调用会返回一个简单的Hello World,但是它返回了CORS问题,因此我在main.ts中添加了额外的enableCors()调用,但似乎没有解决问题。然后我使用Postman来ping我的路由,那时我看到了ECONNREFUSED错误。然后我尝试查看我的主机计算机是否能够与VM通信,它可以,因为ping 192.168.56.101执行得很好,没有网络错误。

稍微更新一下:在Windows上尝试相同的设置,完全正常工作...

英文:

I have setup several VMs provisioned through vagrant, each with their own private network IP. The issue comes up is that for one of the VMs, which I have named netcap_backend, I have set to start a nestjs server but when I try to hit an endpoint on my host computer, it returns with a ECONNREFUSED. I have a simple GET endpoint set directly at 192.168.56.101:3000/. My host computer is running Arch, and this is the provisioning for that specific VM:

  config.vm.define "netcap_backend" do |back|
    back.vm.hostname = "netcap-backend"
    
    back.vm.provider :virtualbox do |vb|
      vb.customize ["modifyvm", :id, "--memory", env['MEM_BACKEND']] 
      vb.customize ["modifyvm", :id, "--cpus", env['CPU_BACKEND']]
    end
  
    back.vm.network "private_network", ip: env['BACKEND_IP'] # 192.165.56.101
    back.vm.provision "ScriptRunAsVagrantUser", privileged: false, type:"shell", path: "#{env['PROVISION_PATH']}/prov_netcap_back.sh"

  end

And this is what I have for my main.ts in my nest server:

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.enableCors({
    allowedHeaders: '*',
    origin: '*',
    credentials: true,
  });

  await app.listen(3000);
}
bootstrap();

What is strange is that this is configured identically to my frontend VM, and that is running a React which my host computer has no problem connecting to, be it with a different IP.

At first when I tried a simple fetch on firefox, I expected the fetch call to return me with a simple Hello World but, it returned me with a CORS issue, hence the additional enableCors() call in my main.ts. But that didn't seem to solve the issue. I then used Postman to ping my route, that's when I saw the ECONNREFUSED. What I then tried is to see if my host computer can actually talk to the VM, which it could as ping 192.168.56.101 executed just fine with no network errors.

A little update: Tried the same setup but on windows and worked perfectly...

答案1

得分: 0

错误发生在这里的问题是,虽然我的前端和后端服务器都使用不同的IP,但它们有重叠的端口(都使用3000端口)。至少在我的系统上,这导致只有一个服务器连接到主机网络。解决方法是为需要暴露到网络的每个服务器使用不同的端口号,并重新启动运行服务器的每个Vagrant虚拟机。

英文:

The error that is happening here is that while both my frontend and backend server are using different IPs, they have overlapping ports (both using 3000). On my system at least, this caused only one of the servers to be connected to the host-only network. Solution is to use different port numbers for each server that needs to be exposed to the network and restart each vagrant vm that is running the server.

huangapple
  • 本文由 发表于 2023年5月26日 01:19:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76334830.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定