What is gzip compression? Why do we need to enable it?
Gzip is the file format and method of compression and decompression of files using it’s algorithm to reduce the file size. It is used in web server, where web servers send data to http client with gzip compression for faster data transfer and low bandwidth consumption.
Enabling gzip compression is the best practice and it is highly recommended, so pages are likely to load faster in browsers.

How to check gzip compression enabled or not
You can check if gzip compression is enabled or not on a particular website using following methods.
You can use the google PageSeep Insights tool not only to check gzip compression but also to analyze performance of a website.
You can also check if gzip compression is enabled on server or not using command line tool curl on linux. Try out the following,
1 |
curl -I -H 'Accept-Encoding: gzip' http://www.google.co.in |
Replace the URL with what ever the website that you want to check for gzip compression.
Where,
-I means, only make HEAD request to server to get headers
-H specify the accepted encoding gzip using header ‘Accept-Encoding’
If you see header Content-Encoding with gzip in response headers, then compression is enabled on server and it’s working
It works in such a way that, client would specify it’s supported compression and encoding using header Accept-Encoding. Server will use compression if it is enabled on server else will send plain text back to client. Server will notify encoding format and compression to client through header Content-Encoding in response headers.
Here is the curl request on google.co.in to verify gzip compression
1 2 3 4 5 6 7 8 9 10 11 12 13 |
$ curl -I -H 'Accept-Encoding: gzip' http://www.google.co.in HTTP/1.1 200 OK Date: Thu, 04 Aug 2016 16:12:40 GMT Expires: -1 Cache-Control: private, max-age=0 Content-Type: text/html; charset=ISO-8859-1 P3P: CP="This is not a P3P policy! See https://www.google.com/support/accounts/answer/151657?hl=en for more info." Content-Encoding: gzip Server: gws Content-Length: 4915 X-XSS-Protection: 1; mode=block X-Frame-Options: SAMEORIGIN Set-Cookie: NID=83=crFkLu-3gxYf8zmcCt9a7x3nQx2HjZbxugSvdSfft73lKKTw0nSGSL-Tf91NowmbP_eYuu65BpOlV3B2d2phbaGbxkyX3Bx_6GwkY5nmfwne7tS3MWXGNZ3Iz89WhCeS; expires=Fri, 03-Feb-2017 16:12:40 GMT; path=/; domain=.google.co.in; HttpOnly |
We can see that, the header Content-Encoding: gzip in response headers. Means gzip compression is enabled.
Enable gzip compression in apache
The gzip compression can be enabled by directly changing httpd conf file. That is httpd.conf or you can use .htaccess file to target only specific directory or path or site.
Add the following code to /etc/httpd/conf/httpd.conf if apache is installed somewhere add the that specific httpd.conf file
1 2 3 4 5 6 7 8 9 |
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 |
To enable gzip compression via .htaccess, put the following code into .htaccess file which in the desired site directory.
1 2 3 4 5 6 7 8 9 10 |
<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> |
Restart the apache. That’s it, apache will compress the response which is being sent to client. Enabling gzip compression absolutely would boost your site performance.
You can test if gzip compression is enabled or not using curl as mentioned in this article.
The gzip compression is recommended on all types of text files including following files and extensions
.html
.php
.css
.js
.txt
.json
But, gzip compression is not recommended on graphic files or .zip (in the sense files which are compressed already). Because by compressing these kind of files we hardly save few bytes. Thus increase in loading time because of added unaffected compression task.
Why GZIP compression ?
As explained, gzip compression saves the lot of bandwidth by reducing file size. It saves up to 50% to 80% of bandwidth. So, it reduces download and waiting time of browser for resources. If you enable gzip compression, it won’t affect unsupported browsers, where they can fallback to normal(no compression) data download.
Summary
GZIP compression is process of zipping or compressing files on server before they get delivered or transferred via network to browser. Browser will uncompress the data before it uses. As it saves 50% to 80% of bandwidth, if it enabled performance of website will increase considerably.