简体   繁体   English

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

[英]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.我想通过在浏览器中输入我的公共 IP 地址来从远程 node.js 服务器获得响应。 When I type my public IP into my browser on my personal computer, I get "Unable to Connect".当我在个人计算机上的浏览器中键入我的公共 IP 时,我得到“无法连接”。 My node.js server isn't connected to the World =(我的 node.js 服务器未连接到 World =(

  • I am running CentOS on a Linode (but I don't think either choice should matter to my question).我在 Linode 上运行 CentOS (但我认为这两种选择对我的问题都不重要)。
  • Via Terminal on my person computer (a Mac), I can successfully SSH as root into my Linode.通过我的个人计算机(Mac)上的终端,我可以成功地将 SSH 以 root 身份进入我的 Linode。
  • I have installed node.js successfully on my Linode.我已经在我的 Linode 上成功安装了 node.js。
  • I can compile and run a simple server on my Linode.我可以在我的 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).更改我的 node.js 服务器中的端口号(3000、80、8080、3001、0.0.0.0 等)。
  • Read literally 100 articles today about how to deploy a node.js server.今天阅读了 100 篇关于如何部署 node.js 服务器的文章。
  • Searched Google, Stackoverflow, Linode forums, etc for threads that might help me.在 Google、Stackoverflow、Linode 论坛等中搜索可能对我有帮助的线程。

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.我最终找到了答案,这要感谢 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:

firewalld stop防火墙停止

firewalld disable防火墙禁用

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

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

iptables -I INPUT 1 -p tcp --dport 25 -j ACCEPT 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 80 -j REDIRECT --to-port 3080

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

iptables-save > /etc/sysconfig/iptables iptables-save > /etc/sysconfig/iptables

After this, I was able to access my node server via a browser.在此之后,我能够通过浏览器访问我的节点服务器。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使Node.js服务器可以通过公共IP地址访问 - How to make node.js server accessible by public ip-address 如何使用 Google 计算 node.js api 创建具有公共 IP 地址的接口? - How do you create an interface with a public IP address with Google compute node.js api? 我无法通过公共 IP 访问我的 Docker Node.js 容器 - I can not access by public IP to my Docker Node.js container 我的服务器(在node.js上)具有可变IP地址的证书SSL - Certificate SSL of my server (on node.js) with variable IP address "如何使用 Node js 获取我的公共 IP 地址" - How to get My PUBLIC IP address Using Node js Node.js似乎忽略了我提供的IP地址,而是使用localhost打开服务器 - Node.js seems to ignore my IP address i give, instead it uses localhost to open the server 我已经在cPanel上执行了一个节点js服务器,它已成功执行,但仍无法使用服务器的公共IP地址访问我的服务器 - I have executed a node js server on cPanel, it is succefully executing but still my server is not accessible using public IP address of my server 如果我的电脑的IP地址每天都在变化,如何编写Node.js服务器 - How to write a Node.js server if my pc's ip address changes daily 如何使用 node.js 获取我的外部 IP 地址? - How to get my external IP address with node.js? 如何通过客户端JavaScript从我的node.js服务器访问数据? - How do I access data from my node.js server from client javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM