How do I prevent CloudFront from caching certain files?
Last updated: 2021-01-19
I want Amazon CloudFront to pass through certain files, instead of caching the files at edge locations. How can I do that?
To prevent CloudFront from caching certain files, use one of the following configurations:
Configuration on the origin
Note: Be sure to update your CloudFront distribution's cache behavior to set Object Caching as Use Origin Cache Headers.
On your custom origin web server application, add Cache-Control no-cache, no-store, or private directives to the objects that you don't want CloudFront to cache. Or, add Expires directives to the objects that you don't want CloudFront to cache.
If you're using Amazon Simple Storage Service (Amazon S3) as the origin, you can add certain Cache-Control headers using object metadata.
Configuration on the distribution
- Open the CloudFront console.
- From your list of CloudFront distributions, choose the distribution that you want to modify.
- Choose the Behaviors tab.
- If you already have a cache behavior for the objects that you don't want CloudFront to cache, select the cache behavior and choose Edit. To create a new cache behavior, choose Create Behavior.
- For Cache and origin request settings, select Use legacy cache settings.
- In the cache behavior's settings, enter the following to prevent caching:
For Object Caching, select Customize.
For Minimum TTL, enter 0.
For Maximum TTL, enter 0.
- Choose Yes, Edit to save the changes that you made.
Note: If you aren't using an Amazon S3 bucket as your origin, you can set a specific cache behavior to forward all headers to the origin. To do this, update the cache behavior to set Cache Based on Selected Request Headers to All.