Node.js不能从Ubuntu的外部IP访问

我相信这是非常不好的,所以原谅我。 我试图在我的Ubuntu 10.04的端口8080上运行一个node.js服务器。

以下是服务器上iptables -L的结果:

Chain INPUT (policy ACCEPT) target prot opt source destination Chain FORWARD (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination 

这是nmap -p 8080的结果(编辑ip地址,因为一切都应该是开放的)

 nmap 173.203.xxx.xxx -p 8080 -A Starting Nmap 5.00 ( http://nmap.org ) at 2011-05-19 22:52 PDT Interesting ports on xxx (173.203.xxx.xxx): PORT STATE SERVICE VERSION 8080/tcp closed http-proxy 

为什么8080被视为封闭? 添加这个没有帮助:

 iptables -A OUTPUT -p tcp --dport 8080 -j ACCEPT iptables -A INPUT -p tcp --dport 8080 -j ACCEPT 

我很困惑。

我会补充一点,以防万一,但我不知道

  netstat -pan | grep 80 tcp 0 0 127.0.0.1:8080 0.0.0.0:* LISTEN 16785/node tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 16471/apache2 

我可以通过apache上的80端口访问我的常规网站,但从外部无法访问node.js服务器。 访问它从服务器本身工作正常。

所以我的问题是:我将如何去debugging呢? 有没有什么比阻止某些端口的iptables? 我如何知道它是什么?

任何帮助非常感谢!

感谢您添加最后一个netstat输出,它真的帮助。 您不能从外部访问node.js,因为它正在监听本地主机IP,即127.0.0.1。 您需要将node.jsconfiguration为在0.0.0.0上进行侦听,以便能够接受机器所有IP上的连接。

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

根据你的netstat输出,8080端口只能打开127.0.0.1这是本地主机,因此从服务器访问工作(这是本地主机),而不是从其他地方。

正确的输出应该看起来像

 0.0.0.0:8080 

我遇到过同样的问题。

我做了以下

  1. 在Google云端控制台中,您需要启用您用于Nodejs的端口
  2. 使用以下链接在Ubuntu服务器上启用相同的端口https://www.digitalocean.com/community/tutorials/how-to-set-up-a-firewall-with-ufw-on-ubuntu-14-04

  3. 在这最后一个之后我所缺less的就是Networking – > Firewall Rules – > default-allow-internal – > all IP address 0.0.0.0/0

看到这个图像

完成