How To Speed up Apache 2 with mod_deflate

How To Speed up Apache 2 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.
So this module decreases the amount of time and data transmitted over the network, resulting in a faster web experience or downloads for visitors. This post explains how to set up Apache 2 with mod_deflate.

You must have Apache 2 installed and configured. If not, see our tutorials to before enabling Apache 2 with mod_deflate:


  1. How To Install Linux, Apache, MySQL, PHP (LAMP) stack On RHEL 8
  2. How To Install Linux, Apache, MySQL, PHP (LAMP) Stack on Debian 9 Stretch
  3. Install LAMP on Debian 10 (Buster)

How to configure Apache mod_deflate

Make sure mod_deflate support comes with your Apache server. By default, this module comes with all modern Linux and Unix distros. Here is how to verify it using the grep command. Open the terminal app and then use ssh and type:
# httpd -t -D DUMP_MODULES | grep -i --color deflate_module
## Debian/Ubuntu Linux try the following
# apache2ctl -t -D DUMP_MODULES | grep -i --color deflate_module

Here is how it looks on CentOS/RHEL 8 or Ubuntu/Debian/ Linux server:

deflate_module (shared)

It would be best to see the output displayed above when deflate_module is installed and enabled on your server. Otherwise, you need to install it using the package manager.

How can I speed up downloads from my Apache 2.0 server

On modern Linux distro such as CentOS/RHEL and co you will find config line in /etc/httpd/conf.modules.d/00-base.conf file. On Debian/Ubuntu Linux you will find such config line in /etc/apache2/mods-enabled/deflate.load. So do not add line if present as it will create problem. Sample config from Debian/Ubuntu:

# Depends: filter
LoadModule deflate_module /usr/lib/apache2/modules/

Outputs from CentOS/RHEL 8.x server:
# grep deflate_module /etc/httpd/conf.modules.d/00-base.conf

LoadModule deflate_module modules/

In case config line missing try it as follows. Edit the httpd.conf file using a text editor such as vim command or nano command:
# cd /etc/httpd/
# vim httpd.conf

Append the following line if not present:
LoadModule deflate_module modules/


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

Config and installation for specific file types

Another option is to update your config file as follows for modern version of Apache 2.4+. Create a new config file such as /etc/httpd/conf.d/deflate.conf and append the following for .js, .html, .php and .css files:

<filesMatch "\.(php|js|html|css)$">
    SetOutputFilter DEFLATE

Close and save the file by pressing Esc and :wq when using vim. Next, restart the Apache web server. All of the above extension files should compressed by mod_deflate:
# systemctl restart httpd #<-- RHEL/CentOS Linux
# systemctl restart apache2.service #<-- Debian/Ubuntu Linux

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.

How to deal with proxy and CDN servers

By default this Apache server module sends a Vary: Accept-Encoding HTTP response header to alert proxies that a cached response should be sent only to clients that send the appropriate Accept-Encoding request header. But, some proxy and CDN servers might cause problems. We can fix this by adding the following line in httpd.conf:

Header append Vary User-Agent
# or following 
Header set Vary *

See mod_headers documentation for details.

How to test Apache Module mod_deflate

We can easily find if a website using gzip / deflate compression using curl on Linux and Unix. The syntax is:

curl -I -H 'Accept-Encoding: gzip,deflate' https://domain-name
curl -s -I -L -H 'Accept-Encoding: gzip,deflate' https://domain-name
curl -s -I -L -H 'Accept-Encoding: gzip,deflate' https://domain-name | grep -i '^content-encoding'

Use the curl command as follows:
$ curl -s -I -L -H 'Accept-Encoding: gzip,deflate'
Look for Content-Encoding:

HTTP/1.1 200 OK
Date: Sat, 05 Jun 2021 13:03:56 GMT
Server: Apache/2.4.46 (Ubuntu)
Last-Modified: Sat, 05 Jun 2021 12:35:28 GMT
ETag: "2aa6-5c4040b13e562-gzip"
Accept-Ranges: bytes
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Length: 3138
Content-Type: text/html

Summing up

You learned how to set up and configure Apache 2 with mod_deflate to enhance web server performance and speed up serving webpages. See also:

🥺 Was this helpful? Please add a comment to show your appreciation or feedback.

nixCrat Tux Pixel Penguin
Hi! 🤠
I'm Vivek Gite, and I write about Linux, macOS, Unix, IT, programming, infosec, and open source. Subscribe to my RSS feed or email newsletter for updates.

35 comments… add one
  • 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/

    # 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/

    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/ into server: /usr/local/apache/modules/ undefined symbol: inflate

    Following is the output of ldd command => (0x00b2d000) => /lib/i386-linux-gnu/ (0x00110000)
    /lib/ (0x008e6000)

    Please help as i dont want to compile apache again.

  • Raju Sethi Jun 5, 2021 @ 13:20

    Thank you for updating and revising this tutorial after a long time, and I appreciate you replying to my email and fixing the issue with my Apache 2. Cheers mate.

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre> for code samples. Your comment will appear only after approval by the site admin.