Speed up your websites and reduce server load with mod_expires
Speed up your websites and reduce server load with mod_expires
19.04.2013

On Debian, apache installed from repositories, has mod_expires turned off by defauls. This seriusly 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 treated as valid.

For example

Drupal 7 in its .htaccess has these lines:

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

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

  <FilesMatch \.php$>
    ExpiresActive Off
  </FilesMatch>
</IfModule>

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, istead 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 requeted only once every 2 weeks. On a website with 10 images, one user who vistis the page every 3 days, makes a total of 10 requsts 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!!!

Withour mod_expires Varnish underperforms

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

In performace config on your drupal website (admin/config/development/performance), you can set cahce 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 heared 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 reauests to varnish (page and css file)  - headers set by drupal
  • 10 requests to fetch images

If you turn on mod_expires, all 12 reauests 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

Need a team of Drupal and PHP web development experts?

Contact us now!