Apache mod expires and Puppet Syntax

Puppet is an awesome tool, but it can get pretty esoteric as it’s modules have configuration option after option. When you encounter parts that are seemingly little used the normal documentation can get thin. mod_expires for Apache took me a while to figure out, this post will save you some time.

puppet apache mod expires example
Woo! It works! Watch that jpeg MIME type though… see a later working revision below!

What are we trying to accomplish, anyway?

When optimizing a website with tools like http://webpagetest.org in order to get a good score on caching static content it becomes a necessity to configure Apache to flag files and images to expire after a certain amount of time. My first foray into this killed my prestashop instance so badly that I had to rebuild it. I was lazy and in trying to be too simple and using a default cache directive for everything the site became really unusable and something got really, really broken even after that directive was removed.. Thankfully restoring the site was scripted but it made me gun shy. That happened late in 2018.

Fast forward today and here I am trying to optimize my site for mobile and getting dinged hard on not having efficient via Pagespeed insights and Lighthouse. Once more into the breach – and this time no explosions – I promised my development team I would get those resolved, just like I figured out the Percona repo issue.

Just read the README

With most major Apache modules, there are examples of functional declarations in the README. mod_expires must not be used that much, although it seems essential to me, because even in the modules README file in the pulled down module itself were very terse.

The best resource I could find for the safer expires_by_type directive was this: https://www.rubydoc.info/github/puppetlabs/puppetlabs-apache

Even that lacked a code example under the mod::expires section, so let me provide you with one.

Puppet Apache Code Example – Mod Expires – Expires by Type

class { '::apache::mod::expires':
  expires_by_type => {
    'image/gif'                => 'access plus 12 hours',
    'image/png'                => 'access plus 12 hours',
    'image/jpeg'               => 'access plus 12 hours',
    'image/svg+xml'            => 'access plus 12 hours',
    'image/x-icon'             => 'access plus 2 weeks',
    'application/javascript'   => 'access plus 1 hour',
    'text/css'                 => 'access plus 1 hour',
    'image/vnd.microsoft.icon' => 'access plus 2 weeks',
  }
}

You need to add your own MIME types and set these expires values to something that truly makes sense for your application.

Leave a Reply

Your email address will not be published. Required fields are marked *