无法连接到vagrant机器上托管的节点js服务器

时间:2017-06-06 12:00:50

标签: node.js vagrant

我正在尝试将vagrant作为本地测试服务器,并且我很难从托管Windows机器连接到nodejs app服务器,但我可以很好地连接到nginx服务器。

我的流浪档案:

Vagrant.configure("2") do |config|
  config.vm.define "web" do |web|
    web.vm.box = "ubuntu/trusty64"
    web.vm.network "private_network", ip: "55.55.55.55"
  end

  config.vm.define "app_0" do |app_0|
    app_0.vm.box = "ubuntu/trusty64"
    app_0.vm.network "private_network", ip: "55.55.55.1"
  end

  config.vm.define "db" do |db|
    db.vm.box = "ubuntu/trusty64"
    db.vm.network "private_network", ip: "55.55.55.56"
  end
end

我的节点js index.js(helloworld)来自app_0 at 55.55.55.1:

// Load the http module to create an http server.
var http = require('http');

// Configure our HTTP server to respond with Hello World to all requests.
var server = http.createServer(function (request, response) {
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.end("Hello World\n");
});

// Listen on port 80, IP defaults to 127.0.0.1
server.listen(80, '0.0.0.0', function() {
    console.log('Listening to port:  ' + 80);
});

哪个是用sudo节点index.js运行的,并且在vagrant机器上本地可见,但我无法从主机上连接到它55.55.55.1:80

请不要将此副本标记为Connect to node js server running on vagrant machine,因为它不使用端口转发。

1 个答案:

答案 0 :(得分:0)

我看到两件事:

  • 55.55.55.x不是专用网络IP(请参阅https://en.wikipedia.org/wiki/Private_network),因此您可能会获得有效的IP,甚至在设置主机文件时,您可能会遇到冲突。

    < / LI>
  • 最后不要使用.1的地址,它是默认的网关/路由器 所以流浪者将如何到达主人。

从专用网络范围中选择一个有效的IP,它将起作用

Vagrant.configure("2") do |config|
  config.vm.define "web" do |web|
    web.vm.box = "ubuntu/trusty64"
    web.vm.network "private_network", ip: "192.168.55.10"
  end

  config.vm.define "app_0" do |app_0|
    app_0.vm.box = "ubuntu/trusty64"
    app_0.vm.network "private_network", ip: "192.168.55.11"
  end

  config.vm.define "db" do |db|
    db.vm.box = "ubuntu/trusty64"
    db.vm.network "private_network", ip: "192.168.55.12"
  end
end