Browser caching is an incredible technique designed to reduce bandwidth utilization and resource consumption while offering a seamless and faster end-user experience to your website visitors. When implemented properly, caching can speed up browsing and deliver a high benefit-cost ratio in websites where visitors revisit the same location.
This tutorial will help you configure your browser caching control on Apache 2 servers. We’ll cover all servers running the mod_headers and Mod_expires Apache modules.
Prerequisites
This tutorial assumes that you are familiar with the following system administration concepts:
- Basic SSH connections
- Navigation in the common Linux command line Shell platform
- Command line piping through Linux I/O redirection
- Managing (open, edit, save) files in a system editor such as Nano, Vim, etc
If you are conversant with these concepts you, are good to go!
Step1- Verifying Modules
Before we start the configuration process, we must ensure that the mod_headers and Mod_expires Modules are properly installed and that Apache2 servers are ready to take the commands. Here, we’ll utilize apachectl -M command to check the existing Apache modules and display the output via grep module_name command. The output will feature filtered results showing only the modules with the provided module_name.
To verify mod_headers run the following command:
apachectl -M | grep header
You will get the following output:
headers_module (shared)
Next, verify the Mod_expires Apache module using the following command:
apachectl -M | grep expires
This will give you an output like this:
expires_module (shared)
Since both outputs showed positive results, it’s a clear that the modules we require for our tutorial are present. However, if the outputs are blank its a sign that the modules are absent. In that case, you will have to install the missing modules before you continue.
Step 2- Configuring The Directives
For the sake of this tutorial, we’ll leverage a generic configuration that prolongs cache duration of basic static files to minimize the stress on server resources. This file will not change between visits and need not be downloaded on each visit. In addition, modern browsers are innovatively equipped to take instructions from servers that offer suggestions regarding the duration of caching content. This example will work well for almost every website, but you may need to adjust the lifespan or add/remove file types to suit your typical content.
<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>
You can visit the Apache Mod_expires Online documents to understand more about these directives.
Step 3- Implementing The Directives
Once you comprehend how to configure the directives, you have to settle for an ideal method of implementation. There two methods of implementing the directives namely Portable and Include methods. This tutorial will focus on the Portable method.
Portable Method
This method relies on the .htaccess file to control directories that will be affected by the configuration settings of Mod_expires. Here, the directories are handled like the other.htaccess file changes
To implement the directives via this method:
- Locate the specific directory that requires browser caching.
- Create a .htaccess file in that directory. If there is one already, proceed with the next instruction.
- Copy the required directives (refer to the configuration directive part above)and paste them in the .htaccess file.
- Save all the changes to the .htaccess file
- That’s all! You have successfully configured you Apache2 servers and controlling your browsing cache should be easy.
Using the Portable method to implement the directive presents a bottleneck caveat linked to the .htaccess file. The caveat is a general Apache issue that is not specific to Mod_expires, but it affects .htaccess files in general. For .htacess files to function properly, Apache scans every directory on the path to the targeted file and along the way it applies every .htaccess file it finds. This may cause I/O bottleneck in your server configuration.
For this reason, its highly recommended you use the Include method when implementing the directives on your Cloud VPS servers.
Conclusion
Using the Portable method is a simple process that only involves a few steps. However, there is another method that allows you to overcome the shortcoming of the portable method. To explore this option, check this article <How to set up Apache 2 server to manage browser caching Using the Include Method>.
Check out these top 3 Best web hosting services
- Want info about best web hosting? Clicking this link can be of great help.