Speed up your websites and reduce server load with mod_expires

Speed up your websites and reduce server load with mod_expires

Being a drupal agency which provides Drupal support, we deal a lot with performance. In this post, I will discuss a simple setting which substantially increases load time of websites with images.

On Debian, Apache installed from repositories has mod_expires turned off by default. This seriously increases server load.

So what does mod_expires do? It "controls the setting of the Expires HTTP header and the max-age directive of the Cache-Control HTTP header"...

Let me explain.

The module allows apache to inform clients (eg. You browser) about the length of time a particular asset can treat as valid.

Drupal 7 in its .htaccess has these lines

For example:

<IfModule mod_expires.c>
  # Enable expirations.
  ExpiresActive On

  # Cache all files for 2 weeks after access (A).
  ExpiresDefault A1209600

  <FilesMatch \.php$>
    ExpiresActive Off

This piece of code (if you have mod_expires enabled), informs all clients that files which are not *.php files are valid for 2 weeks.

If your browser fetches an image from a server once, it will hold it in its cache and serve it from memory for 2 weeks, instead of fetching it from the server every time you load a page.

The biggest gain is usually achieved on images. All images for your website's template and all content images are requested only once every 2 weeks. On a website with 10 images, one user who visits the page every 3 days, makes a total of 10 requests instead of 50 during these 2 weeks. Assuming he visits 3 pages every single visit, you might save yourself 140 requests to Apache. 14 times less requests!!!

Without mod_expires Varnish underperforms

If you did not enable mod_expires, the varnish is seriously underperforming on your server.

In performance config on your Drupal website (admin/config/development/performance), you can set cache expiration for cached pages. This, however, works only for the page (HTML and for the css/js files).

Requests for images are separate requests and if mod_expire is disabled max-age directive nor EXPIRES heard are set on these files. Varnish treats them as files which are valid only for a single request and does not cache them. Each request is forwarded to Apache.

Back to our example, if we have a website with 10 graphical resources, which stands behind varnish, if you have mod_expires disabled, w visiting user will do:

  • min 2 requests to varnish (page and CSS file)  - headers set by Drupal
  • 10 requests to fetch images

If you turn on mod_expires, all 12 requests can be served by varnish, not even touching your hard drives.

Thanks to mod_expires, your varnish hit rate will improve significantly and your hard drives will last much longer.

So do you have mod_expires enabled?

If not then:

sudo a2enmod mod_expires sudo service apache2 reload

As part of Drupal support, we maintain existing websites and expand them with new functionalities