Enabling HTTP Compression and Browser Caching on Linux

Your website popularity depends a lot on how fast your pages load. You can make use of simple techniques to make your site load faster.

HTTP Compression is one such technique, which is a simple server side configuration. You do not need to make any changes in your website files or code. HTTP Compression takes place between the server and the web browser dynamically, if the browser advertises its capability. All modern browsers support HTTP Compression.

This causes some processing overhead on the server, due to which some of the low cost hosting providers do not provide this feature, as they want to be able to host more websites on the server. If you are stuck with such a service provider, you may want to switch over to another who does allow it.

If you have root access to the server, you are in luck. You can configure HTTP Compression and Browser Caching yourself.

For HTTP compression on Apache, a module named mod_deflate is included in the standard rpm. However, the module is not enabled by default in the Apache configuration file..

To enable the mod_deflate module in Linux, edit the Apache configuration file:

# nano /etc/httpd/conf/httpd.conf
Search the line that says,

#LoadModule deflate_module modules/mod_deflate.so
and uncomment it i.e. remove the ‘#’ mark. The line should then look as below:

LoadModule deflate_module modules/mod_deflate.so
Now, create a /etc/httpd/conf.d/deflate.conf file. Apache reads all the .conf files from the /etc/httpd/conf.d directory on a Linux server.

# nano /etc/httpd/conf.d/deflate.conf

and place the following code in it

SetOutputFilter DEFLATE

DeflateCompressionLevel 9
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/atom_xml
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/x-httpd-php
AddOutputFilterByType DEFLATE application/x-httpd-fastphp
AddOutputFilterByType DEFLATE application/x-httpd-eruby
AddOutputFilterByType DEFLATE image/svg+xml

To Leverage browser caching, you need to similarly enable a module named mod_expires. Once again, open httpd.conf as shown above, and look for the line:

#LoadModule expires_module modules/mod_expires.so

Uncomment the line by removing the “#” mark.

Then create a /etc/httpd/conf.d/expires.conf file like before:
# nano /etc/httpd/conf.d/expires.conf

and place the following code in it:

ExpiresActive On
ExpiresByType image/jpg “access 1 year”
ExpiresByType image/jpeg “access 1 year”
ExpiresByType image/gif “access 1 year”
ExpiresByType image/png “access 1 year”
ExpiresByType text/css “access 1 month”
ExpiresByType application/pdf “access 1 month”
ExpiresByType application/javascript “access 1 month”
ExpiresByType application/x-javascript “access 1 month”
ExpiresByType application/x-shockwave-flash “access 1 month”
ExpiresByType image/x-icon “access 1 year”
ExpiresDefault “access 2 days”


Finally, restart Apache server:
# systemctl restart httpd

You can test if the compression and caching is working as expected using one of the free tools such as https://tools.pingdom.com/ and http://www.whatsmyip.org/http-compression-test/

Note: The configuration directory paths may differ on your system, if you are not using RPM based installation, or using a different distribution of Linux other than Centos / Redhat.