简体   繁体   中英

How do I access my node.js server via public IP address?

I want to get a response from a remote node.js server by typing my public IP address into my browser. When I type my public IP into my browser on my personal computer, I get "Unable to Connect". My node.js server isn't connected to the World =(

  • I am running CentOS on a Linode (but I don't think either choice should matter to my question).
  • Via Terminal on my person computer (a Mac), I can successfully SSH as root into my Linode.
  • I have installed node.js successfully on my Linode.
  • I can compile and run a simple server on my Linode.
var http = require('http');//create a server object:
http.createServer(function (req, res) {
  res.write('Hello World!'); //write a response
  res.end(); //end the response
}).listen(3000, function(){
 console.log("server start at port 3000");
});

I've tried:

  • Setting a hostname.
  • Changing the "hosts" file on my server.
  • Changing the port number in my node.js server (3000, 80, 8080, 3001, 0.0.0.0, etc).
  • Read literally 100 articles today about how to deploy a node.js server.
  • Searched Google, Stackoverflow, Linode forums, etc for threads that might help me.

I have zero idea what I'm doing wrong and would be so grateful for your help.

I eventually found the answer, thanks to Saddy's suggestion that the problem might be port forwarding.

1. I decided to use ports 3080 and 3443 for my node server.
2. I SSHed into my CentOs instance.
3. I disabled the default firewall, firewalld.
4. I set up port forwarding using iptables with the following commands:

firewalld stop

firewalld disable

iptables -I INPUT 1 -p tcp --dport 80 -j ACCEPT

iptables -I INPUT 1 -p tcp --dport 443 -j ACCEPT

iptables -I INPUT 1 -p tcp --dport 25 -j ACCEPT

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

iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 443 -j REDIRECT --to-port 3443

iptables-save > /etc/sysconfig/iptables

After this, I was able to access my node server via a browser.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM