TIL: Expires required

Published 2016-03-16 by muhh

We use the following configuration snippet in our Apache VirtualHosts on freistilbox:

# Cache TTL
<IfModule mod_expires.c>
  ExpiresActive On
  ## default: 4h
  ExpiresDefault A14400
  ## text/html: 15min
  ExpiresByType text/html A900

This snippet takes care of proper HTTP caching headers when the web application doesn’t set caching headers itself. The values used here enable Varnish to cache files for 4 hours, HTML content for 15 minutes.

Today I learned that if you want to override this, it’s not enough to simply set the header Cache-control: max-age=600 to change the caching time to 10 minutes. mod_expires is quite strict in its interpretation of the following lines:

When the Expires header is already part of the response generated by the server, for example when generated by a CGI script or proxied from an origin server, this module does not change or add an Expires or Cache-Control header.

When you only set Cache-Control: max-age=600, the web server adds another Cache-Control: max-age=900 and an Expires header. Confusing, right?

To properly control both headers, you have to set an explicit Expires and everything works as expected.