How to use mod_deflate module to compress static content

How to use mod_deflate module to compress static content

ahs_mod_deflate_setup

Here in this article we will try to use mod_deflate module to compress static content served by the Apache HTTP server to the clients.

Test Environment

  • Fedora server 41
  • Apache httpd v2.4.63

What is mod_deflate

mod_deflate is a very useful module that is used widely to compress the static content server by the httpd server to the client. It basically applies the compression algorithm on the static files such as html, css or javascript before being sent over the network to client. This greatly helps in reducing the network bandwidth usage and improving on the performance of the site. For more details refer mod_deflate documentation.

Though mod_deflate helps in compression it comes at a cost of increased cpu and memory resources on the server. This module provides us with diretive to adjust the compression level which should be fine tuned as per your requirements.

NOTE: The gzip encoding is the only one supported to ensure complete compatibility with old browser implementations. The deflate encoding is not supported.

Procedure

Step1: Ensure Apache HTTP server installed and running

As a first step we need to ensure that httpd service is installed and running on the server as shown below.

admin@linuxser:~$ rpm -qa | grep httpd-2.4.*
httpd-2.4.63-1.fc41.x86_64
admin@linuxser:~$ sudo systemctl status httpd.service 
● httpd.service - The Apache HTTP Server
     Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; preset: disabled)
    Drop-In: /usr/lib/systemd/system/service.d
             └─10-timeout-abort.conf, 50-keep-warm.conf
     Active: active (running) since Thu 2025-04-24 10:05:35 IST; 1h 51min left

Step2: Ensure mod_deflate module is loaded

The default httpd package installation on Fedora 41 OS loads the “mod_deflate” module from the “00-base.conf” module configuration file as shown below.

admin@linuxser:~$ cat /etc/httpd/conf.modules.d/00-base.conf | grep mod_deflate
LoadModule deflate_module modules/mod_deflate.so

We can also validate it by listing all the dynamically loaded modules by running below command. As we can see the module is loaded.

admin@linuxser:~$ httpd -M | grep "deflate"
 deflate_module (shared)

Step3: Create and Update configuration file

Let us create a new file named “deflate.conf” in the “/etc/httpd/conf.d/” which is by default including in the default “httpd.conf” configuration file.

This configuration basically suggests httpd service to apply the “DEFLATE” compression outputfilter for any .html, .css or .js file that are getting served by it to the client.

admin@linuxser:~$ cat /etc/httpd/conf.d/deflate.conf 
<filesMatch "\.(js|html|css)$">
    SetOutputFilter DEFLATE
</filesMatch>

Step4: Upload a static content

Here we are going to download a JQuery library file to our “/var/www/html/” DocumentRoot which is the default location from where the content is served for the default httpd configuration.

admin@linuxser:~$ sudo wget http://code.jquery.com/jquery-1.11.3.js -O /var/www/html/jquery-1.11.3.js
admin@linuxser:~$ ls -ltr /var/www/html/jquery-1.11.3.js 
-rw-r--r--. 1 root root 284394 Oct 18  1991 /var/www/html/jquery-1.11.3.js

Step5: Restart httpd service

Once the configuration is updated we need to restart the httpd service as shown below.

admin@linuxser:~$ sudo systemctl restart httpd.service

Step6: Validate compression results

Now from any remote workstation let’s try to retrieve our jquery library as shown below.

First we will try using wget without any option. In this case the mod_deflate compression is not applied to the javascript file as the client in this case “wget” has not mentioned if it accepts gzip encoded files. The result of the downloaded file size is “284394 bytes” same as the original file on the server.

admin@fedser:tmp$ wget http://linuxser.stack.com/jquery-1.11.3.js
admin@fedser:tmp$ ls -ltr jquery-1.11.3.js 
-rw-r--r--. 1 admin admin 284394 Oct 18  1991 jquery-1.11.3.js

Now let’s add an header to mention that the “wget” client supports gzip compressed files and try to retrieve the jquery library. This time gzip compression will be applied to the files and downloaded with a size “84792”. We are saving the output to a file using “-O” option here.

dmin@fedser:tmp$ admin@fedser:tmp$ wget --header="Accept-Encoding: gzip" http://linuxser.stack.com/jquery-1.11.3.js -O jquery-1.11.3.js.gz
admin@fedser:tmp$ ls -ltr jquery-1.11.3.js.gz 
-rw-r--r--. 1 admin admin 84792 Oct 18  1991 jquery-1.11.3.js.gz

Basically this file is now compression in binary format and cannot be read directly.

admin@fedser:tmp$ file jquery-1.11.3.js.gz 
jquery-1.11.3.js.gz: gzip compressed data, from Unix, original size modulo 2^32 284394

Now let’s try to decompress the file to get the original content as shown below.

admin@fedser:tmp$ gunzip jquery-1.11.3.js.gz 
admin@fedser:tmp$ ls -ltr jquery-1.11.3.js 
-rw-r--r--. 1 admin admin 284394 Oct 18  1991 jquery-1.11.3.js

If you try to access the same jquery library using your browser, the file is sent in .gz compressed format over the network and the browser renders the file by decompressing it as by default the browser sends headers in the request stating it accepts the gzip and deflate compressed files and its able to decode it as “text/javascript” file as shown below. Also you can refer the request and response headers that are set for this query.

Hope you enjoyed reading this article. Thank you..