I want to specify the amount of time that my Amazon CloudFront distribution caches objects, so I added Cache-Control or Expires headers on the origin. However, CloudFront doesn't cache objects for the amount of time I specified, or CloudFront doesn't cache at all. Why is this happening?
Be sure that the directives that you set on the Cache-Control or Expires headers don't conflict with each other. As a best practice, use the Cache-Control max-age directive instead of the Expires header. If you specify values for both, then CloudFront uses only the value that you set for Cache-Control max-age.
Additionally, verify that the Minimum TTL, Default TTL, and Maximum TTL that you set on the CloudFront distribution don't conflict with the Cache-Control or Expires headers.
If these cache-duration settings conflict with each other, then CloudFront might not cache objects for the set time, or CloudFront may not cache at all. Avoid these examples of cache-duration settings that conflict with each other:
- If you set the Maximum TTL to 5 minutes (300 seconds) and the Cache-Control max-age header to 1 hour (3600 seconds), then CloudFront caches the object for 5 minutes instead of 1 hour.
- If you set the Cache-Control max-age header to 3 hours and the Expires header to 1 month, then CloudFront caches the object for 3 hours instead of 1 month.
- If you set the Default, Minimum, and Maximum TTL to 0 seconds, then CloudFront always verifies that it has the most recent content from the origin.
Important: If you update an origin asset with a caching header, then CloudFront reflects the change only after CloudFront makes a new request to your origin. CloudFront makes a new request to your origin when the cached asset expires in the edge locations. Then, your distribution receives a new request for the asset. To force CloudFront to reflect the update before the cached asset is set to expire, invalidate the cache.