Using mod_expires to speed up serving pages with apache

May 21, 2009

This is rails specific to a certain extend and involves setting the expires header on static asset to make browsers cache. This can be dangerous so is best used with timestamped css and javascript assets.

By default: rails helpers will add a timestamp onto the end of the asset so when you redeploy the file the browsers will recache the file because the timestamp has changed.

eg.

  <script src="/javascripts/all.js?1242919746" type="text/javascript"></script>

To setup mod_expires apache on debian based:

  sudo a2enmod expires

Then in your config

  ExpiresActive On
  <FilesMatch "\.(js|css)$">
    ExpiresDefault "access plus 1 year"
  

You can easily add image types to this as well but often css loads images and designer often forget to use these helper methods. So in the real world just doing it with js and css makes it safer to do with maximum performance gain.