Node.js Site is Refusing Connection

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.


Solution 1:

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.

Solution 2:

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 :)