Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

If I run the basic nodejs.org example on my Amazon EC2 Server, I get the message "Server running" in the console. But if I try to open it in a browser, using my elastic IP + the port, I get no result.

I think I have opened the TCP ports 22 and 1337 (I'm not a 100% sure because I'm new to this stuff).

share|improve this question

1 Answer 1

up vote 5 down vote accepted

Presumably you mean this example:

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

The message you would have received like so is Server running at http://127.0.0.1:1337/ - the emphasized part is key here, i.e. the server has been configured to listen on the IP address 127.0.0.1 (aka localhost) only, whereas you are trying to browse it via your elastic IP address.

Accordingly, you can achieve you goal by replacing 127.0.0.1 in the listen()statement with the private/internal IP address which has been assigned to your EC2 instance.


Variations

Alternatively, you could use the public/external DNS name instead, which exhibits a lesser known dynamic IP address resolution depending on the origin of the DNS query, see e.g. Why do I have two IP addresses and two host names?:

The external DNS name (which looks like ec2-72-44-45-204.compute-1.amazonaws.com) resolves to the public IP address of the instance outside the Amazon EC2 network and the private IP address from within Amazon EC2 network.

Finally (as per cyberx86's comment, thanks!), you may be able to omit the optional address parameter altogether (but see below), see server.listen(port, [hostname], [callback]):

If the hostname is omitted, the server will accept connections directed to any IPv4 address (INADDR_ANY).

Be aware though, that this might not always be the right thing in advanced scenarios, insofar there may be more than one network interface available in fact after the recent introduction of Elastic Network Interfaces (ENI) in the Virtual Private Cloud (VPC), which is giving you the ability to create additional ENIs, and to attach a second ENI to an instance (again, this is within the VPC).

share|improve this answer
    
oh that makes sense. If I replace it with my elastic IP I receive the error: "Error: listen EADDRNOTAVAIL" - does this mean that the port is not open? –  Christian Strang Jan 31 '12 at 12:43
1  
@Christian: sorry, I mixed this up in the hurry - you'll have to use the internal IP address, which AWS automatically assigned to your EC2 instance on startup (the routing/wiring from the external to the internal address is done automatically by AWS on your behalf); you can inspect it manually e.g. on the properties window of your instance in the AWS Management Console, or by various other means for automation purposes. –  Steffen Opel Jan 31 '12 at 12:48
1  
Great! Replacing it with my "Internal IP" made it work. For everyone who doesn't know where to find it, it's listed under "Private DNS" in your instance description. Use your internal ip for the listen statement and your elastic ip to "call" your server. –  Christian Strang Jan 31 '12 at 12:51
2  
@ChristianStrang: You should be able to simply omit the address altogether, to have nodejs listen on all (local and external) addresses - i.e.: http.createServer(...).listen(1337); This may be practical since there is only one external interface, and your internal-ip may change. –  cyberx86 Jan 31 '12 at 13:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.