Search code examples
amazon-web-serviceshttpamazon-cloudfront

How do I stop Cloudfront from caching a HEAD request independently of the GET request?


Cloudfront is returning cache hits for HEAD responses which don't have corresponding cached content.

A normal example looks like this...

  1. GET /path (miss)
  2. GET /path (hit)
  3. HEAD /path (hit)

This is fine, and what we expected. It means we can estimate if a user is likely to hit the cache or will have to wait for content to be generated by the origin.

However, we are also observing this.

  1. HEAD /path2 (miss)
  2. HEAD /path2 (hit)
  3. GET /path2 (miss)

Which is a problem because we aren't able to differentiate between HEAD responses which were cached in their own right and HEAD responses which are linked to corresponding GET responses.

Is there a way we stop Cloudfront from returning cache hits on HEAD requests for content which isn't actually cached?


Solution

  • Disabling cache headers on HEAD responses at your origin will prevent Cloudfront caching the HEAD response itself. So if you issue a HEAD request to the CDN it will look for a matching object in the cache and return a cache hit if it is present. If the content is missing it will forward the HEAD request to your origin, but will not cache the response.