When CloudFront Keeps Requesting Content From Your Server

On the Faith Promise website we serve images using CloudFront. Our origin server uses PHP to resize our images. While trying to cache some dynamically generated CSS I noticed that CloudFront was requesting our static content on every request. You could see this in the response header. Instead of X-Cache saying, "Hit from cloudfront" it said, "Miss from cloudfront."

The issue for me was that Nginx (I suppose) was appending "private" to my Cache-Control header, so it looked like this:

Cache-Control:max-age=31536000, private

When it should be:

Cache-Control:max-age=31536000, public

or simply leave off "private."

The solution was to manually add "public" to the header. In PHP / Laravel, this was done using:


$response->header(

   'Cache-Control',

   'max-age=31536000, public'

);

return $response;