Apache

Easy Steps To Configure Browser Caching (Leverage Browser Caching) In Apache

Caching is the temporary storage of content like image,file,bits of php scripts,html page, css and other web objects.It store data in a cache for temporarily basis.It also help to reduce page load time.The content like html page, image, file and other web objects is stored on HDD (Local Hard Disk) to make faster for the user. Its also help to improve server performance. For Example when user access page first time the browser pull the all file from original server and browser cached all static content If user access those pages second time browser put those static content from cache instead of the original server.

If you want to go in depth of cache read Cache Wikipedia.

In this tutorial I am going to explain how we can configure Apache to control browser caching.

STEP 1: MODULES VERIFICATION

Your Apache must be configured with “mod_expires” and “headers_module” modules to Browser Caching or leverage browser caching.

Follow below command to verify Modules in Apache.

#apachectl -M | grep expires

expires_module (shared)

Than you will need to check “headers_module”.

#apachectl -M | grep headers

headers_module (shared)

STEP 2: DIRECTIVES EXAMPLE

You will need to put the below directives example in .htaccess file in your web root directory.

I will suggest put below directives example in your Apache configurations (httpd.conf) file.

## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access plus 1 day"
ExpiresByType image/jpeg "access plus 1 day"
ExpiresByType image/gif "access plus 1 day"
ExpiresByType image/png "access plus 1 day"
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/pdf "access plus 1 month"
ExpiresByType text/x-javascript "access plus 1 month"
ExpiresByType application/x-shockwave-flash "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 day"
ExpiresByType text/html "access plus 1200 seconds"
ExpiresDefault "access plus 3 days"
</IfModule>
## EXPIRES CACHING ##
Fields Explanation:

*Default expiration has been set to 2 days.
*Images will expire afer 1 day.
*JavaScript and CSS will expire after 1 month
*Pdf will expire after 1 month
*HTML will expire after 20 Minutes (1200 Seconds).

STEP 3: DIRECTIVES CONFIGURATION

You can configure above directives easily in Apache configuration file. Login to your server using ssh as root. Open httpd.conf file in your favourite editor like vim and nano.

#vim /etc/httpd/conf/httpd.conf

Find your web document root and add below directives.Follow my below httpd.conf example file.

<Directory /var/www/chroot/example.com/web/>
Options -Indexes -MultiViews +FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all

## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access plus 1 day"
ExpiresByType image/jpeg "access plus 1 day"
ExpiresByType image/gif "access plus 1 day"
ExpiresByType image/png "access plus 1 day"
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/pdf "access plus 1 month"
ExpiresByType text/x-javascript "access plus 1 month"
ExpiresByType application/x-shockwave-flash "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 day"
ExpiresByType text/html "access plus 1200 seconds"
ExpiresDefault "access plus 3 days"
</IfModule>
## EXPIRES CACHING ##

<Files ~ "^\.ht">
Deny from all
</Files>
</Directory>

Than Restart/Reload Apache server

#service httpd restart
Or
#service httpd reload

I hope this article will be helpful to understand Leverage Browser Caching in Apache. If you have any queries and problem please comment in comment section or you can also ask your question.

Thanks:)

Thank you! for visiting LookLinux.

If you find this tutorial helpful please share with your friends to keep it alive. For more helpful topic browse my website www.looklinux.com. To become an author at LookLinux Submit Article. Stay connected to Facebook.

About the author

mm

Santosh Prasad

Hi! I'm Santosh and I'm here to post some cool article for you. If you have any query and suggestion please comment in comment section.

Leave a Comment