blog-banner

How to Enable Gzip Compression in Apache

  • Apache
  • GZIP

The mod_deflate module provides the DEFLATE output filter that allows the output from your server to be compressed before being sent to the client over the network. mod_deflate is the replacement of mod_gzip which was used with an older version of Apache....

Apache Gzip

By default, a mod_deflate module is enabled in Apache. To make sure that it is enabled, check the following line in the Apache configuration file.

LoadModule deflate_module modules/mod_deflate.so

Configure Gzip Compression

Now edit your apache configuration and add the extensions you want to compress.

nano /etc/httpd/conf/httpd.conf

Add the following lines at end of the file

AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript

Restart Apache service,

## CentOS ##

sudo service httpd restart

## Ubuntu ##

sudo /etc/init.d/apache2 restart

Add the following configuration in Apache Virtual Host to enable gzip compression for your website. You can also add this code in the website's .htaccess file in the site root.


   
	AddType application/x-javascript .js
	AddType text/css .css
   
   
	AddOutputFilterByType DEFLATE text/css application/x-javascript text/x-component text/html text/plain text/xml application/javascript
	
		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 env=!dont-vary

Testing Compression

We have enabled gzip compression, let’s use one of the online tools below to verify gzip is working correctly.