首页 > 解决方案 > 与 AWS EC2 上的基本 HTTP 服务器的连接被拒绝

问题描述

我知道有很多关于这个主题的资源,但我认为我做的一切都正确,但我仍然无法连接到我的服务器。

我在端口 80 上启动了一个简单的 node.js 服务器。

sudo netstat -tnlp | grep 80
tcp        0      0 127.0.0.1:80            0.0.0.0:*               LISTEN      3657/node   
curl localhost:80
Welcome Node.js

我已经为此实例以及 VPC 配置了安全组以允许流量。 安全规则

我确保没有本地防火墙并且 VPC ACL 没有阻塞流量(不是我所期望的,因为这是一个全新的实例。)

service iptables status
Redirecting to /bin/systemctl status iptables.service
Unit iptables.service could not be found.

当我尝试从本地机器连接时的输出:

 curl 3.xxx.xxx.xxx
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:--  0:00:03 --:--:--     0
curl: (7) Failed to connect to 3.xxx.xxx.xxx port 80: Connection refused

关于接下来要检查的内容还有其他想法吗?

标签: node.jsamazon-web-servicesamazon-ec2

解决方案


我的问题的答案是https://stackoverflow.com/a/14045163/2369000。我复制的样板代码使用了一种仅侦听来自 localhost 的请求的方法。这可能是从 netstat 输出中检测到的,它表示127.0.0.1:80监听地址。答案是使用.listen(80, "0.0.0.0")或只是.listen(80)因为默认行为是侦听来自任何 IP 地址的请求。


推荐阅读