专用子网中与EC2 Node.js服务器的连接超时

时间:2019-12-14 07:08:44

标签: node.js amazon-web-services nginx amazon-ec2 aws-security-group

架构图显示了我如何在AWS上设置应用程序。

如果Application Server组和Web Server组位于公共子网中,则一切正常。但是,当我在专用子网中放置应用程序服务器时,由于没有Internet网关连接到这些子网,所有API调用都会超时。

我希望Nodejs应用服务器仍然在私有子网中,而nginx Web服务器仍在公共子网中。 Web服务器组的应用程序负载平衡器面向Internet,但是App Server组的应用程序负载平衡器面向内部,而不面向Internet。

是否可以在保持应用程序服务器在私有子网中的同时使这种体系结构正常工作?

Application Architecture Diagram

Nginx设置如下。

server {
    listen 80;
    listen [::]:80;
    server_name  www.example.com;

    proxy_set_header X-Forwarded-Proto $scheme;
    if ( $http_x_forwarded_proto != 'https' ) {
        return 301 https://$host$request_uri;
    }

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
        proxy_pass https://api.example.com;
    }

    error_page  404              /404.html;
    location = /404.html {
        root   /usr/share/nginx/html;
    }

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

}

server {
    listen 443;
    listen [::]:443;
    server_name example.com;
    location / {
        proxy_pass http://api.example.com;
        root   /usr/share/nginx/html;
        index  index.html index.htm;
        }
}

53号公路

A记录设置

www.example.com A-面向负载平衡器DNS的Web服务器互联网

api.example.com A-应用服务器内部负载平衡器DNS

NACL设置

每个子网都设置了一个NACL。

入站

Inbound NACL

出站

Outbound NACL

Web负载平衡器安全组允许端口80和443上的所有流量 Web服务器安全组允许Web负载平衡器安全组的端口80和443上的所有流量 内部负载均衡器安全组中来自Web Server安全组的端口80和443以及来自App Server安全组的端口3000的所有流量 应用服务器安全组允许端口3000上的流量从内部负载均衡器安全组27017到MongoDB Atlas VPC对等连接,从HTTPS到VPC Gateway Enpoint for S3,从6379到Redis安全组。

是否有一种方法可以将应用程序服务器保留在专用子网中,并且在对api.example.com/abc端点进行任何调用时都不会出现此连接超时问题?

谢谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我不太了解您对专用子网的NACL规则所做的操作,也没有提及路由,但这可能会有所帮助: 在您的公共子网中添加一个NAT实例,并通过该NAT实例创建一个从您的私有子网到Internet的路由,创建一个NACL规则,允许从您的私有子网到Internet的出站连接,如果可行,首先允许所有一切尝试缩小您的出站范围仅连接到所需的端口。