【问题标题】:Running a node.js server on my VPS on port 3000 and the connection times out在我的 VPS 上的 3000 端口上运行 node.js 服务器并且连接超时
【发布时间】:2014-06-24 03:46:15
【问题描述】:

在 hostgator 中,我有一个运行 centOS 的 VPS。我安装了 NodeJS 和屏幕。 我将以下代码添加到名为 index.js 的文件中:

//1
var http = require('http');

//2 
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/html'});
  res.end('<html><body><h1>Hello World</h1></body></html>');
}).listen(3000);

console.log('Server running on port 3000.');

在“screen:1”上,我运行以下命令:

node index.js

它给了我控制台输出,说明“服务器在端口 3000 上运行。” 我切换到“screen:0”并运行以下命令:

curl localhost:3000

我得到以下响应:

<html><body><h1>Hello World</h1></body></html>

然而,当我尝试我的服务器的 IP 地址时(用 xxx 代替真实的 IP 地址,因为我没有透露我的 VPS IP 地址):

xxx.xxx.xxx.xxx:3000

页面永远不会出现并最终超时。 我尝试了各种端口(8080、7000)但都没有用。 我是否需要将 iOS 项目放在不同的目录中。 目前我在 /root/Projects/NodeTutorial2/index.js 中有它。 我需要做什么才能从我的 VPS 获得 hello world 响应?

【问题讨论】:

  • 我确定您刚刚运行了防火墙。停止 iptables 并查看。此外,请确保您是通过真实的互联网连接进行测试。许多移动运营商对非 HTTP 端口上的流量做了一些不稳定的事情。
  • 我做了:root@mic [~/Projects/NodeTutorial2]# /etc/init.d/iptables save iptables: 将防火墙规则保存到 /etc/sysconfig/iptables:[ OK ] root@mic [~/Projects/NodeTutorial2]# /etc/init.d/iptables stop iptables: 将链设置为策略 ACCEPT: nat mangle filte[OK] iptables: 刷新防火墙规则: [OK] iptables: 卸载模块: [OK] root@麦克风 [~/Projects/NodeTutorial2]#
  • 布拉德成功了!请提交作为答案!
  • 保罗已经做到了。你可以接受他的回答。而且,当您设置适当的规则时,不要忘记重新打开防火墙。
  • 我保存了 iptables 并运行了 'iptables start'

标签: node.js vps


【解决方案1】:

如果您收到来自盒子的响应,但没有从其他盒子获得响应,则几乎可以肯定是防火墙问题。关闭 IPTables 或允许相关端口上的流量是一种选择,但更简单/更合适的选择是让您的应用程序使用端口 80(用于 HTTP)或 443(用于 HTTPS)。您可以通过直接在应用程序上监听该端口来做到这一点,也可以通过让 Web 服务器充当您的反向代理(例如 NGINX 或 Apache)来实现。

【讨论】:

  • 如果布拉德没有回答,我会给你答案。他先发帖。
  • 我刚刚重新启用了防火墙。我会看看使用端口 443 或端口 80。
猜你喜欢
  • 2016-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-18
  • 2013-10-18
  • 2014-09-03
  • 2020-12-28
  • 2018-06-22
相关资源
最近更新 更多