Compressing Web Content with mod_gzip and mod_deflate

Compressing Web content can produce a much faster site for users. Here's how to set it up and measure your success.

Reducing costs is a key consideration for every IT budget. One of the items looked at most closely is the cost of a company's bandwidth. Using content compression on a Web site is one way to reduce both bandwidth needs and cost. With that in mind, this article examines some of the compression modules available for Apache, specifically, mod_gzip for Apache 1.3.x and 2.0.x and mod_deflate for Apache 2.0.x.

Content Compression Basics

Most compression algorithms, when applied to a plain-text file, can reduce its size by 70% or more, depending on the content in the file. When using compression algorithms, the difference between standard and maximum compression levels is small, especially when you consider the extra CPU time necessary to process these extra compression passes. This is quite important when dynamically compressing Web content. Most software content compression techniques use a compression level of 6 (out of 9 levels) to conserve CPU cycles. The file size difference between level 6 and level 9 is usually so small as to be not worth the extra time involved.

Compression in HTTP

For files identified as text/.* MIME types, compression can be applied to the file prior to placing it on the wire. This simultaneously reduces the number of bytes transferred and improves performance. Testing also has shown that Microsoft Office, StarOffice/OpenOffice and PostScipt files can be GZIP-encoded for transport by the compression modules.

Some important MIME types that cannot be GZIP encoded are external JavaScript files, PDF files and image files. The problem with Javascript files mainly is due to bugs in browser software, as these files are really text files and overall performance would benefit by being compressed for transport. PDF and image files already are compressed, and attempting to compress them again simply makes them larger and leads to potential rendering issues with browsers.

Prior to sending a compressed file to a client, it is vital that the server ensures the client receiving the data correctly understands and renders the compressed format. Browsers that understand compressed content send a variation of the following client request headers:

  • Accept-encoding: gzip

  • Accept-encoding: gzip, deflate

Current major browsers include some variation of this message with every request they send. If the server sees the header and chooses to provide compressed content, it should respond with the server response header:

Content-encoding: gzip

This header tells the receiving browser to decompress the content and parse it as it normally would. Alternatively, content may be passed to the appropriate helper application, based on the value of the Content-type header.

The file size benefits of compressing content can be seen easily by looking at a couple of examples, one an HTML file (Table 1) and the other a PostScript file (Table 2). Performance improvements are examined later in this article.

mod_deflate for Apache versions 2.0.44 and earlier comes with the compression ratio set for best speed, not best compression. This configuration can be modified using the tips found at www.webcompression.org/mod_deflate-hack.php. Starting with Apache 2.0.45, a configuration directive is included.

Table 1. /compress/homepage2.html

CompressionSizeCompression %
No compression56,380 bytesn/a
Apache 1.3.x/mod_gzip16,333 bytes29% of original
Apache 2.0.x/mod_deflate19,898 bytes35% of original
Apache 2.0.x/mod_deflate [2]16,337 bytes29% of original

Table 2. /documents/spierzchala-resume.ps

CompressionServerCompression %
No Compression63,451 bytesn/a
Apache 1.3.x/mod_gzip19,758 bytes31% of original
Apache 2.0.x/mod_deflate23,407 bytes37% of original
Apache 2.0.x/mod_deflate [2]19,942 bytes31% of original
______________________

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

I had no idea you could do

David Hops's picture

I had no idea you could do this. Should come in handy as I don't have much bandwidth at the moment and am hosting a few large images and zips. Only concern would be extra CPU load.

Great article, thank you for

Webtiful Search Engine SEO's picture

Great article, thank you for sharing.

mod_deflate

Mikhailov Anatoly's picture

Article about mod_deflate settings like on Amazon EC2 AMI
http://railsgeek.com/2008/12/16/apache2-httpd-improving-performance-mod_...

Nice Work!

Fargham's picture

A great and informative article!

Really helped me!!

Ashutosh Chaturvedi's picture

Hi,

It's really a great artical and really helped me. One question from myside is..

Is there anyway of copressing .tiff files by using mod_gzip??

Plz help me, if anyone has idea about the same.

Thanks in advance..

Ashutosh

Brain Cancers

seda's picture

I read your article.The things you have written sound very sincere and nice topics i am looking forward to its continuation.

Re: Compressing Web Content

Anonymous's picture

stephen,
good article summarizing the methods and benefits of compressing web pages. however, you should touch on the difficulty of using mod_gzip with mod_ssl under apache 1.3.x -- this is a cumbersome issue and there are only workaround solutions. one such is to use a mod_proxy frontend virtual server to buffer the ssl request, and a mod_gzip backend virtual server to handle the compression. more detail on this two stage approach is here:
http://lists.over.net/pipermail/mod_gzip/2002-February/005911.html
i have implemented the above method on a few production servers and it does indeed work, with some caveats.

i believe that i read somewhere that apache 2.x had improved handling of the gzip/ssl pairing. not having played with 2.x i'm not in a position to say whether or not it actually works. perhaps someone could comment on this.

regards,
jim

At what cost to the CPU

Anonymous's picture

Great article! I enjoyed reading it and found it very informative. One question though...

What will this module do to my CPU? Will the load average on my box go through the roof everytime I need to send out a compressed webpage? I think this would have been a nice point to look at as part of your article.

Really helped me!!

Ashutosh Chaturvedi's picture

Hi,

It's really a great artical and really helped me. One question from myside is..

Is there anyway of copressing .tiff files by using mod_gzip??

Plz help me, if anyone has idea about the same.

Thanks in advance..

Ashutosh

compressing .tiff files

Marcus's picture

To compress TIFF files, simply remove the following exlusion for images from the above example configuration:

mod_gzip_item_exclude mime ^image/.*$

Also, add the .tif file extension in the file inclusions:

mod_gzip_item_include file \.(tif)$

Please let me know if you add this because I'd like to test a browser implementation against it. Thanks.

Marcus Adams
yEnc Decoder Proxy

White Paper
Linux Management with Red Hat Satellite: Measuring Business Impact and ROI

Linux has become a key foundation for supporting today's rapidly growing IT environments. Linux is being used to deploy business applications and databases, trading on its reputation as a low-cost operating environment. For many IT organizations, Linux is a mainstay for deploying Web servers and has evolved from handling basic file, print, and utility workloads to running mission-critical applications and databases, physically, virtually, and in the cloud. As Linux grows in importance in terms of value to the business, managing Linux environments to high standards of service quality — availability, security, and performance — becomes an essential requirement for business success.

Learn More

Sponsored by Red Hat

White Paper
Private PaaS for the Agile Enterprise

If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.

Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.

Learn More

Sponsored by ActiveState