How To Configure GZip Compression In Apache Server Linux

Suppose if you don’t have GZip compression enabled or configured on the Apache serves. We can enable and check the gzip compression module in the apache web server and also test whether it’s working or not.

How To Configure GZip Compression In Apache Server Linux:

When a client tries to access the website, it can take lots of time to load properly because it loads all the files like javascript, CSS, images, etc.

Check Out: How To Make EBS Volume To Use On AWS Cloud

In this case, GZip compression can help you to reduce the load time of the website and mod_deflate filters the output that allows from the server to be compressed before sending it to the client over the network.

You can check whether gzip enabled or not using this command.

 curl -I -H 'Accept-Encoding: gzip' https://www.curiousviral.com

Configure GZip Compression

If you see gzip in Content-Encoding means it’s enabled.

Check Out: Learn Useful Tar Command With Example In Linux

We can check the module enabled in Apache configuration using the below command.

 grep mod_deflate /etc/httpd/conf.modules.d/00-base.conf

You need to define these codes in httpd.conf to configure the gzip compression. This mod_deflate is the replacement of the mod_gzip module in apache.

<IfModule mod_deflate.c>
# Compress HTML, JavaScript, CSS, Text, fonts and XML 
 AddOutputFilterByType DEFLATE application/javascript
 AddOutputFilterByType DEFLATE application/rss+xml
 AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
 AddOutputFilterByType DEFLATE application/x-font
 AddOutputFilterByType DEFLATE application/x-font-opentype
 AddOutputFilterByType DEFLATE application/x-font-otf
 AddOutputFilterByType DEFLATE application/x-font-truetype
 AddOutputFilterByType DEFLATE application/x-font-ttf
 AddOutputFilterByType DEFLATE application/x-javascript
 AddOutputFilterByType DEFLATE application/xhtml+xml
 AddOutputFilterByType DEFLATE application/xml
 AddOutputFilterByType DEFLATE font/opentype
 AddOutputFilterByType DEFLATE font/otf
 AddOutputFilterByType DEFLATE font/ttf
 AddOutputFilterByType DEFLATE image/svg+xml
 AddOutputFilterByType DEFLATE image/x-icon
 AddOutputFilterByType DEFLATE text/css
 AddOutputFilterByType DEFLATE text/html
 AddOutputFilterByType DEFLATE text/javascript
 AddOutputFilterByType DEFLATE text/plain
 AddOutputFilterByType DEFLATE text/xml
</IfModule>

If you need to match the old browser, we can use the below code in the above directory.

Check Out: How To Secure Apache Web With Server Hardening In Linux

# Remove browser bugs (only needed for really old browsers)

 BrowserMatch ^Mozilla/4 gzip-only-text/html
 BrowserMatch ^Mozilla/4\.0[678] no-gzip
 BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
 Header append Vary User-Agent

For the older version with mod_gzip, you can use the below code.

<IfModule mod_gzip.c>
 mod_gzip_on Yes
 mod_gzip_dechunk Yes
 mod_gzip_item_include file .(html?|txt|css|js|php|pl)$
 mod_gzip_item_include handler ^cgi-script$
 mod_gzip_item_include mime ^text/.*
 mod_gzip_item_include mime ^application/x-javascript.*
 mod_gzip_item_exclude mime ^image/.*
 mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</IfModule>

If you want to enable it for a particular site, you can define it like the code below.

<Directory /yoursite>
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript
</Directory>

You’re done  enable gzip compression module for   apache check test 

Share on:

I'm the founder of Curious Viral. I hope this blog will provide you complete information about Linux Technology & I would like to share my technical knowledge with you which I have learned during this period.

Other Posts You May Like...

Leave a comment