I want to configure Amazon CloudFront cache policies and origin request policies to control cache behavior and forward requests to the origin.
Resolution
Configure a cache policy
You can create custom cache policies, or use managed cache policies. To create a custom cache policy or attach a cache policy to a distribution, see Create cache policies.
For an overview of cache policies, see Understand cache policies.
For information about how to use policies with cache keys, see Control the cache key with a policy.
Configure an origin request policy
You can create custom origin request cache policies, or use managed origin request cache policies. To create a custom origin request policy or attach an origin request policy to an existing or new distribution, see Create origin request policies.
For an overview of origin request policies, see Understand origin request policies.
For information about how to use policies with cache keys, see Control origin requests with a policy.
Use cache policies and origin request policies together
Cache policies control caching behavior based on the cache key and forward the information to the origin, and origin request policies only forward requests. To use an origin request policy in a cache behavior, you must also use a cache policy.
For example, you want to cache requests based on a specific query string parameter and forward all query strings to the origin. Create a cache policy that includes only the query string parameter that you specified for the cache key. Then, create an origin request policy that includes all query strings. Attach the cache policy and the origin request policy to the cache behavior.
In the preceding example, the specified parameter optimizes the cache, but the origin receives all query strings to process. For more information, see Understand how origin request policies and cache policies work together.
Migrate from legacy cache settings
It's a best practice to use a cache policy or origin request policy instead of the CloudFront legacy cache settings.
To migrate from legacy cache settings to a cache policy or origin requests policy, complete the following steps:
- Open CloudFront console.
- In the navigation pane, choose Distributions, and then select your distribution.
- Choose the Behaviors tab, and then choose the behavior.
- Choose Edit.
- Under Cache key and origin requests, choose Cache policy and origin request policy (recommended).
- Choose the Cache policy dropdown list, and then select a cache policy. Or, choose the Origin request policy dropdown list, and then select an origin policy.
- (Optional) Choose the Response headers policy dropdown list, and then choose a response header.
- Choose Save changes.
Related Information
How do I cache content in CloudFront based on HTTP headers?
How do I change my CloudFront configurations so that certain files aren't cached?
How do I configure CloudFront to forward the host header to the origin?
How can I configure CloudFront to forward the Authorization header to the origin?