Implement Efficient Cache Policy on Apache HTTP Server

Implement Efficient Cache Policy on Apache HTTP Server

Shows the configuration to implement efficient cache policy on Apache HTTP Server. The configuration can be done either by updating the virtual host configuration or .htaccess file placed at the project root directory.

March 29, 2019

We can always configure the cache policies to leverage browser caching of websites and applications hosted on Apache HTTP Server. This tutorial provides the options to configure caching using the mod_expires module.

Add MIME Types

We can always add the appropriate MIME type in order to explicitly specify the cache duration. The most common is to add MIME type specific to font file types and static resources. We can also add the MIME types to file formats including SVG as shown below.

# Format - the extension is optional here
AddType <type> <extension>

# Add MIME types specifc to font files
AddType application/vnd.ms-fontobject .eot
AddType application/x-font-ttf .ttf
AddType application/x-font-opentype .otf
AddType application/x-font-woff .woff

# Add MIME type to support svg extension
AddType image/svg+xml .svg

Enable Caching

Enable the caching by adding the following directive.

# Enable caching
ExpiresActive On

Configure default Cache duration

The below-mentioned code shows the configuration to add default duration effective for all the MIME types and in the case when MIME type duration is not explicitly defined.

# Default cache duration to 1 yr
# A31536000 stands for 1 year in the future -> 31536000 = 365 * 24 * 60 * 60

# Below listed are the examples to set 1 yr
ExpiresDefault "access plus 1 year"
ExpiresDefault "access plus 12 months"
ExpiresDefault "access plus 365 days"
ExpiresDefault A31536000

# Default cache duration to 1 month
# A2592000 stands for 30 days in the future -> 2592000 = 30 * 24 * 60 * 60

# Below listed are the examples to set 1 month
ExpiresDefault "access plus 1 month"
ExpiresDefault "access plus 4 weeks"
ExpiresDefault "access plus 30 days"
ExpiresDefault A2592000

# Default cache duration to 1 day or 1 hour
# A86400 stands for 1 day in the future -> 86400 = 24 * 60 * 60
# A3600 stands for 1 hour in the future -> 3600 = 60 * 60

# Below listed are the examples to set 1 day or 1 hour
ExpiresDefault A86400
ExpiresDefault A3600

Cheatsheet

You can also refer to the below-listed cheatsheet having reference conversions from Seconds to Mins, Days, Weeks, and Months.

## Seconds  - Minutes
# 300 - 5 MINS - Example - A300
# 600 - 10 MINS
# 900 - 15 MINS
# 1800 - 30 MINS
# 2700 - 45 MINS

## Seconds - Hours
# 3600 - 1 HR
# 18000 - 5 HRS
# 36000 - 10 HRS
# 54000 - 15 HRS
# 86400 - 24 HRS

## Seconds - Days
# 86400 - 1 DAY
# 432000 - 5 DAYS

## Seconds - Weeks
# 604800 - 1 WEEK
# 1209600 - 2 WEEKS
# 1814400 - 3 WEEKS
# 2419200 - 4 WEEKS

## Seconds - Months
# 2419200 - 1 MONTH
# 12096000 - 5 MONTHS
# 24192000 - 10 MONTHS
# 29030400 - 12 MONTHS

Explicit cache duration

We can explicitly define the cache duration for a specific MIME type as shown below.

# Set 1 year for gif images
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/gif "access plus 12 months"
ExpiresByType image/gif "access plus 30 days"
ExpiresByType image/gif A31536000

# Examples to set 1 month for gif images
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/gif "access plus 4 weeks"
ExpiresByType image/gif "access plus 30 days"
ExpiresByType image/gif A2592000

Another way to explicitly define the cache duration specific to a set of file extensions is as shown below.

# One month duration for selected file types
<FilesMatch "\.(ico|pdf|avi|mov|doc|mp3|wmv|wav)$">
ExpiresDefault A2592000
</FilesMatch>

Complete Solution

The complete solution to implement the above-listed policies is as mentioned below defaulting to 1-year duration.

<ifModule mod_expires.c>
# Add font and image types
AddType application/vnd.ms-fontobject .eot
AddType application/x-font-ttf .ttf
AddType application/x-font-opentype .otf
AddType application/x-font-woff .woff
AddType image/svg+xml .svg

# Enable caching
ExpiresActive On

# Default cache duration to 1 yr
ExpiresDefault A31536000

# Enable caching of individual image file types
ExpiresByType image/gif A31536000
ExpiresByType image/png A31536000
ExpiresByType image/jpg A31536000
ExpiresByType image/jpeg A31536000
ExpiresByType image/x-icon A31536000
ExpiresByType image/icon A31536000
ExpiresByType image/svg+xml A31536000

# Enable caching of text types
ExpiresByType text/plain A31536000
ExpiresByType text/css A31536000
ExpiresByType text/javascript A31536000

# Enable caching of individual application types
ExpiresByType application/x-javascript A31536000
ExpiresByType application/javascript A31536000
ExpiresByType application/x-ico A31536000
ExpiresByType application/ico A31536000
ExpiresByType application/pdf A31536000
ExpiresByType application/vnd.ms-fontobject A31536000
ExpiresByType application/x-font-ttf A31536000
ExpiresByType application/x-font-opentype A31536000
ExpiresByType application/x-font-woff A31536000
</ifModule>

Alternate Solutions

Apart from using the mod_expires module, there are other ways to implement caching using mod_headers module.

# One month duration for selected file types
# max-age -> 30 days ( 60 * 60 * 24 * 30 )
<FilesMatch "\.(ico|pdf|avi|mov|doc|mp3|wmv|wav)$">
Header set Cache-Control "max-age=2592000, public"
</FilesMatch>

Write a Comment
Click the captcha image to get new code.
Discussion Forum by DISQUS