Optimizing page load times using mod_deflate, mod_expires, and ETag on Apache2

If you are a front-end web developer or host your own websites I highly recommend you take the time to read the following books by Steve Souders of Yahoo:

They are both quick reads and if you are in the industry they should be interesting to you as well

You should also be using the YSlow Firefox extension along with Firebug which enables you to test your sites easily against the precepts outlined in Steve Souders first book.

And now here are the changes I made on my server that improve end user experience by decreasing page load times on sites, I encourage you to try it as well.

GZip compression using mod_deflate

First enable the deflate module, using debian this can be done as follows
a2enmod deflate
Then edit the deflate.conf file located in /etc/apache2/mods-available/deflate.conf

<IfModule mod_deflate.c>
        AddOutputFilterByType DEFLATE text/plain
        AddOutputFilterByType DEFLATE text/html
        AddOutputFilterByType DEFLATE text/xml
        AddOutputFilterByType DEFLATE text/css
        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

        AddOutputFilterByType DEFLATE application/x-httpd-php
        AddOutputFilterByType DEFLATE application/x-httpd-fastphp
        AddOutputFilterByType DEFLATE application/x-httpd-eruby

        DeflateCompressionLevel 9

# Netscape 4.X has some problems
        BrowserMatch ^Mozilla/4 gzip-only-text/html

# Netscape 4.06-4.08 have some more problems
        BrowserMatch ^Mozilla/4\.0[678] no-gzip

# MSIE masquerades as Netscape, but it is fine
        BrowserMatch \bMSIE !no-gzip !gzip-only-text/html

# 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 (add to vhosts)
#        CustomLog logs/deflate_log DEFLATE
</IfModule>

Expires headers using mod_expires

First enable the module
a2enmod expires
Then edit (I had to create this file) the expires.conf file located in /etc/apache2/mods-available/expires.conf

<IfModule mod_expires.c>

#        ExpiresDefault "access plus 2 months"

        ExpiresByType image/x-icon "access plus 1 month"
        ExpiresByType image/png "access plus 1 month"
        ExpiresByType image/jpg "access plus 1 month"
        ExpiresByType image/gif "access plus 1 month"
        ExpiresByType image/jpeg "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/css "access plus 1 hour"
        ExpiresByType text/javascript "access plus 1 hour"

</IfModule>

Configure ETags to make YSlow happy

Add the following to /etc/apache2/apache2.conf

FileETag None

Restart Apache to apply the changes

sudo /etc/init.d/apache2 restart

Test with YSlow and see the difference!

    Post new comment

    The content of this field is kept private and will not be shown publicly.
    • Web page addresses and e-mail addresses turn into links automatically.
    • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
    • Lines and paragraphs break automatically.

    More information about formatting options