Sunday, September 21, 2008

Image Caching in Apache Webserver

Using Apache with the mod_expire module, you can define the Webserver to allow the client to “cache” images, including java script and CSS files.
This greatly improves the load-time of the website.
To enable edit the httpd.conf and define:
enable mod_expire module

    LoadModule expires_module modules/mod_expires.so

there are different way of configuring caching in apache.

Caching Files by Extension:

ExpiresActive On
   <Directory "/home/website/public_html">
Options FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
Allow from all
ExpiresDefault A300
<FilesMatch "\.html$">
Expires A86400
</FilesMatch>
<FilesMatch "\.(gif|jpg|png|js|css)$">
Expires A2592000
</FilesMatch>
</Directory>

Caching Files by MIME Type:

ExpiresActive On
ExpiresByType text/java script “access plus 1 day”
ExpiresByType image/gif A2592000
ExpiresByType image/jpg A2592000

Now restart apache. ExpiresDefault A300 sets the default expiry time to 300 seconds after access (A). Using M300 would set the expiry time to 300 seconds after file modification. Above setting will cache gif,jpg, png images for 259200 seconds(30 days) after access(A signifies access).

No comments: