CA BD NY
  • Categories

  • Recent Posts

  • RSS MySQL Hacker

  • RSS CentOS Hacker

  • RSS Editor's Lists

    • An error has occurred; the feed is probably down. Try again later.
  • Meta

  • Speeding Up Your Web Site Using mod_deflate Module for Apache

    Published November 28th, 2008

    Problem Statement

    If your Web site serve a lot of textual data vs. image or video, you might be able to take advantage of mod_deflate and speed up your Web site without changing any PHP code or MySQL database schema. OK, sure it works for non-PHP/MySQL sites too. But hey, we are biased as we do PHP/MySQL for a living.
    In this article, we will show you how to use mod_deflate to compress your textual contents to speed up delivery time, which results in bandwidth savings in addition to higher user sanctification.

    Step 1: Did you install mod_deflate

    Make sure that you have mod_deflate already installed as part of your Apache Web server. To check you can run /path/to/bin/httpd -l | grep mod_deflate and if you get mod_deflate.c back as a return string, you are in good shape. Otherwise, you will have to install the mod_deflate module for Apache.

    Step 2: Creating a deflate.conf configuration file

    Copy the following Apache directives in a file called deflate.conf and put it in your Apache conf directory.

    <IfModule deflate_module>
    
    <Location />
       SetOutputFilter DEFLATE
       SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ 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
    
       BrowserMatch ^Mozilla/4 gzip-only-text/html
       BrowserMatch ^Mozilla/4\.0[678] no-gzip
       BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
    </Location>
    
    # Testing purposes (remove from production environment)
    DeflateFilterNote Input inbytes
    DeflateFilterNote Output outbytes
    DeflateFilterNote Ratio percent
    LogFormat '%h "%r" %{outbytes}n/%{inbytes}n (%{percent}n%%)' deflate
    CustomLog logs/deflate.log deflate env=!no-gzip
    </IfModule>
    

    Here we have a Apache configuration specific to mod_deflate that only works if mod_deflate is already loaded by Apache. This configuration snippet tells Apache the following:

    • Set the output filter for all files to be DEFLATE — which is the mod_deflate code that compresses contents passed on to the filter code
    • Sets environment variable no-gzip for common image files such as .gif, .jpg, .jpeg, .png
    • Sets environment variable no-gzip for common binary/compressed files such as .exe, .gz, .zip, .bz2, .sit and .rar
    • Sets environment variable no-gzip for PDF file with .pdf extension
    • Ignores older browsers that do nto support on-the-fly decompression on their end
    • For testing purposes, we set a note called inbytes which stores the input (content) size in byte
    • The outbytes note stores the output (contents) bytes and the percent note stores the compression ratio (output / input) * 100
    • A custom log format allows us to create a custom log that records all compressed (!no-gzip) requests

    Step 3: Testing your setup

    Now using your favorite Web browser, request compressible pages of your site and monitor the deflate.log in the logs directory of your Apache installation. The deflate.log should show how good the compression ratio. The better the ratio, the faster you are able to send out compressed contents to the Web clients.

    Why is compressed HTML pages better?

    In the Web’s request and response model, the bottlenecks are in the following order:

    1. Network latency/bandwidth/response
    2. Disk I/O
    3. Other System Calls

    By compressing the HTML contents, which adds to CPU processing time on the server side but no additional disk I/O and also reduces network response time as the size of the content is less than the original. It adds decompression processing on client side. So the additional cost of compression/decompression is only CPU processing on server and client side. Since modern CPU is hardly the bottleneck, the benefit is: network response time reduction.

    See Also
    Apache 2.0 mod_deflate documentation

    Get a Trackback link

    1 Trackbacks/Pingbacks

    1. Pingback: » Speeding Up Your Web Site Using mod_deflate Module for Apache … on November 28, 2008

    1 Comments

    1. liz on December 4, 2008

      Please check, ‘Not’ is misspelled.

      Ignores older browsers that do nto(not) support on-the-fly decompression on their end.

      Thanks.

    Leave a comment

    Comment Policy: First time comments are moderated. Please be patient.

    You must be logged in to post a comment.