How to enable gzip compression in Apache

We check whether the module is connected in the /etc/httpd/conf/httpd.conf file for this, open the file and look for the line

[[email protected] ~]# cat /etc/httpd/conf/httpd.conf | grep mod_deflate.so
LoadModule deflate_module modules/mod_deflate.so

Add the following lines to the configuration

[[email protected] ~]# nano /etc/httpd/conf/httpd.conf

# mod_deflate configuration
<IfModule mod_deflate.c>

# Restrict compression to these MIME types
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE text/css

# Level of compression (Highest 9 - Lowest 1)
DeflateCompressionLevel 9

# 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 bMSI[E] !no-gzip !gzip-only-text/html

</IfModule>

Restart Apache

[[email protected] ~]# service httpd restart

The mod_deflate module allows you to save up to 70% of traffic on pages with HTML content. Depending on the amount of graphics and other incompressible elements on your sites, the savings can be around 10% of the total traffic.

Sidebar