One of the easiest and most effective ways to make a web page load fast is to compress the data sent from the server to the browser. Gzip and Brotli are compression methods that, when enabled on the web server, greatly reduce file sizes. This guide explains how compression works and how to enable it.

Related reading: Virtual host configuration · Connecting to a server with SSH

Why Does Compression Speed Things Up?

Web pages consist largely of text: HTML, CSS and JavaScript files. Text data is highly compressible — because it contains repeating patterns. The server sends the file compressed; the browser decompresses it. The result: data carried over the network drops by 70-80%, and the page loads noticeably faster.

This gain is especially felt on slow connections and mobile devices. Page speed also affects user experience and SEO through Core Web Vitals.

The Difference Between Gzip and Brotli

PropertyGzipBrotli
Release year1992 — a long-standing standard2015 — developed by Google
Compression ratioGood~15-20% better than Gzip
Browser supportUniversalAll modern browsers
Recommended useBackward compatibilityIdeal for static content

Serving both is the best approach: browsers that support Brotli get Brotli, those that do not get Gzip. The browser announces the method it supports with the Accept-Encoding header.

Enabling Compression on Nginx

On Nginx, Gzip is built in; it is turned on by adding a few lines to the configuration file:

gzip on;
gzip_comp_level 5;
gzip_min_length 256;
gzip_types text/plain text/css application/json
           application/javascript text/xml application/xml
           image/svg+xml;

For Brotli, Nginx's ngx_brotli module needs to be installed; then it is enabled with the brotli on; and brotli_types lines.

Which Files Should Be Compressed?

  • Compress: HTML, CSS, JavaScript, JSON, XML, SVG and plain text — these shrink significantly.
  • Do not compress: Already-compressed files like JPEG, PNG, WebP, MP4, ZIP. Recompressing them does not reduce size, it only spends CPU.
  • Very small files: For files smaller than a few hundred bytes the compression gain can be less than the overhead — that is why gzip_min_length is set.
Tip
Setting the compression level (comp_level) too high increases CPU usage while the gain stays limited. A medium level (around 5-6) is ideal for most servers in terms of the speed-CPU balance.

Verifying Compression

You can verify that compression works from the response headers. In the output of curl -I -H "Accept-Encoding: br,gzip" https://yoursite.com you should see a content-encoding: br or gzip line. The browser's developer tools (Network tab) also show this.

Frequently Asked Questions

Does compression slow down the server?

Compression uses a small amount of CPU, but because it greatly reduces the data carried over the network, the net effect is a clear speedup. Caching the compressed version of static files removes the CPU load too.

Should I prefer Gzip or Brotli?

Serve both. Brotli compresses better and all modern browsers support it; Gzip remains a safe fallback for old clients.

If I use a CDN, should I still enable compression?

Many CDNs do compression themselves; still, having it enabled on your origin server too ensures the CDN receives compressed content and keeps caching consistent.

A Fast Web Infrastructure

Go live on a server where compression and caching are optimized from the start with KEYDAL hosting solutions. Explore KEYDAL hosting

WhatsApp