Optimizing your web server: Part 1 – Gzip

November 22nd, 2009 by Crazy Leave a reply »

This will be the first blogpost in a compilation of posts about optimizing your webserver.

Everyone likes their website to go fast right?, So did I.

Something that can give you an easy performance boost is gzip compression.

This compresses the data that is being send to the user with mod_deflate(apache).
Because of the way xml and html is structured, it contains allot of repeating data.
So this means it’s the perfect candidate to compress.

By enabling compression your webpages can become up to 80% smaller.
A downside is that it does use a little cpu and memory to do the compression, this scares allot of people, thinking that the machine isn’t powerfull enough to do the gzip compression.
But, this uses almost nothing if configured correctly. Make sure you only compress files that benefit from it, for example xml, html, css, js, … and leave out those that do not, jpg, gif, png, mpg, mov, flv, zip, rar, …

Images and movies are already compressed, so compressing them a second time would be pointless.

An extra advantage that gzip gives is that requests are processed faster(files are smaller), because of this, extra cpu time and memory becomes available faster than before. So the cpu time required to do the compression of the files is won back.

A practical example of http://crazytje.be:

Original Size: 42.91 KB
Gzipped Size: 8.08 KB
Data Savings: 81.17%

So how do we go about configuring gzip?
Normally mod_deflate is already supplied with your apache installation, it only requires you to enable it:

LoadModule deflate_module modules/mod_deflate.so
<Location />
# Insert filter
SetOutputFilter DEFLATE
 
# 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

# NOTE: Due to a bug in mod_setenvif up to Apache 2.0.48
# the above regex won't work. You can use the following
# workaround to get the desired effect:
BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html
 
    # Don't compress
    SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary
    SetEnvIfNoCase Request_URI dl.php$ no-gzip dont-vary
    SetEnvIfNoCase Request_URI FileDownload$ no-gzip dont-vary
 
    SetEnvIfNoCase Request_URI \.(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary
    SetEnvIfNoCase Request_URI \.pdf$ no-gzip dont-vary
    SetEnvIfNoCase Request_URI \.avi$ no-gzip dont-vary
    SetEnvIfNoCase Request_URI \.mov$ no-gzip dont-vary
    SetEnvIfNoCase Request_URI \.mp3$ no-gzip dont-vary
    SetEnvIfNoCase Request_URI \.mp4$ no-gzip dont-vary
    SetEnvIfNoCase Request_URI \.rm$ no-gzip dont-vary
    SetEnvIfNoCase Request_URI \.plist$ no-gzip dont-vary
 
 
    AddOutputFilterByType DEFLATE text/plain text/xml application/xml application/xhtml+xml text/javascript text/css application/x-javascript
    AddOutputFilterByType DEFLATE application/x-httpd-php
    AddOutputFilterByType DEFLATE application/x-httpd-fastphp
    AddOutputFilterByType DEFLATE application/x-httpd-eruby
    AddOutputFilterByType DEFLATE text/html
 
    SetEnvIf User-Agent ".*MSIE.*" nokeepalive ssl-unclean-shutdown downgrade-1.0 force-response-1.0
 
    # Make sure proxies don't deliver the wrong content
    Header append Vary User-Agent env=!dont-vary
</Location>

I don’t think it requires any explination, depending on the content being served you might want to add some extensions to compress or not to compress.

Optimizing your web server: Part 1 – Gzip
Optimizing your web server: Part 2 – Keep Alives
Optimizing your web server: Part 3 – Opcode Caching
Optimizing your web server: Part 4a – PHP
Optimizing your web server: Part 4b – XDebug Profiler

Advertisement

Leave a Reply

You must be logged in to post a comment.