简体   繁体   English

如何配置AWS EC2实例中的Node.js以接受来自客户端的HTTPS请求

[英]How to configure Nodejs that is in AWS EC2 Instance to accept HTTPS request from client side

I would like to ask about how to configure Nodejs (backend/server) to accept HTTPS request from client side (Front end). 我想问一下如何配置Node.js(后端/服务器)以接受来自客户端(前端)的HTTPS请求。

What we did. 我们做了什么。

  • Registered domain name in AWS. 在AWS中注册的域名。
  • List item 项目清单
  • Requested SSL in ACM. 在ACM中请求的SSL。
  • Create bucket in S3, and store our front-end code (angular 5) inside of it. 在S3中创建存储桶,并将我们的前端代码(角度5)存储在其中。
  • Created distribution in cloud front and put our custom ssl there and connect to bucket in S3. 在Cloud Front中创建了分发,并将我们的自定义SSL放到那里,并连接到S3中的存储桶。
  • We set up also ec2 instance and store our back-end code (node js) there. 我们还设置了ec2实例,并将后端代码(节点js)存储在此处。
  • In our front end code we connect to the ip of our ec2 instances so that we can connect to backend. 在前端代码中,我们连接到ec2实例的ip,以便可以连接到后端。

The problem: 问题:

  • The front-end can't access the backend in ec2 instances because the front end is https and the backend is http (we don't know how to configure it from http to https in AWS EC2 Instance). 前端无法访问ec2实例中的后端,因为前端是https,后端是http(我们不知道如何在AWS EC2实例中将其从http配置为https)。
  • Do you know how to setup web app in aws which front end and backend code is separated? 您知道如何在前端和后端代码分开的AWS中设置Web应用程序吗?
  • What did we missed? 我们错过了什么?

What did we missed? 我们错过了什么?

If I understand you correctly, you have a Cloudfront distribution serving angular which is then attempting to connect to an EC2 instance - I presume the IP address or public DNS entry for the EC2 is hard-coded into the angular code. 如果我正确理解,则您有一个提供角度服务的Cloudfront发行版,然后尝试连接到EC2实例-我认为EC2的IP地址或公共DNS条目已硬编码为角度代码。

This is not a good arrangement - if your EC2 goes down or the IP address changes you will need to push a new site to S3 - and then this change will take time to propagate through Cloudfront. 这不是一个很好的安排-如果您的EC2发生故障或IP地址更改,则需要将新站点推送到S3-然后此更改将需要一些时间才能通过Cloudfront传播。

What you should rather be doing is this. 您应该这样做的是。

  1. create an application load balancer 创建一个应用程序负载均衡器
  2. create a target group and add your EC2 to that target group. 创建目标组并将您的EC2添加到该目标组。
  3. add a listener on the ALB, listening on the port your web app connects on, with a rule that forwards to the HTTP port of the back-end EC2. 在ALB上添加侦听器,侦听Web应用程序所连接的端口,并使用转发到后端EC2的HTTP端口的规则。
  4. Add a route 53 DNS Alias record for the ALB (because ALBs do sometimes go away or change their IP address) 为ALB添加路由53 DNS别名记录(因为ALB有时会消失或更改其IP地址)
  5. Change your front-end code to point at the Route 53 Alias record. 更改您的前端代码,以指向Route 53 Alias记录。

(This is an incredibly simplistic way of doing things that leaves your EC2 open to the internet etc etc). (这是一种非常简单的处理方式,使您的EC2可以访问互联网等)。

You should also give serious thought to putting your EC2 into an autoscaling group that spans at least two availability zones, and to setting its minimum size to ensure at least servers are running at any one time. 您还应该认真考虑将EC2放入一个跨越至少两个可用性区域的自动伸缩组,并设置其最小大小以确保至少任何一次服务器都在运行。

AWS EC2 instances can go away at any time, and when they do your app goes down with them. AWS EC2实例可以随时消失,当它们运行时,您的应用程序也会随之消失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM