X

Mod_Expires Configuration In Apache

How to setup mod_expires in Apache

mod_expires is a module which runs on the Apache web server. It allows manipulation of the cache control headers to leverage browser caching. What this means more specifically is that you will be able to set values on how long the image will be stored by the browser when a client makes a request.  This will greatly improve page load times on subsequent requests by that same client. If a asset it set to not change very often, then using longer cache times are better, if the asset changes frequently you would want to use a shorter cache time so that returning visitors will see the updated asset. You can read more about the granular configuration on Apache’s site.

This guide assumes you already have a working version of Apache web server. If you do not, please see How To Install Apache.

 Verify mod_expires is loaded

First you will want to check to see if mod_expires is loaded by performing the following

$ httpd -M 2>&1|grep expires
 expires_module (static)

If it returns nothing, you will need to verify mod_expires is loaded the the config:

cat httpd.conf|grep expires
#LoadModule expires_module modules/mod_expires.so

If it is commented out, you will want to uncomment it and restart Apache.

Configure mod_expires Rules

You will now want to set rules for your site. The configuration can either be placed in the .htaccess or directly in the Apache vhost stanza. The expiration time you will want to set largely depends on how long you plan on keeping the asset as it is. The below ruleset is fairly conservative, if you do not plan on updating those media types at all, you can set them for even a year before expiration.

In this example we will just set the mod_expires values in the .htaccess file in the document root

nano .htaccess

Add the following, adjust any for longer or shorter times depending on your needs:

<IfModule mod_expires.c>
# Turn on the module.
ExpiresActive on
# Set the default expiry times.
ExpiresDefault "access plus 2 days"
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType text/css "access plus 1 month"
ExpiresByType text/javascript "access plus 1 month"
ExpiresByType application/javascript "access plus 1 month"
ExpiresByType application/x-shockwave-flash "access plus 1 month"
ExpiresByType text/css "now plus 1 month"
ExpiresByType image/ico "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 month"
ExpiresByType text/html "access plus 600 seconds"
</IfModule>

Once you have set those values, further subsequent requests should now start setting expires headers. If you set the expires values directly in the Apache v-host stanza, you will want to restart Apache.

Testing mod_expires to ensure its working correctly

There are a couple different ways, you can use the developer tools in a browser to verify the expires value is being set correctly.  You can also test this functionality with curl. You will want to curl the URL of the file you are checking

$ curl -Is https://linuxadmin.io/wp-content/uploads/2017/04/linuxadmin_io_logo.png
HTTP/1.1 200 OK
Date: Mon, 09 Oct 2017 23:10:29 GMT
Content-Type: image/png
Content-Length: 6983
Connection: keep-alive
Set-Cookie: __cfduid=d7768a9a20888ada8e0cee831245051cc1507590629; expires=Tue, 09-Oct-18 23:10:29 GMT; path=/; domain=.linuxadmin.io; HttpOnly
Last-Modified: Fri, 28 Apr 2017 02:21:20 GMT
ETag: "5902a720-1b47"
Expires: Sun, 30 Sep 2018 21:49:53 GMT
Cache-Control: max-age=31536000
CF-Cache-Status: HIT
Accept-Ranges: bytes
Server: cloudflare-nginx
CF-RAY: 3ab5037b7ac291dc-EWR

The line you are checking for is what starts with Expires:

Expires: Sun, 30 Sep 2018 21:49:53 GMT

this should return a time based on the mod_expires value you set.  That is it for setting mod_expires headers in Apache to leverage browser caching.

 

 

LinuxAdmin.io
5 1 vote
Article Rating
LinuxAdmin.io:

View Comments (2)

  • Hi Admin,

    Is there any information for Debian Apache?

    $ httpd -M 2>&1|grep expires
    showed nothing

    cat httpd.conf|grep expires
    not found; no such file or directory

    $ curl -Is https://login.plus/
    is not showing expires headers

    • Hello,
      Debian has a different file location, if you do a httpd -V (or apachectl -V) does it return the SERVER_CONFIG_FILE variable, this will contain the path to the config file which you will need to load the module in.

Related Post