How to configure Apache 2 to Control Browser Caching?

How to configure Apache 2 to Control Browser Caching?

Cache is a component to store data. A cache hit occurs when the requested data is seen in the cache and miss occurs when it is not found. Caching allows to reduce server resource consumption and utilize the bandwidth and provide fast end-user experience to visitors.

Step 1: Verify Modules:

Apache must be configured with the suitable modules to influence browser caching. Servers contain mod_expires and mod_headers modules for controlling browser cache. Before configuring the directives, the modules should be installed properly and Apache2 has to be ready to accept the directives. Apachectl –M command is used to list the Apache modules that are installed.

mod_expires are verified by following command :

Code:
apachectl –M | grep expires
and it will return :

Code:
expires _module (shared)
mod_headers are verified by following command :

Code:
apachectl –M | grep headers
and it will return :

Code:
headers_module (shared)
While running the command, these modules must be there in the output. It is not available in the output or it is blank, it shows that modules are not installed.

Step 2: Configuring Directives:

The codes can be placed in .htaccess files for particular directory or in the root directory but it is always suggested to place in httpd.conf directory on server.

  • <IfModule mod_expires.c> ... </IfModule> - Opening and closing tags and the directives between these tags are processed only if mod_expires module is installed in the server.

  • ExpiresDefault "access plus 2 days" - The default expiration is set to 2 days. The files are downloaded only if the cached file doesn’t accessed in 2 days.

  • ExpiresByType image/jpg "access plus 1 month" - Images expire after 1 month. The files are downloaded only if the cached filed doesn’t accessed in 1 month. The cached file includes jpg, gif, jpeg, javascript, png, css, ico, flash, and x-icon files.

  • ExpiresByType text/css "access plus 1 month" and ExpiresByType text/javascript "access plus 1 month" - CSS and JavaScript file cache expires in 1 month.

  • ExpiresByType text/html "access plus 600 seconds" - HTML expires after 600 seconds. Files are downloaded if the cache doesn’t accessed in 10 minutes (600 seconds).

Code:
<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>
Step 3: Implementing Directives:

To configure the directives the type of implementation has to be selected. The two types of implementation for directives are Portable and Include methods.

Portable Methods:

This method uses .htaccess file for managing directories that are affected by mod_expires configuration. These are handled by
  1. SSH/FTP to the server as root
  2. Locate the required directory in which browser caching should be enabled.
  3. Modify .htaccess file in the directory or create a new file.
  4. Add the directives that are needed from Configuration Directives.
  5. Save the changes.
  6. Done
Include Method:

This method take the advantages of Apache include system. The include methods can be used Globally or Per Website. Both the methods involve in locating and modifying the right include files on the server. The modifying the include file depends on the distribution and server management software. Expiration directives must be added between <Directory “/var/www/html”> and </Directory>. Then Apache is restarted using systemctl restart httpd.

1. First you have the create a file with name “expires.conf” in this location “/etc/httpd/conf.d/” by using below command:
Code:
vim /etc/httpd/conf.d/expire.conf
2. Now add the required directives to the file and save changes. Your file should be look like this:
Code:
<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/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>
3. After that reload Apache with below command:

Code:
Service httpd reload
Author
Bhawani Singh
First release
Last update
Rating
0.00 star(s) 0 ratings

More resources from Bhawani Singh

Top