Find answers, guides, and tutorials to supercharge your content delivery.

Enable Gzip Compression - Configuration for Nginx and Apache

Updated on October 4, 2018
Enable Gzip Compression - Configuration for Nginx and Apache

What is Gzip compression?

Gzip compression allows you to make files smaller thus reducing the amount of time required to transfer a resource from the server to a browser. In today's web environment, many browsers and servers both support Gzip compression. Its ability to reduce file sizes by up to 70% provides a great incentive to make use of this compression method. To enable Gzip compression is considered a high-priority recommendation by site speed test tools, as without it you are unnecessarily increasing your webpage's load time.

How does Gzip compression work?

Gzip compression works by searching for duplicate strings within a file and then replaces the second string with a pointer to the previous string. This helps greatly reduce the amount of actual text within a file as there is usually a significant amount of whitespace and duplicate text.

As for server-to-browser communication, Gzip works in the following way.

  1. A browser which understands Gzip makes a request for a file from the web server.
  2. The server receives the request and notices that the browser understands Gzip therefore returns a Gzipped version of the requested file.
  3. The browser receives the file, decompresses it, and is able to read the data.

In the case that the browser does not understand Gzip, the server will simply send an uncompressed version of the file. This inability for old browsers to support Gzip compression is why the vary header plays a vital role in delivering the proper content.

How to enable Gzip compression?

Depending on which web server you are using, the method to enable Gzip will vary. The following describes both an Apache Gzip configuration as well as an Nginx Gzip configuration.

Apache Gzip configuration

The following snippet can be added to your .htaccess to enable Apache Gzip compression for JavaScript, XML, text, fonts, HTML, and CSS. This list of file types can be modified based on your needs.

<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE application/javascript
    AddOutputFilterByType DEFLATE application/rss+xml
    AddOutputFilterByType DEFLATE application/
    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/javascript
    AddOutputFilterByType DEFLATE text/plain
    AddOutputFilterByType DEFLATE text/xml

Nginx Gzip configuration

For Nginx users, the following snippet can be added to the configuration file in order to enable Nginx Gzip.

gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types application/javascript application/rss+xml application/ application/x-font application/x-font-opentype application/x-font-otf application/x-font-truetype application/x-font-ttf application/x-javascript application/xhtml+xml application/xml font/opentype font/otf font/ttf image/svg+xml image/x-icon text/css text/javascript text/plain text/xml;

KeyCDN users also have the ability to enable Gzip compression for cached files delivered from our edge servers to your website visitors. This option is enabled by default for all users and can be found by navigating to the Zone settings within the KeyCDN dashboard.

To check if your site is Gzip compression enabled, there are a variety of tools which can be used. Site speed test tools such a GTmetrix will recommend you to enable Gzip compression if it is not detected.

Which compression algorithm is coming next?

Gzip has been the primary compression method for years, however there is now a new, emerging compression method called Brotli. This compression method is still in the process of acquiring support from various browsers, however shows great promise in the compression results it is able to achieve.

Compression Algorithm Comparison
Source: Google

To check if your server or CDN supports Brotli, use our Brotli Test tool.

Supercharge your content delivery 🚀

Try KeyCDN with a free 14 day trial, no credit card required.

Get started
KeyCDN uses cookies to make its website easier to use. Learn more