2

我有这个 Vagrantfile

VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "hashicorp/precise32"
  config.vm.network "forwarded_port", guest: 80, host: 3000
  config.vm.network "private_network", ip: "192.168.33.11"
end

我的目标是为 nodejs 运行一个虚拟机。我已经正确安装了节点。在“vagrant ssh”之后,我创建了一个包含以下内容的文件“index.js”:

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(3000, '127.0.0.1');
console.log('Server running at http://127.0.0.1:3000/');

从 vagrant@precise32:/vagrant$,当我运行“curl localhost:3000”时,我得到“Hello world”。但, ...

我需要做什么,在我的本地机器上打开我的浏览器并获得相同的“Hello world”?


如果我尝试“卷曲”我的虚拟机的 ip,我会得到:

$ curl 192.168.33.11:3000
curl: (7) Failed connect to 192.168.33.11:3000; Connection refused

尝试远程登录:

$ telnet 192.168.33.11:3000
Trying 192.168.33.11:3000...
curl: (7) Failed connect to 192.168.33.11:3000; Connection refused

尝试 curl --verbose 仍然不适用于端口 3000

$ curl --verbose 192.168.33.11:3000
* About to connect() to 192.168.33.11 port 3000 (#0)
*   Trying 192.168.33.11...
* Adding handle: conn: 0x7f8f5a803a00
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0x7f8f5a803a00) send_pipe: 1, recv_pipe: 0
* Failed connect to 192.168.33.11:3000; Connection refused
* Closing connection 0
curl: (7) Failed connect to 192.168.33.11:3000; Connection refused

与端口 80 完美配合

$ curl --verbose 192.168.33.11
* About to connect() to 192.168.33.11 port 80 (#0)
* Trying 192.168.33.11...
* Adding handle: conn: 0x7fc93b802000
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0x7fc93b802000) send_pipe: 1, recv_pipe: 0
* Connected to 192.168.33.11 (192.168.33.11) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.30.0
> Host: 192.168.33.11
> Accept: */*
>
< HTTP/1.1 200 OK
< Date: Mon, 07 Jul 2014 07:06:25 GMT
* Server Apache/2.2.22 (Ubuntu) is not blacklisted
< Server: Apache/2.2.22 (Ubuntu)
< Last-Modified: Sun, 06 Jul 2014 13:53:47 GMT
< ETag: "4811a4-bb-4fd86b009e369"
< Accept-Ranges: bytes
< Content-Length: 187
< Vary: Accept-Encoding
< Content-Type: text/html
< X-Pad: avoid browser bug
<
<html><body><h1>It works un casino!</h1>
<p>This is the default web page for this server.</p>
<p>The web server software is running but no content has been added, yet.</p>
</body></html>
* Connection #0 to host 192.168.33.11 left intact
4

2 回答 2

1

If 192.168.33.11 is the IP of the virtual machine running node, then typically you d load 192.168.33.11:3000 in your local machine. A curl error 7 means the request is blocked in case of a firewall.

于 2014-07-06T14:21:54.017 回答
0

虽然是一个老问题,但对于那些在较新版本的 Node/Vagrant 上偶然发现它的人来说,这里有一个解决方案。

似乎 Node 不再需要端口转发才能通过 Vagrant 工作。事实上,当 Node 示例本身在 VM 中的 127.0.0.1 上运行时,没有任何转发端口配置对我有用。

相反,让 Node 服务器在您在 Vagrantfile 中指定的专用网络 ip 上运行。在上面的示例中,这等于listen(3000, '192.168.33.11');

在 Win 10 和 macOS Mavericks 上的 Node 4.2.6 和 Vagrant 1.8.7 上为我工作。

于 2016-11-22T23:20:53.330 回答