Optimize website speed with mod_expire and mod_deflate

PDF

This tutorial explain how to speed up your Apache webserver with mod_deflate and mod_expire.
mod_expire controls the setting of the Expires HTTP header and the max-age directive of the Cache-Control HTTP header in server responses. These HTTP headers are an instruction to the client about the document's validity and persistence. If cached, the document may be fetched from the cache rather than from the source until this time has passed. After that, the cache copy is considered "expired" and invalid, and a new copy must be obtained from the source.

Like mod_gzip, mod_deflate allows you to compress files before they are served to the client, saving your server traffic.
Compressing causes a increased load on the server but the clients connections times decrease a lot, it might decrease the response times with 25% to 50%. The Apache package shipped with CentOS 5 is not compiled with mod_gzip but mod_deflate instead. With mod_deflate you can compress HTML, XML, CSS, text, JavaScript, ect.

mod_deflate

By default mod_deflate is enabled on CentOS 5. To be sure open the file /etc/httpd/conf/httpd.conf and look for the line showing:
LoadModule deflate_module modules/mod_deflate.so

and make sure it is not comment out.

Next create a new file /etc/httpd/conf.d/deflate.conf with the lines:

<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE image/svg+xml
AddOutputFilterByType DEFLATE image/x-icon
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript

DeflateCompressionLevel 9

# Browser specific settings
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
BrowserMatch \bOpera !no-gzip

# Setup custom deflate log
DeflateFilterNote Input instream
DeflateFilterNote Output outstream
DeflateFilterNote Ratio ratio

LogFormat '"%r" %{outstream}n/%{instream}n (%{ratio}n%%)' deflate
# Example of log file
CustomLog logs/deflate_log DEFLATE
</IfModule>

mod_expire

Also by default mod_expire is enabled on CentOS 5. To be sure open the file /etc/httpd/conf/httpd.conf and look for the line showing
LoadModule expire_module modules/mod_expire.so

and make sure it is not comment out.

Now create a new file /etc/httpd/conf.d/expire.conf with the lines:

<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access plus 60 days"
ExpiresByType image/png "access plus 60 days"
ExpiresByType image/gif "access plus 60 days"
ExpiresByType image/jpeg "access plus 60 days"
ExpiresByType text/css "access plus 1 days"
ExpiresByType image/x-icon "access plus 1 month"
ExpiresByType application/pdf "access plus 1 month"
ExpiresByType audio/x-wav "access plus 1 month"
ExpiresByType audio/mpeg "access plus 1 month"
ExpiresByType video/mpeg "access plus 1 month"
ExpiresByType video/mp4 "access plus 1 month"
ExpiresByType video/quicktime "access plus 1 month"
ExpiresByType video/x-ms-wmv "access plus 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType text/javascript "access plus 1 week"
ExpiresByType application/x-javascript "access plus 1 week"
ExpiresByType application/javascript "access plus 1 week"
</IfModule>
Tags:
 

Comments  

 
+13 # Hisham Malik 2013-06-25 11:58
Nice writeup!

Slight typo: the mod_expires module is loaded via making sure the following line is not commented:
LoadModule expires_module modules/mod_expires.so
 

Please login first before adding a comment.

Search






You are here: Home Howtos and FAQs Apache Optimize website speed with mod_expire and mod_deflate