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.
- A browser which understands Gzip makes a request for a file from the web server.
- The server receives the request and notices that the browser understands Gzip therefore returns a Gzipped version of the requested file.
- 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
Nginx Gzip configuration
For Nginx users, the following snippet can be added to the configuration file in order to enable Nginx Gzip.
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.
To check if your server or CDN supports Brotli, use our Brotli Test tool.