How to Enable GZIP Compression for Faster Loading Step by Step

Enable GZIP Compression
By Tapas Pal Updated

GZIP Compression is a simple and efficient way to speed up any website. The formula of success is simple here

Less bytes = Fast download = Faster website

In this tutorial, we will show you how to enable GZIP compression on Apache and Nginx web servers to speed up any website.

What is GZIP

GZIP is the most popular compression method used on the web today. It is an open source compression algorithm developed by GNU project. GZIP works best on text-based content and provides an average 70 to 90 percent byte savings.

GZIP is supported on all major web browsers used today.

How GZIP communication works

When a browser that supports GZIP compression requests a resource from the server, it sends the Accept-Encoding header. The Accept-Encoding header informs the server of the compression algorithms supported by the browser.

Accept-Encoding: gzip, deflate

When the GZIP enabled server sees the Accept-Encoding header, it will compress the resource before sending it to the client browser. The server sets Content-Encoding header to tell the browser what kind of compression was applied to the resource.

Content-Encoding: gzip

When the browser receives the resource, it scans the header and finds that the resource is compressed. The browser then goes on to decompress the received resource before using it.

What should be GZIPPED

GZIP works best on text-based content. GZIP support should be enabled for anything that includes text content – HTML, CSS, Javascript, JSON, XML, SVG.

How to enable GZIP compression on Apache Web Server

If you are using Apache web server, then add the following lines of code to your .htaccess file to enable GZIP.

<IfModule mod_deflate.c>
    <IfModule mod_mime.c>
        # DEFLATE by extension
        AddOutputFilter DEFLATE js json css htm html xml svg
    </IfModule>
</IfModule>

Note: if you have access to the main configuration file, then you should use it to enable GZIP instead of using the .htaccess file. The .htaccess file should only be used when access to the main configuration file is not available.

How to enable GZIP compression on NGINX Web Server

Edit your Nginx configuration file to enable GZIP support. Your configuration file will look something like this

server {
    gzip on;
    gzip_types
        text/plain
        text/css
        text/js
        text/xml
        text/javascript
        application/javascript
        application/x-javascript
        application/json
        application/xml
        application/xml+rss;
}

The above settings need the Nginx mime.types configuration file to be included before. For example

http {
    include       mime.types

That’s all you need to enable GZIP support on Apche and Nginx web servers.