Sub topic 7
Cache behaviors and path patterns
This chapter assumes you can read the distribution and origin setup from the earlier chapters, and that you know basic HTTP paths and query strings.
What a cache behavior is
A cache behavior is a rule inside a distribution. It says: for requests whose path matches this pattern, send them to this origin and handle them with these settings. Each behavior sets:
- the path pattern
- the target origin
- the viewer protocol policy (for example
redirect-to-https) - the allowed HTTP methods
- the cache policy, which is covered in the next chapter
Every distribution has one default behavior with the pattern *. It catches any request that no other behavior matches. You can add more behaviors when different parts of your site need different treatment. For example, long-lived images and a frequently changing JSON file.
How CloudFront picks a behavior
CloudFront compares the request path, without the domain name and without the query string, against your behaviors in the order they are listed. The first match wins. The default behavior is always checked last.
Writing path patterns
Patterns use two wildcards. * matches zero or more characters, and ? matches exactly one character. Matching is case-sensitive, so /Images/* does not match /images/logo.png. A leading slash is optional in the pattern.
| Pattern | Matches | Does not match |
|---|---|---|
| `assets/*` | `/assets/app.js`, `/assets/img/logo.png` | `/Assets/app.js` |
| `*.jpg` | `/photos/cat.jpg` | `/photos/cat.jpeg` |
| `data/v?.json` | `/data/v1.json` | `/data/v10.json` |
Because the first match wins, put the most specific patterns first. If *.jpg is listed above assets/private/*, a JPEG under assets/private/ uses the *.jpg behavior.
Example: two behaviors for one bucket
This excerpt belongs inside DistributionConfig of the distribution from the previous chapter. The default behavior serves pages, and a second behavior serves fingerprinted assets.
DefaultCacheBehavior:
TargetOriginId: s3-origin
ViewerProtocolPolicy: redirect-to-https
CachePolicyId: 4135ea2d-6df8-44a3-9df3-4b5a84be39ad # CachingDisabled
CacheBehaviors:
- PathPattern: assets/*
TargetOriginId: s3-origin
ViewerProtocolPolicy: redirect-to-https
CachePolicyId: 658327ea-f89d-4fab-a63d-7e88639e58f6 # CachingOptimizedBoth behaviors point at the same origin. Only the caching differs. Pages are fetched from S3 on every request, while files under assets/ are cached at the edge.
Using one origin for several behaviors is common. You can also add more origins, such as a second bucket, and route a path to each.
Common errors
- Wrong order. A broad pattern placed above a narrow one hides the narrow one. Reorder the behaviors.
- Case mismatch. S3 keys and patterns are case-sensitive. A pattern that matches nothing silently falls through to the default behavior.
- Query strings in patterns. The pattern never sees the query string, so
*.js?v=2will not work. Use a cache policy to control query strings. - Missing origin access. A behavior that targets a bucket the Origin Access Control policy does not cover returns
403errors.
Exercise
A distribution has behaviors in this order: *.png, images/private/*, and the default *. A viewer requests /images/private/id.png. Which behavior applies, and how would you make images/private/* apply instead?
Show answer
The *.png behavior applies, because behaviors are checked in order and the first match wins. To make images/private/* apply, move it above *.png in the list.