简体   繁体   中英

Cloudfront not caching despite Cache-Control

I have a Cloudfront distribution that points to a server that resizes images from an S3 bucket. The server returns a Cache-Control header with public, max-age=31104000 , but I keep getting miss from Cloudfront so the images are not cached in Cloudfront.

To test, I set up another Cloudfront distribution that points directly to the S3 bucket, and that indeed caches the images with hit from Cloudfront .

I compared the request and response headers, and the only difference is that the miss has a Last-Modified header response, which is missing from the good request. However, I'm not sure that's the problem.

Do you have any idea of what is happening? Here are the headers:

Good Response:
Age:10
Cache-Control:public, max-age=31104000
Connection:keep-alive
Date:Sat, 05 Sep 2015 16:15:51 GMT
ETag:"51dbe11a5fa320c4495221b69df1d860"
Server:AmazonS3
Via:1.1 b7bc8b4c398aa9f5a08980055c497334.cloudfront.net (CloudFront)
X-Amz-Cf-Id:4KE-cAoFrTmcjkRW3goZAImvyQE5i1Vl4KZyrWL8HyCk6GcJZ9N7Kw==
X-Cache:Hit from cloudfront

Bad Response:
Cache-Control:public, max-age=31104000
Connection:keep-alive
Date:Sat, 05 Sep 2015 16:16:42 GMT
ETag:"51dbe11a5fa320c4495221b69df1d860"
Last-Modified:Sat, 05 Sep 2015 16:07:41 GMT
Server:nginx/1.6.2
Via:1.1 45578d14a69df96accaab0d1aba82a5a.cloudfront.net (CloudFront)
X-Amz-Cf-Id:FGMRwDI0vOJQ7aPj83s-RGTO0fg_Zesu7FLo7Ia5vwCxcTZI97ri5A==
X-Cache:Miss from cloudfront

Good Request:
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0    .8
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8,da;q=0.6
Cache-Control:max-age=0
Connection:keep-alive
Host:assets.runemadsen.com
If-Modified-Since:Sat, 05 Sep 2015 16:07:41 GMT
If-None-Match:"51dbe11a5fa320c4495221b69df1d860"
Upgrade-Insecure-Requests:1
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (    KHTML, like Gecko) Chrome/45.0.2454.85 Safari/537.36

Bad Request:
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0    .8
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8,da;q=0.6
Cache-Control:max-age=0
Connection:keep-alive
Host:images.runemadsen.com
If-Modified-Since:Sat, 05 Sep 2015 16:07:41 GMT
If-None-Match:"51dbe11a5fa320c4495221b69df1d860"
Upgrade-Insecure-Requests:1
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (    KHTML, like Gecko) Chrome/45.0.2454.85 Safari/537.36

Any help appreciated.

Thanks for the comments. Here's the final update: The reason was that an older response without the Cache-Control was cached in Cloudfront, which somehow made cloudfront continously reload the image from the origin. I'm not sure exactly how a cached response can trigger constant "miss", but that's what happened. I invalidated the route, and Cloudfront started cahing the images.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM