5 GZIP Compression
GZIP is a method of compressing files (also a file format) for faster network transfers. Enabling GZIP compression is a standard practice. If you are not using it, your webpages are likely slower than they should be.
However, in practice, GZIP performs best on text-based content, often achieving compression rates of as high as 70-90% for larger files, whereas running GZIP on assets that are already compressed via alternative algorithms (e.g. most image formats) yields little to no improvement.
The three ways to enable compression by adding a bit of code via either a .htaccess file or an Apache/Nginx server configuration file. Let’s start with the .htaccess route.
Enable compression via .htaccess
The .htaccess file controls many important things for your site. If you are not familiar with the .htaccess file, please read my working with .htaccess article to get some know how before changing it. Following is the code that should put in the .htaccess file.
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_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
Save the .htaccess file and upload it to the server.
Compression on NGINX webservers
For NGINX add the following code to your config file
gzip_buffers 16 8k;
# Disable for IE < 6 because there are some known problemsgzip_disable “MSIE [1-6].(?!.*SV1)”;
# Add a vary header for downstream proxies to avoid sending cached gzipped files to IE6gzip_vary on;
Compression on Apache webservers
The .htaccess technique also work on Apache If you see this .htaccess solution is not working then try below commands instead.
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
You can check whether your compression is working or not using this tool: GZIP.