Search code examples
curlamazon-cloudfronthttp-caching

Why are two requests with different clients from the same computer cache misses on cloudfront?


My website uses cloudfront for its images. If I load a webpage in a browser, and then use curl to request an image on that page, then the request with curl is a cache miss. A subsequent request with curl is a cache hit.

example: run this twice:

curl --verbose https://d1hvwudqvjuczb.cloudfront.net/assets/landing/splash_dickenko-2199af423f275d0784c1813cbaab5645.jpg`

The first time, X-Cache will be "Miss from cloudfront", the second time it will be "Hit from cloudfront".

So it seems cloudfront is using some aspect of the request to decide if it should refetch the image or not.

What could this criteria be?


Solution

  • The reason you are seeing this behavior is because your requests are different. To give you some background, Cloudfront cache varys based on the http accept-encoding header. In your first example, in your browser, you are sending the value of gzip. With your curl example, by default, you are not. Therefore Cloudfront see's this objects as being unique.

    Try passing the --compressed flag in curl and see if the behavior matches your browser.

    http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/ServingCompressedFiles.html#CompressedCustomOrigin