Cache-Control Dropped on Files from S3 Subfolders

0

I set up a CloudFront distribution linked to a public S3 bucket that contains files in the bucket's root as well as in subfolders. All S3 files have Cache-Control set to "public, max-age=31536000" in their S3 metadata. Accessing the files from Chrome directly from S3 yields Response Header "cache-control: public, max-age=31536000" on all of them. However, when accessing via the CloudFront distribution, only the files located in the bucket's root come back with the cache-control in the Response Header, and those from the subfolders come back without this Response Header property. I changed the Object Caching from "Use Origin Cache Headers" to "Customize" - no effect. Is this a CloudFront bug or am I missing something?

svdev
asked 5 years ago1456 views
5 Answers
0
Accepted Answer

You need to invalidate your cache...that's probably the issue..check out the invalidation tab in cloudfront... distribution settings. It takes 15 or so minutes per invalidation.

Also, when testing, be sure you are getting a HIT from Cloudfront. Because it's a CDN, even after the first try after getting a HIT, you could receive a miss, especially when the site is first getting hit. Think of 1000s of servers across multiple regions that need to get the content on their first request and cache it...

answered 5 years ago
0

Do you have a single behavior?
Can you share all of your Default Cache Behavior Settings settings?

answered 5 years ago
0

Yes, single behavior set up following the https://console.aws.amazon.com/cloudfront/home?region=us-west-2#create-distribution: page. Kept most of the options default, except picked Redirect HTTP to HTTPS. Later, as noted, set Object Caching to Customize.

Path Pattern Default (*) Origin or Origin Group my-s3-bucket
Viewer Protocol Policy Redirect HTTP to HTTPS
Allowed HTTP Methods GET, HEAD
Field-level Encryption Config blank
Cached HTTP Methods GET, HEAD (Cached by default)
Cache Based on Selected Request Headers None (Improves Caching)
Object Caching Customize
Minimum TTL 0
Maximum TTL 31536000
Default TTL 31536000
Forward Cookies None (Improves Caching)
Query String Forwarding and Caching None (Improves Caching)
Smooth Streaming No
Restrict Viewer Access No
Compress Objects Automatically No
Lambda Function Associations blank

svdev
answered 5 years ago
0

I'm noticing the cache-control header missing on some files from the root of another S3 bucket I set up with CloudFront. I set the metadata in bulk selecting all the files in the S3 bucket, and I see that each file has the cache-control present in its metadata. But, somehow, 2 files out of about 20 get delivered to the browser without the cache-control header via the CloudFront link - the others work fine. Makes no sense ... I'm thinking I might've set the S3 metadata after initiating the CloudFront distribution - so, possibly, some files got into the CDN cache before the tags were recorded in S3, and some after, but it's a far shot explaining the misbehavior. Not sure if there is a way to "re-push" the files into the CDN at this point to test out if they all get recorded with the proper tags.

svdev
answered 5 years ago
0

After a couple of days, CloudFront displays proper cache-control headers on all the S3 source files. Got to be related to the lag how this feature is handled, considering the internal CF caching. So, it looks like the S3 metadata must be set before the CF distribution is created, or the files must be invalidated explicitly in the CF. I assumed, the caching in CF would apply to the content of the files only. Looks like it applies to more than that. If this is documented somewhere - my bad missing. Although, it certainly makes sense if CF behaves as a fully independent layer that reaches back to the origin for anything only if its internal caching is empty.

Edited by: svdev on Feb 25, 2019 12:52 AM

svdev
answered 5 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions