Node.js无法从外部IP访问

Node.js is not accessible from external IPs

本文关键字:IP 访问 从外部 js Node      更新时间:2023-09-26

我有一个亚马逊EC2服务器,我在那里配置了节点js服务器,公共dns无法从外部访问它,但可以由同一实例(localhost)访问它。任何帮助都将不胜感激。

服务器代码:

var my_http = require("http");
my_http.createServer(function(request,response){
    console.log("Request Recieved : "+request.url);
    response.writeHeader(200, {"Content-Type": "text/plain"});
    response.end("Server is UP 'n"); 
}).listen(3000, "0.0.0.0");
console.log("Server is Running on Port 3000");

netstat输出

netstat -tupln | grep :3000  
tcp    0    0 0.0.0.0:3000     0.0.0.0:*    LISTEN   11706/node 

卷曲输出

curl http://127.0.0.1:3000                =>     Server is UP 
curl http://localhost:3000                =>     Server is UP 
curl http://{PRIVATE_IP}:3000             =>     Server is UP 
curl http://{PUBLIC_DNS}:3000             =>     curl: (52) Empty reply from server

这听起来确实像是一个安全组问题。

在EC2控制面板中,找到您的实例并一直向右滚动。有一个安全组列,如果您没有更改默认名称,您的安全组将被称为"launch-wizard-1"。

单击它,将打开一个筛选后的安全组列表(列表中可能只有一个条目)。

右键单击并选择"编辑入站规则"。

添加一条规则:

Type: HTTP
Protocol: TCP
Port Range: 3000
Source: Anywwhere

保存,它应该可以工作。