Speed up Apache 2.0 web access or downloads with mod_deflate

You can speed up downloads or web page access time with Apache mod_deflate module. The mod_deflate module provides the DEFLATE output filter that allows output from your server to be compressed before being sent to the client over the network.

This decreases the amount of time and data transmitted over the network, resulting in faster web experience or downloads for visitors.

Make sure mod_deflate included with your Apache server (by default it is now installed with all modern distro).

How can I speed up downloads from my Apache 2.0 server?

Open httpd.conf file using a text editor such as vi:
# vi httpd.conf

Append following line:
LoadModule deflate_module modules/mod_deflate.so

Append following configuration <Location /> directive:
<Location />
AddOutputFilterByType DEFLATE text/html text/plain text/xml

Above line only compress html and xml files. Here is the configuration from one of my production box:
<Location />
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE image/svg+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/atom_xml
AddOutputFilterByType DEFLATE 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

Close and save the file. Next restart apache web server. All of the above extension file should compressed by mod_deflate:
# /etc/init.d/httpd restart

You can also specify specific directory and enabling compression only for the html files. For example /static/help/ directory:
<Directory "/static/help">
AddOutputFilterByType DEFLATE text/html

In real life, there are issues with compressing other types of files such as mp3 or images. If you don’t want to compress images or mp3 files, add following to your configuration:
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
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

Please note that this processing takes additional CPU and memory on your server as well as on the client browser. So you must make decision which document you need to compress (thanks to mdxp).

See also:

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 34 comments so far... add one
CategoryList of Unix and Linux commands
Disk space analyzersncdu pydf
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
34 comments… add one
  • John May 25, 2006 @ 20:21

    And how the hell I am gonna test a page?

    Use lwp-request command, which is a simple command line user agent.

    lwp-request -e -H ‘Accept-Encoding: gzip’ http://www.yoursite.com/



  • 🐧 nixCraft May 25, 2006 @ 20:25


    Yes, you can test it with lwp-request.

    Appreciate your post.

  • l33t May 26, 2006 @ 20:58

    Good article but please note that:

    a) mod_deflate has replaced Apache 1.3’s mod_gzip in Apache2 i.e. mod_gzip is not at all compatible with Apache 2.0. I had big problem with mod_gzip when upgraded to Apache 2.0 (FreeBSD box)

    b) mod_deflate should work with all modern browsers but it will not work with outdated browser such as Netscape v4.0, just add following rules:
    BrowserMatch ^Mozilla/4 gzip-only-text/html
    BrowserMatch bMSI[E] !no-gzip !gzip-only-text/html

    See the mod_deflate home page for more examples

  • tony May 26, 2006 @ 21:06

    I’m using Apache 2.0.x with mod_deflate and I have no trouble.

    I carried out some experiments with both mod_gzip and mod_deflate. My results were as follows:

    File: test.php (size w/o compression: 53664 bytes)
    Apache 2.0 with mod_deflate: 17709
    Apache 1.3.x with mod_gzip: 22002


  • 🐧 nixCraft May 29, 2006 @ 13:19

    >Is the configuration similar for Apache 2.2.x?


  • mdxp May 29, 2006 @ 13:29

    You might want to add somewhere, that this will obviously add some additional CPU overhead, because all the operation (to compress the files before sending it to the client browser) is done in real time. This might not cause problems with small sites, but on higher traffic sites it can have an impact on the performance of the system.

  • MikeTA May 29, 2006 @ 13:45

    I can really vouch for mod_deflate, it is particularly good on sites where pages are generated dynamically (ie, from CGI scripts) and contain a lot of repeated boilerplate.

    I’d also recommend a look at mod_expires if your pages contain a lot of stock graphics (such as icon-like images) that appear on lots of pages and which never (or infrequently) change.

  • 🐧 nixCraft May 29, 2006 @ 15:18

    MikeTA, good hint, mod_expires seems to be a good choice for all those tiny graphics files. Earlier I had used 512Mb/1GiB ramdisk mounted on /images, to server images from ram to save disk I/O. I will try out mod_expires …

    Mdxp, considering power of today CPU and lots RAM it will not make big difference. But, I will keep an eye on this issue, thanks for heads up.

    Appreciate your post.

  • mangoo May 29, 2006 @ 13:15

    Is the configuration similar for Apache 2.2.x?

  • 🐧 nixCraft May 29, 2006 @ 18:26


    It works with all modern browsers but it may not work with old browser such as Netscape or IE. You can setup filter to ignore unsupported browser.

    Please note that if a client web browser requests and tells server that it supports compression then server sent back compressed document (otherwise server will send normal uncompressed document). So you don’t have to worry about browsers.

    It is *quite* useful if end-user is connected via modem.

    Slashdot and many other sites with tons of visitors use thing technique You can see if particular site is using mod_gzip or mod_deflate http://www.whatsmyip.org/mod_gzip_test/

    And remember this processing takes additional CPU and memory on your server as well as on the client browser. So you must make decision which document you need to compress.

  • Scott May 29, 2006 @ 21:17

    Also, keep in mind that mod_deflate must queue all transfers in memory in order to compress the content prior to delivery to the client. Keep an eye on your server’s memory utilization if you enable this on a busy site.

    All in all, it works very well on the sites I manage which average 12 million hits/month.


  • Razvan May 29, 2006 @ 17:24

    How is this technique working with the major browsers ?

    Internet Explorer

    ? ? ?

    What is happening if a page is still very slow to load ? If the page is just HTML (uncompressed) the browser will start to render it as it loads. At least the used doesn’t have to wait until the whole page is downloaded.

    If this module is used the whole compressed page must be downloaded before anything is displayed ? True / False

    If the above is true, then webmasters should check that the sizes of the compressed files are not bigger than 50k.

    Are there any popular web sites (more than 100,000 unique visitors a month) that are using this technique ? Can you provide some examples ?


  • Hans-Werner Jul 26, 2006 @ 11:10

    @Razvan and Scott: gzip is a stream compressor, it doesn’t need random access to the full data to be (un-)compressed. Such no full buffering is needed, neither server side, nor client side.

  • GoLD Oct 21, 2006 @ 0:14

    Wich value you use for the DeflateCompressionLevel directive ?
    Thanks for all. 🙂

  • Farrukh Shahzad Sep 13, 2007 @ 9:18

    what you are saying I don’t agree to that…

    To use the gzip compression in PHP/Apache the output buffering is required to be set as “ON”

    So whole page (or output buffer gets full) is buffered and compressed and then sent to the client….

    also the funny part is that if a client receives a compress page and that page contains more than one gzip compress block then till all the blocks are downloaded at client browser… the output in the browsers will not be displayed…

    mean page will show up till all the compressed contents are downloaded in the client side

  • Nigel Aves Nov 28, 2007 @ 2:11

    Thanks for this incredibly useful page, integrated into Apache with no problems.

    One technical question. Does anyone know why “image” files can not be compressed? I run an on-line gallery so even a few % of compression could be handy.

    The final should be

    Log Files …

    Found this on the apache site

    DeflateFilterNote Input instream
    DeflateFilterNote Output outstream
    DeflateFilterNote Ratio ratio

    LogFormat ‘”%r” %{outstream}n/%{instream}n (%{ratio}n%%)’ deflate
    CustomLog logs/deflate_log deflate

    solves the question above about “testing”. Seems to work with webalizer as well.

    Many Thanks


  • Nigel Aves Nov 28, 2007 @ 2:13

    ah …. this was dropped from my message 🙂

    the final location should be preceded by a back-slash.

  • Jeff Shields Dec 11, 2007 @ 4:08

    I used Mod_deflate with apache 2.0.54 with .shtml SSI which also have embedded php code. These pages are returned empty to the browser.

    Not sure why this is happening. Anyone know about this.

  • matsu Mar 23, 2008 @ 17:10

    I found spelling mistake.

    The end of code snipet, should be .

    thank you for your article. Very useful for real operating server.

  • Shivin Aug 14, 2008 @ 9:04


    Will mod_jk and mod_deflate works together?
    If yes which versions are good.
    Pls mail me the answer shivinvijai@gmail.com

    Thanks in advance

  • treblam Nov 8, 2008 @ 6:18

    The end tag should be ,if I use I would get this error when I restart apache:”The requested operation has failed”

  • Jiwei Wang Nov 28, 2008 @ 3:20

    Great tip. Works like a charm, speeding up my sites a good percentage. One bug in the post: should read in two places.

  • Jiwei Wang Nov 28, 2008 @ 3:23

    My previous comment was messed up by the html code. The bug is “Location” should read “/Location”, replacing ” with angle brackets.

  • Anton Ongsono Dec 25, 2008 @ 11:02

    how to use this in htaccess?

  • otyek james Jan 29, 2009 @ 10:06

    thanxs alot, we in africa have issues woth bandwidth, I believe this will help optimize our webserver’s performance.
    Good day,

  • Amol Feb 9, 2009 @ 5:57

    Can we ask apache not to compress the static contents every time and use the compressed version which is would have prepared for the first time access

  • Amol Feb 9, 2009 @ 6:03

    We have enabled apache compression(mod_deflate) as per the configuration given
    LoadModule deflate_module modules/mod_deflate.so

    # 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
    ## Don’t compress images
    SetEnvIfNoCase Request_URI
    .(?:gif|jpe?g|png)$ no-gzip dont-vary

    # Make sure proxies don’t deliver the wrong content
    Header append Vary User-Agent env=!dont-vary


    And after this we are facing one strange issue, that if we access the contents from the local intranet, we get the compressed version. However if we access the same contents over the internet(using the same url, our site is static one), we get the un-compressed version, we have identified this by observing the bytes delivered for the request. We also tried enabling/disabling the proxy setting, but no change. Any reason/clue for such kinda situation?


  • David Dec 16, 2009 @ 6:08

    How do I set mod_deflate to work for just ONE domain, not all the domains that my Apache web server works for?

  • Daniel Jan 27, 2010 @ 10:53

    Just add the configurationdata to the vhost container for your particular domain.

  • sim so dep Jan 27, 2011 @ 12:21

    Hey cool weblog, simply questioning what anti-spam software program you utilize for comments because i get heaps on my blog. Anyway, in my language, there are not much good source like this.

  • Fuming Solder May 18, 2011 @ 0:51

    Somehow I still have issues with IE8 corrupting zip files even though zip files are specifically excluded from further gzipping with

    SetEnvIfNoCase Request_URI .(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary

    Does anyone have a clue why this regex mey not be working with Apache/2.2.11 (Unix) ? Or is there another reason Apache keeps gzipping zip files (and IE8 does not recognize that the file has been compressed twice) ?


  • Triactol Aug 11, 2011 @ 7:54

    I am not sure where you’re getting your information, but good topic. I needs to spend some time learning much more or understanding more. Thanks for excellent info I was looking for this info for my mission.

  • Eloy Leja Sep 19, 2011 @ 4:15

    Hi there can I quote some of the material here in this site if I reference you with a link back to your site?

  • Vidyadhar Jan 14, 2012 @ 8:28

    0 down vote favorite
    share [fb] share [tw]

    I want to enable compression in apache. For the same I am trying to use deflate module of apache.

    I have already installed apache 2. But it is a default installation and in that mod_deflate is not enable. Now I am compiling mod_deflate module by using following commands:

    cd /usr/local/src/httpd-2.2.21/modules/filters/
    /usr/local/apache/bin/apxs -i -c -Wl,lz mod_deflate.c

    After doing it I have added following line in my httpd.conf file

    LoadModule deflate_module modules/mod_deflate.so

    but as soon as i do syntax check I am getting following error httpd: Syntax error on line 417 of /usr/local/apache/conf/httpd.conf: Cannot load /usr/local/apache/modules/mod_deflate.so into server: /usr/local/apache/modules/mod_deflate.so: undefined symbol: inflate

    Following is the output of ldd command

    linux-gate.so.1 => (0x00b2d000)
    libc.so.6 => /lib/i386-linux-gnu/libc.so.6 (0x00110000)
    /lib/ld-linux.so.2 (0x008e6000)

    Please help as i dont want to compile apache again.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum