• Home
  • About Us
  • Contact
  • Privacy Policy

LinuxAdmin.io

  • Home
  • Tutorials
    • System Administration
    • Linux Tutorials
    • Web Hosting
    • WordPress
    • Virtualization
    • Troubleshooting
    • OpenStack

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.

 

 

Oct 9, 2017LinuxAdmin.io
5 1 vote
Article Rating
WebP Image Conversion On A Linux ServerHow To Check And Repair MyISAM Tables In MySQL
You Might Also Like
 
Setup and Optimize Zend OpCache
 
WebP Image Conversion On A Linux Server
Subscribe
Notify of
guest
guest
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Login.plus
Login.plus
5 years ago

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

0
Reply
LinuxAdmin.io
Author
LinuxAdmin.io
5 years ago
Reply to  Login.plus

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.

0
Reply
5 years ago 2 Comments Apache, Linux System Administration Linux Performance Tuning4,327
Recent Posts
  • Laravel Installation Guide For CentOS
  • Openstack Services Explanation And Overview
  • OSSEC Intrusion Detection Installation On Centos 7
  • Configure ProFTPd for SFTP on CentOS
  • How To Check And Repair MyISAM Tables In MySQL
Most Commented
Hot Clone Linux Server with Rsync
Hot Clone A CentOS Server With Rsync
14 Comments
ngx_cache_purge module
Install The ngx_cache_purge Module In Nginx
8 Comments
piwik-nginx
Piwik Analytics on Nginx
8 Comments
Tags
linuxcentosLinux Performance Tuningsysadminkvmnetworkingmemcachedanalyticssystemd
About

We love Linux and are dedicated to creating Linux administration tutorials for System Administrators since 2016.

Most Viewed
Default Gateway
How To Configure A Default Gateway on CentOS
62,976 views
Zend Opcache
Setup and Optimize Zend OpCache
46,714 views
Install ffmpeg
FFMpeg Install On CentOS 7
20,909 views
Archives
Email subscription

Sign up for our newsletter to receive the latest news and event postings.

2018 © LinuxAdmin.io
wpDiscuz