2
votes

I've this 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

My objective is to run a virtual machine for nodejs. I've correctly installed node. After "vagrant ssh", I've create a file "index.js" with this content:

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/');

From vagrant@precise32:/vagrant$, when I run "curl localhost:3000", I get "Hello world". But, ...

What I need to do, to open my browser in my local machine and get same "Hello world"?


If I try to "curl" the ip of my virtual machine I get this:

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

Trying to telnet:

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

trying curl --verbose Still dont work with port 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

Work perfectly with port 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
2
Can you telnet the virtual machine? Try restarting it once as well. - walmik
Well, If apache is up, I can curl and telnet. If I stop apache, ... when I run nodejs script, ... I am not able to reach vagrant from my web browser :-/ - sensorario
Are you running centos on your virtual machine? - walmik
Nope. Is Ubuntu precise 32-bit. - sensorario
Hmm, maybe you ve already done this, but could you try curl with the --verbose option. I d also try and setup a basic HTML page and run a python simple http server and curl that to check if this issue is pertaining to Node.js - walmik

2 Answers

1
votes

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.

0
votes

Although an old question, here's a solution for those that stumble upon it on newer versions of Node/Vagrant.

It seems that Node no longer requires port forwarding to work via Vagrant. In fact, no forwarded port configuration worked for me while having the Node example itself running on 127.0.0.1 in the VM.

Instead, have the Node server run on the private network ip you specify in your Vagrantfile. In the example above, that equates to listen(3000, '192.168.33.11');.

Worked for me on Node 4.2.6 and Vagrant 1.8.7 both on Win 10 and macOS Mavericks.