Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cloudfront cache-control headers missing

I setup Cloudfront to serve static images, js, css files for www.lottoresults.ie. I used a custom origin server.

The cloudfront domain name I setup is icdn.lottoresults.ie. I set this up using Bind9 dns, with icdn.lottoresults.ie a CNAME for the cloudfront distro.

That's all fine.

However, for website performance optimization (Yslow, google pagespeed etc), I want to have Cache-control and Expires headers set, and I do not want Etags.

To do this, I have the following in my htaccess file for the doc_root of www.lottoresults.ie:

Header unset Pragma
FileETag none
Header unset ETag

<FilesMatch "(.*)\.(ico|jpg|jpeg|png|gif|js|css|swf)$">
ExpiresActive on
ExpiresDefault "access plus 1 year"
Header set Cache-Control "max-age=1864000, public"
Header unset Last-Modified
</FilesMatch>

The problem is that for assets coming from the cloudfront distro (icdn), I have no Cache-control or Expires headers, but I do have Etags - yet for www I do have Cache-control and Expires headers, and no Etags.

When I query an asset from my cloudfront domain, I have no Cache-control or Expires headers, but I do have Etags.

curl -I -L http://icdn.lottoresults.ie/images/green-header.jpg

HTTP/1.0 200 OK
Date: Sun, 01 Apr 2012 22:58:30 GMT
Server: Apache/2.2.9 (Debian) PHP/5.2.6-1+lenny13 with Suhosin-Patch
Last-Modified: Mon, 08 Aug 2011 20:23:00 GMT
**ETag**: "28a213-19b-4aa0436a1b100"
Accept-Ranges: bytes
Content-Length: 411
Content-Type: image/jpeg
Age: 22137
X-Cache: Hit from cloudfront
X-Amz-Cf-Id: AV55dR4_vWHtlTbU6E9M2tkh9reoAwZcHlD4y9csetK6B6Ey8gDPJA=
Via: 1.0 8adf6ec3585d73c680b4a6d5052988d8.cloudfront.net (CloudFront)
Connection: close

Yet when I query the same asset from my origin server, I get the following, which includes Cache-control, Expires, and no Etags.

curl -I -L http://www.lottoresults.ie/images/green-header.jpg

HTTP/1.1 200 OK
Date: Fri, 06 Apr 2012 20:50:08 GMT
Server: Apache/2.2.9 (Debian) PHP/5.2.6-1+lenny13 with Suhosin-Patch
Accept-Ranges: bytes
Content-Length: 411
**Cache-Control**: max-age=1864000, public
**Expires**: Sat, 06 Apr 2013 20:50:08 GMT
Content-Type: image/jpeg

Any help or advice greatly appreciated.

Thanks Paul

like image 994
pokero Avatar asked Apr 06 '12 21:04

pokero


People also ask

Does CloudFront add Cache-Control header?

Headers and distributions – overviewBy default, CloudFront doesn't consider headers when caching your objects in edge locations. If your origin returns two objects and they differ only by the values in the request headers, CloudFront caches only one version of the object.

What happens if there is no Cache-Control header?

Without the cache control header the browser requests the resource every time it loads a new(?) page.

How do I add a Cache-Control header?

If you want to enable Cache-Control for all files, add Header set line outside the filesMatch block. As you can see, we set the Cache-Control header's max-age to 3600 seconds and to public for the listed files.


1 Answers

I think I know what this was, for anyone else who might have same issue.

It is kind of PEBKAC - I have to invalidate the assets before they acquire new headers.

E.g. I added a new file, images/zoom2.gif, and it looks correct:

curl -I -L http://icdn.lottoresults.ie/images/zoom2.gif

HTTP/1.0 200 OK
Date: Fri, 06 Apr 2012 23:00:24 GMT
Server: Apache/2.2.9 (Debian) PHP/5.2.6-1+lenny13 with Suhosin-Patch
Accept-Ranges: bytes
Content-Length: 1849
Cache-Control: max-age=1864000, public
Expires: Sat, 06 Apr 2013 23:00:24 GMT
Content-Type: image/gif
Age: 29
X-Cache: Hit from cloudfront
X-Amz-Cf-Id:         vPPBdJpw0ZI_J2trHyECnE2UBeQO9NsQZD9GSvl3GIVAZuQN5lz1ZQ==,J4bBHfF5lKEpjRPLZVYpS9HUdAKEOLEe-Gy3-a_x4PsLPrdWuRg93Q==
Via: 1.0 00204466f749ba432b50dc49d604c534.cloudfront.net (CloudFront)
Connection: close
like image 101
pokero Avatar answered Oct 29 '22 15:10

pokero