Search code examples
amazon-s3amazon-cloudfront

CloudFront ignoring Cache-Control header from S3


CloudFront is not evicting my objects when it is supposed to:

(This is the reverse problems from this one, where CloudFront is evicting objects early, as it is entitled to do.)

Here is one typical session:

% curl -v -o /dev/null https://mycfdomain.me/file.json
< HTTP/2 200
< content-type: application/json
< content-length: 64
< date: Fri, 04 Dec 2020 13:17:32 GMT
< last-modified: Fri, 04 Dec 2020 04:08:04 GMT
< etag: "782c85948b6f309eaa1a483e5a535ecf"
< cache-control: public; max-age=60
< x-amz-version-id: XE56pcaQJCG.M_UGK2T_.eWgDMtOggZt
< accept-ranges: bytes
< server: AmazonS3
< x-cache: Hit from cloudfront
< via: 1.1 c1caaceb6655a57ae014aef7bc8ec389.cloudfront.net (CloudFront)
< x-amz-cf-pop: SFO20-C1
< x-amz-cf-id: C2YhfjBOOMpwpkxEg49vlP14vk4ijEsYNgXwkJdcIoK4YjsaqkCLxg==
< age: 327
<

It’s returning on object it candidly admits is over-age (327 seconds), claiming it is a "hit from cloudfront", which it should not be.

Here is the equivalent session, going directly to S3.

 c % curl -v https://mys3domain.s3.amazonaws.com/file.json
< HTTP/1.1 200 OK
< x-amz-id-2: I7mSgXtStAW7cgMqzxFvYwG78lH+BjjXp1pwuzdSYFwaK0w9s6v6IKGJVbuHY9DP6xkmvIvDnbg=
< x-amz-request-id: 6D366A4F89FBD06D
< Date: Fri, 04 Dec 2020 13:25:46 GMT
< Last-Modified: Fri, 04 Dec 2020 13:21:23 GMT
< ETag: "587bcf7b0f22d8b128b0f826e93bf18b"
< Cache-Control: public; max-age=10
< x-amz-version-id: iaHtXqm0jurqDyq2kyNDM1yShpyMaM7c
< Accept-Ranges: bytes
< Content-Type: application/json
< Content-Length: 117
< Server: AmazonS3
<

I checked the minimum TTL on the CF cache-policy I was using: it was set to 1 second (and it shouldn’t matter, since Cache-Control is set.)


Solution

  • The error is here:

    < Cache-Control: public; max-age=10
    

    It should be a comma, not a semi-colon, like this:

    < Cache-Control: public, max-age=10