如何通过公共IP地址访问我的node.js服务器?

我想通过在浏览器中输入我的公共IP地址来从远程node.js服务器获得响应。当我在个人计算机上的浏览器中键入公用IP时,出现“无法连接”。我的node.js服务器未连接到World =(

  • 我在Linode上运行CentOS(但我认为这对我的问题都不重要)。
  • 通过个人计算机(在Mac上)上的Terminal,我可以作为root用户成功地通过SSH进入Linode。
  • 我已经在我的Linode上成功安装了node.js。
  • 我可以在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");
});

我尝试过:

  • 设置主机名。
  • 更改服务器上的“主机”文件。
  • 更改我的node.js服务器中的端口号(3000、80、8080、3001、0.0.0.0等)。
  • 今天确实阅读了100篇有关如何部署node.js服务器的文章。
  • 在Google,Stackoverflow,Linode论坛等中搜索了可能对我有帮助的主题。

我不知道我在做什么错,非常感谢您的帮助。

jenny013 回答:如何通过公共IP地址访问我的node.js服务器?

我终于找到了答案,这要归功于Saddy的建议,即问题可能是端口转发。

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:

防火墙站

防火墙禁用

iptables -I输入1 -p tcp --dport 80 -j接受

iptables -I输入1 -p tcp --dport 443 -j接受

iptables -I输入1 -p tcp --dport 25 -j接受

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保存> / etc / sysconfig / iptables

此后,我能够通过浏览器访问我的节点服务器。

本文链接:https://www.f2er.com/2388979.html

大家都在问