无法命中EC2实例上托管的简单节点Web服务器

问题描述 投票:0回答:1

无法访问我在AWS中的ubuntu EC2上托管的简单节点Web服务器。但我看不出我错过了什么!我在AWS下面提供了屏幕截图 - 我缺少什么?请帮忙!。非常感谢,

节点代码

const http = require('http');

const hostname = '127.0.0.1';
const port = 8080;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World\n');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

命令提示符

$ node index.js 

命令提示响应

Server running at http://127.0.0.1:8080/

EC2实例

ec2 settings

安全设定

security settings

弹性IP设置

enter image description here

浏览器

http://"Public DNS (IPv4) value":8080/ 

更新

enter image description here

amazon-ec2 aws-security-group
1个回答
1
投票

选择类型时,选择“自定义TCP规则”:

Port Rules

并在端口范围字段中输入8080。

编辑

但是,这只会让你成为一部分。如果您注意到,您的服务器正在侦听IP地址127.0.0.1。这意味着它不是听外面的世界,只有localhost。要在服务器计算机外部访问它,您需要将代码更改为:

const http = require('http');

const hostname = '0.0.0.0';
const port = 8080;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World\n');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

更改的是,您现在正在侦听“所有接口”,而不仅仅是localhost。

© www.soinside.com 2019 - 2024. All rights reserved.