Enable gzip compression Apache Nginx for faster web speed

Gzip compression helps to reduce the size of transmitted data on Apache Nginx server

There are 2 ways to Enable gzip compression on Apache, with httpd configuration and htaccess

Enable gzip compression n Apache with httpd configuration

By default mod_deflate.so module has been active but we want to make sure and check if mod_deflate.so module has been installed and activate at httpd configuration.

#cat /etc/httpd/conf/httpd.conf | grep deflate
LoadModule deflate_module modules/mod_deflate.so

** If it is commented (#), please remove it (#) out.

Next part is add code below to httpd.conf, these lines can store at the end of the file:

#Enable gzip compression
<ifModule mod_headers.c>
SetOutputFilter DEFLATE
# You can't compress what is already compressed
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

#Make proxies work as they should.
<ifModule mod_headers.c>
Header append Vary User-Agent
</ifModule>
</ifModule>

Restart Apache to effect the changes

#service httpd restart

Enable gzip compression on Apache with .htaccess by add configuration line below to .htaccess

#Enable gzip compression
<ifModule mod_headers.c>
# Compress HTML, CSS, JavaScript, Text, XML and fonts
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

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

Enable gzip compression on Nginx by activate gzip on and add some configuration to etc/nginx/nginx.conf

#vi /etc/nginx/nginx.conf

Add this line below to etc/nginx/nginx.conf

#Enable gzip compression
gzip on;
gzip_comp_level 2;
gzip_min_length 1000;
gzip_proxied expired no-cache no-store private auth;
gzip_types text/plain application/x-javascript text/xml text/css application/xml;

 

Leave a Reply

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