Apache expires module

ApacheThe Apache expires module, also known as mod_expires, allows you to fine tune “Expires” and “Cache-Control” HTTP headers sent with files served by Apache. These HTTP headers provide information for browsers and caching proxy servers on how long to cache content served by Apache.

Loading mod_expires

Before you can use mod_expires you have to make sure it is loaded. On CentOS Linux you can run the following command to see if mod_expires is loaded with Apache:

httpd -M | grep expires

If the expires module is not loaded you can load it by editing /etc/httpd/conf/httpd.conf and adding this line:

LoadModule expires_module modules/mod_expires.so

Expires module options

The expires module has the following key options:

  • ExpiresActive: Determines whether the module is actively modifying headers or not. Set to “ExpiresActive on” to switch it on.
  • ExpiresDefault: Set default expires and cache-control headers for all file types.
  • ExpiresByType: Set headers based on the mime type of the content being served.

These options can be configured server wide or for specific virtual hosts or directories.

Example configuration of the expires module

On CentOS Linux all configuration files placed in the /etc/httpd/conf.d folder are automatically loaded by Apache. So it makes sense to create a new file for a server wide configuration of the expires module in that directory. This file will have expires module options in an IfModule block:

<IfModule mod_expires.c>
... #add expires module options here
</IfModule>

Let’s see an example configuration that instructs clients to cache images for 6 months from the date of access:

ExpiresActive On
ExpiresByType image/gif "access plus 6 months"
ExpiresByType image/jpeg "access plus 6 months" 
ExpiresByType image/png "access plus 6 months"

You can also set an expires header relative to the date of file modification by replacing “access” with “modification”.

You may also want to cache CSS files and Javascript files:

ExpiresByType text/css "access plus 6 months"
ExpiresByType text/javascript "access plus 6 months"
ExpiresByType application/javascript "access plus 6 months"
ExpiresByType application/x-javascript "access plus 6 months"

Depending on how your server is configured you may or may not have to include the application/javascript and application/x-javascript lines above.

Once you’ve added and saved the configuration file be sure to restart Apache to make the configuration active:

service httpd restart

Testing expires and cache-control headers

To test whether the expires and cache-control headers are being sent out you can use one of these two tools:

  • Firefox Developer tools: Firefox now includes web developer tools with the browser. Bring up the network tool with Ctrl + Shift + q to see the request and response headers.
  • Curl: This is a command line utility and can be used to view HTTP headers. Just run it like so:

    curl -I http://example.com/image.jpg

Regardless of which tool you use to test the configuration you should see expires headers like the ones below:

HTTP/1.1 200 OK
Date: Mon, 07 Jan 2013 14:54:17 GMT
Server: Apache/2.2.15 (CentOS)
Last-Modified: Sat, 31 Dec 2011 15:40:17 GMT
ETag: "209247a-1c49-4b5652a8c3a40"
Accept-Ranges: bytes
Content-Length: 7241
Cache-Control: max-age=2592000
Expires: Wed, 06 Feb 2013 14:54:17 GMT
Content-Type: image/png

As you can see the expires header contains a date in the future while the cache-control header contains the number of seconds to cache the file.

A note about dynamic content

Apache adds expires and cache-control headers based on the content type of the file being served. If you have PHP or some other server side scripting language generating CSS or javascript content dynamically then that too will have expires and cache-control headers added to it. If you don’t want that to happen, code your script to add it’s own expires and cache-control headers. Apache will not override them.

Conclusion

Setting the correct expires and cache-control headers reduces unnecessary requests by browsers to your server. This improves site loading speeds and reduces server load significantly.

Leave a Reply

Your email address will not be published.