8

I have a fresh Ubuntu instance setup on Amazon EC2. This instance has a node web server on it. When I try to hit my web site, I get an error in my browser. The error is:

Connect: Connection refused

I've confirmed that I can ping the machine. Yet when I enter http://[ipAddress] or http://[ipAddress]:8080 in my browser, I get the error above. When I start the node web server, it says:

Server listening on 8080

I implemented port forwarding using the following command:

iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 80 
-j REDIRECT --to-port 8080

I believe I opened the firewall appropriately using the following commands:

iptables -A INPUT -p tcp -m tcp --sport 80 -j ACCEPT
iptables -A OUTPUT -p tcp -m tcp --dport 80 -j ACCEPT

I'm stuck. I do not know why I'm getting a "Connection refused." I would sincerely appreciate any help or insights that someone can provide. At this point, I not sure if this message is coming from Ubuntu or from the Node server. Beyond that, I'm unfamiliar with how to track this issue down.

Thank you for your help. It means a lot to me.

1
  • Check that port 80 is allowed in your instance's Security Group Apr 21, 2014 at 15:44

4 Answers 4

3

Your server is most likely not listening on [ipAddress]:8080 is likely only listening on localhost:8080. You can check this using netstat You will need to configure your server to listen on all/appropriate IP addresses. No doubt how to do this will be in the documentation.

3
  • Hello Iain, Thank you for your response. I've ran netstat and noticed that neither 80, nor 8080, are in the list. I see three tcp records. However, I do not understand what I should be looking for. Do you know of a good link that would explain what I'm looking for? I'm historically not a linux guy. Apr 21, 2014 at 17:46
  • Please note I also ran "netstat -a | egrep 'Proto|LISTEN'" and did not see my node server in the list. Apr 21, 2014 at 17:50
  • 2
    @user3284007: try netstat -tnl | grep 8080.
    – user9517
    Apr 21, 2014 at 18:06
3

I had the exact same issue with my ExpressJS app, and it's not your rerouting. I hacked at iptables and netstat for a long time until I realized that I had my HOST variable in my server.js file set to localhost. Here was my non-working code:

app.listen(PORT, 'localhost', function(err) {
    if (err) return console.log(err);
    console.log("Listening at http://localhost:%s", PORT);
});

Simply add a host variable

var HOST = 'actual_server_goes_here...';

And then do this:

app.listen(PORT, HOST, function(err) {
  if (err) return console.log(err);
  console.log("Listening at http://%s:%s", HOST, PORT);
});

Hope this helps :)

1

You want to fill in the serverAddress to make it listen on the right network interface:

http.createServer(function (req, rsp) () {
    ...
}).listen(serverPort, serverAddress);

Also make sure with Amazon EC2 that in the Management Console you have a Security Group attached to your Machine Instance that allows access on that port. (These Security Groups allow you to configure the EC2-firewall.)

1

you need to use your PRIVATE IP to listen on it. The private IP address of the instance (multiple IP addresses are listed if there is more than one network interface to the instance). it is shown in the EC2 management console, per instance

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .