【问题标题】:This site can’t be reached aws无法访问此站点 aws
【发布时间】:2021-02-14 21:51:35
【问题描述】:

我正在尝试在 AWS ec2 上部署 node.js 我关注了this tutorial 和我的代码

const express = require('express') ;
const app = express();
const port = process.env.PORT || 3000 ;

app.get('/', (req , res) =>{
  res.send('Connected');
});

app.listen(port, () => {
 console.log(`Example app listening at http://localhost:${port}`);

});

当我开始时它给了我

在 http://localhost:3000 监听的示例应用

但是当我尝试从浏览器访问它时,我无法访问它 知道我能做什么或去哪里看吗??

【问题讨论】:

  • 您是否按照教程的安全组步骤进行操作?你知道什么是安全组吗?
  • 您的应用程序不应该监听所有接口,如app.listen(3000, '0.0.0.0'); 或非常具体的your.ec2.ip.address:3000 而不是localhost 以允许您从互联网访问? + 你的安全组应该允许端口3000上的传入流量
  • 谢谢,这是一个入站规则问题,我没有把端口范围的源放在正确的地方。

标签: node.js amazon-web-services amazon-ec2


【解决方案1】:

您需要在实例的安全组中配置入站和出站规则。 https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/authorizing-access-to-an-instance.html

【讨论】:

    【解决方案2】:

    我假设您已经按照您提到的指南添加了必要的安全组规则以允许端口 3000 上的传入流量。

    您的应用应该监听所有接口或 ec2 实例的 IP 地址,而不是 localhost. localhost 解析为 127.0.0.1,这是一个无法通过 Internet 访问的环回接口。

    documentation

    [app.listen([port[, host[, backlog]]][, callback])]

    const express = require('express') ;
    const app = express();
    const port = process.env.PORT || 3000 ;
    
    app.get('/', (req , res) =>{
      res.send('Connected');
    });
    
    app.listen(port, '0.0.0.0',() => {
     console.log(`Example app listening at http://localhost:${port}`);
    
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-10
      • 2021-04-09
      • 1970-01-01
      • 2017-12-07
      • 2017-03-29
      • 2021-02-09
      • 2021-09-22
      相关资源
      最近更新 更多