Enable Gzip Compression – Configuration for Nginx and Apache

enable gzip compression

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/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/javascript
  AddOutputFilterByType DEFLATE text/plain
  AddOutputFilterByType DEFLATE text/xml
</IfModule>

Nginx Gzip Configuration

For Nginx users, the following snippet can be added to the .conf 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/vnd.ms-fontobject 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 advanced features section of your KeyCDN zone.

keycdn enable gzip compression

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.com will recommend you to enable gzip compression if it is not detected. Another option is to use a tool such as checkgzipcompression.com. This tool tells you if gzip is enabled as well as how many bytes were saved by compression the data.

check-gzip-compression-tool

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.

Google-Brotli-Compression-StudyCompression Algorithm Comparison Src: gstatic.com

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

2 Comments

  1. Budd Grant

    You can remove “text/html” from the gzip/types as it is on by default. It also sprouts a warning on nginx restart. 😉

Leave a Reply to Cody Click here to cancel reply.