简体   繁体   English

基于路径的Cloudfront和EC2路由

[英]Path based routing to cloudfront and ec2

So currently we have two ec2 instances (lets say A and B) and a cloudfront. 因此,当前我们有两个ec2实例(分别说A和B)和一个云前。

If the user goes to www.appdomain.com/app the user should get routed to the cloudfront SPA page. 如果用户访问www.appdomain.com/app,则应将其路由到cloudfront SPA页面。 However if the user goes www.appdomain.com the user should be routed to the EC2 instance A, and if user goes to www.appdomain.com/api be routed to EC2 instance B. 但是,如果用户访问www.appdomain.com,则应将其路由到EC2实例A,如果用户访问www.appdomain.com/api,则应将其路由到EC2实例B。

All of these applications must be on the same domain. 所有这些应用程序必须位于同一域中。

Now we found out how to set path rules using an application load balancer, but would like to know how to set it to cloudfront as well. 现在,我们了解了如何使用应用程序负载平衡器设置路径规则,但也想知道如何将其设置为Cloudfront。

Update: So in summary the question is how do we route /app to cloudfront / and /api to ec2. 更新:所以总的来说,问题是我们如何将/ app路由到cloudfront /和/ api路由到ec2。

All of these applications must be on the same domain. 所有这些应用程序必须位于同一域中。

In this scenario, every request for that domain must pass through CloudFront first . 在这种情况下,对该域的每个请求都必须首先通过CloudFront。

Your DNS record will need to point to CloudFront (not the ALB) and CloudFront is then responsible for routing the request to the appropriate target -- to an EC2 instance via an ALB, to an S3 bucket, to wherever you need the requests to go -- and each of these things is called a content origin . 您的DNS记录将需要指向CloudFront(而不是ALB),然后CloudFront负责将请求路由到适当的目标-通过ALB到EC2实例,到S3存储桶以及您需要将请求转到何处-这些都被称为内容来源

Once the origins are specified by their individual domain name (not your site's domain name, but a domain name specifically for the resource in question), you define CloudFront path patterns to select which origin is to receive the request for each pattern (eg /api* ). 一旦通过其各自的域名(不是您网站的域名,而是专门用于所涉及资源的域名)指定了来源,就可以定义CloudFront 路径模式以选择接收每个模式请求的来源(例如/api* )。

Once your DNS is changed to point to CloudFront, all requests go there first, and are handed off to the next service, unless CloudFront has a cached copy of the requested object -- in which case, CloudFront will serve it from its cache, and nothing will be sent to the origin. 将您的DNS更改为指向CloudFront之后,所有请求都将首先发送到CloudFront,然后移交给下一个服务,除非CloudFront拥有所请求对象的缓存副本-在这种情况下,CloudFront将从其缓存中为其提供服务,并且什么都不会发送到原点。

You can't route from ALB to CloudFront, but you can route from CloudFront to ALB. 您不能从ALB路由到CloudFront,但是可以从CloudFront路由到ALB。

You can't subdivide a domain into multiple, different path-based content origins without using a reverse proxy that is able to match the paths and fetch the content on behalf of the requester -- HTTP and DNS don't support such functionality. 如果不使用能够匹配路径并代表请求者获取内容的反向代理,则无法将域细分为多个不同的基于路径的内容来源,HTTP和DNS不支持此类功能。 CloudFront, in addition to providing the CDN service, is also a reverse proxy. 除了提供CDN服务之外,CloudFront还是反向代理。

ALB, of course, is also a reverse proxy, but does not support as many different types of content origins as CloudFront does -- ALB only supports EC2 instances, servers in your data center (in which case, ALB must have a VPN path in order to reach them), and Lambda functions as content origins. 当然,ALB也是反向代理,但不支持CloudFront所支持的多种不同类型的内容来源-ALB仅支持EC2实例,数据中心中的服务器(在这种情况下,ALB必须在其中具有VPN路径)以达到目标),而Lambda则作为内容来源。 CloudFront can use literally anything as a content origin as long as it speaks HTTP/HTTPS and is accessible via the Internet. 只要使用HTTP / HTTPS并且可以通过Internet进行访问,CloudFront便可以将任何内容用作内容来源。 (To choose a somewhat random example, CloudFront can even use a service from another vendor -- like a Google Cloud Storage bucket -- as a content origin, if that was something you needed to do, for whatever reason... because these are accessible via HTTP across the public Internet.) (要选择一个随机的示例,CloudFront甚至可以使用来自其他供应商的服务(例如Google Cloud Storage存储桶)作为内容来源,如果出于某种原因您需要这样做,因为这些都是可以通过HTTP通过公共互联网访问。)

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

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