43

i have 2 different images in 2 websites at:

If i create an Amazon Cloudfront distribution with 2 origins: www.siteA.com and www.siteB.com and then i call for uniqueDistributionID.cloudfront.net/avatar.png, then which avatar.png will be returned? The one in siteA or the one in siteB?

Why & why not?

Trying to understand the potential of conflicts in Cloudfront distributions.

Chad Johnson
  • 21,215
  • 34
  • 109
  • 207
Rakib
  • 12,376
  • 16
  • 77
  • 113

1 Answers1

60

No, CloudfFront doesn't have a concept of a "conflict," because when you have a distribution with multiple origins, you have to define which path matches go to which origin.

CloudFront's path pattern matching is deterministic. It uses first match, not best match. Whichever pattern matches first is the one that will be used, even if that path is a dead-end at the origin server.

When CloudFront receives an end-user request, the requested path is compared with path patterns in the order in which cache behaviors are listed in the distribution. The first match determines which cache behavior is applied to that request.

http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/distribution-web-values-specify.html#DownloadDistValuesPathPattern

Update

CloudFront now supports a concept of Origin Groups, which allow any given Cache Behavior to send a request to one origin, and then -- if one of the error types that you specify (e.g. 404 or 503) is returned by the first origin, then CloudFront will attempt to fetch the content from a second origin. This can be used for failover, but it can also be used for cases where you want CloudFront to try one origin, and then another. The two origins in the origin group are tried, in order, for every cache miss. If either origin returns a cacheable response, that response will be stored in the cache.

Michael - sqlbot
  • 169,571
  • 25
  • 353
  • 427
  • 1
    "when you have a distribution with multiple origins, you have to define which path matches go to which origin" -- where do I define these paths? The Cloudfront "origins" tab only has path for origins. Not the path for Cloudfront URL. – PKHunter Feb 11 '17 at 11:45
  • 4
    @PKHunter the path matching is done in **Cache Behaviors**. The cache behaviors match path patterns to origins, and they are evaluated in the order shown when processing requests. One cache behavior exists by default, and it's the "default" behavior that matches `*`. This one is of course always last on the list for evaluation. For each path pattern, you create a new behavior. Don't use the "origin path" setting under origins unless you need to *prepend* something to the request path when sending the request to the origin -- this configuration is not common. – Michael - sqlbot Feb 11 '17 at 16:36
  • 1
    Using Origin Groups does indeed work very nicely! I have a CloudFront distribution for my domain.com with multiple origins and behaviors. I want to use one origin to serve some top-level routes and the default origin to serve all other top-level routes (including 404). An origin group lets me do exactly this. – Chad Johnson Aug 15 '19 at 00:59
  • path_pattern in terraform docs https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudfront_distribution#path_pattern – Volodymyr Kozubal Jan 04 '23 at 20:55