All posts

Blog

Gzip vs Brotli Compression: Which Should Your Website Use?

Illustrated cover for the Speed & Performance guide "Gzip vs Brotli Compression: Which Should Your Website Use?"

Every time someone visits your site, your server sends them text: HTML, CSS, JavaScript, JSON, SVG icons. Text compresses extremely well, often to a fraction of its original size. Compressing it before sending means fewer bytes on the wire and faster page loads, especially on mobile connections.

The two compression methods you will run into are Gzip and Brotli. This guide explains the difference, which one to use, and how to check that your site is actually compressing its files.

How HTTP compression works

Compression is negotiated between the browser and the server:

  1. The browser sends a request header listing what it can handle, for example Accept-Encoding: gzip, deflate, br.
  2. The server picks a method it supports, compresses the response, and labels it with a header such as Content-Encoding: br or Content-Encoding: gzip.
  3. The browser decompresses it before rendering.

This all happens automatically. Visitors never see it, and older browsers that don't support a method simply get a version they can read.

What is Gzip?

Gzip is based on the DEFLATE algorithm and has been the standard for web compression for decades. Practically every browser, server, CDN and proxy supports it.

Strengths: - Universal support - Fast to compress and decompress - Enabled by default on many servers

Weaknesses: - Produces larger files than Brotli for typical web content

What is Brotli?

Brotli is a newer compression format developed at Google and standardized as RFC 7932. It combines modern compression techniques with a built-in dictionary of common words and snippets found in web content, such as HTML tags and common JavaScript keywords. That dictionary gives it an edge on the kind of text websites send.

Strengths: - Typically produces smaller files than Gzip for HTML, CSS and JavaScript - Supported by all current major browsers - Decompression is fast

Weaknesses: - The highest compression levels are slow, which matters when compressing on the fly - Browsers generally only use it over HTTPS - Not every server or older software stack supports it out of the box

Gzip vs Brotli side by side

Gzip Brotli
Typical file size Good Usually smaller
Browser support Universal All modern browsers
Requires HTTPS No In practice, yes
Compression speed Fast Fast at low/mid levels, slow at max
Decompression speed Fast Fast
Server support Everywhere Most modern servers and CDNs

The exact savings depend on your files, so treat any specific percentage you read online as a rough guide. The general pattern is consistent, though: Brotli usually wins on size.

Compression levels: dynamic vs static

Both algorithms have levels. Gzip ranges from 1 to 9 and Brotli from 0 to 11. Higher levels squeeze harder but take more CPU time.

How you should pick depends on when compression happens:

  • Dynamic compression happens for each request, for example HTML pages generated by WordPress. Here speed matters, so moderate levels are best. Many servers use something around Gzip level 5 or 6, or a mid-range Brotli level.
  • Static (pre-compressed) files are compressed once, ahead of time, and stored as .gz or .br alongside the original. Because the work is done once, you can use the highest levels. Build tools and some servers can do this for CSS and JavaScript bundles.

So which should you use?

Use both. Serve Brotli to browsers that ask for it and fall back to Gzip for everything else. Since the browser announces what it supports, there is no downside to having both enabled. If you can only have one, Gzip is the safe universal choice and still gives large savings over sending uncompressed text.

What to compress (and what not to)

Compress: - HTML - CSS - JavaScript - JSON and XML - SVG images - Fonts in older formats like TTF (WOFF2 is already compressed)

Don't bother compressing: - JPEG, PNG, WebP and AVIF images - Video and audio - ZIP files and WOFF2 fonts

Those formats are already compressed. Running Gzip or Brotli over them wastes CPU and can make files slightly bigger. For images, the right move is optimizing them properly instead.

How to check whether your site uses compression

Browser developer tools: 1. Open your site, press F12, and go to the Network tab. 2. Reload the page and click the main HTML document. 3. Under Response Headers, look for content-encoding: br or content-encoding: gzip.

Command line:

curl -sI -H "Accept-Encoding: br, gzip" https://example.com | grep -i content-encoding

Speed testing tools: PageSpeed Insights and Lighthouse flag "Enable text compression" if they find uncompressed text files. See our guide on how to test website speed for more tools.

If you see no content-encoding header on your HTML, CSS or JS, compression isn't working.

How to enable compression

On shared hosting

Most hosts enable compression at the server level. On LiteSpeed servers, Gzip and Brotli are typically handled by the server. In cPanel, the "Optimize Website" tool lets you turn on compression for your site. If it's still missing, ask your host.

On Apache

Gzip is handled by mod_deflate, which you can enable in .htaccess:

<IfModule mod_deflate.c>
  AddOutputFilterByType DEFLATE text/html text/css application/javascript application/json image/svg+xml
</IfModule>

Brotli requires mod_brotli (Apache 2.4.26 and later) with a similar AddOutputFilterByType BROTLI_COMPRESS directive.

On Nginx

Gzip is built in with gzip on; plus gzip_types. Brotli needs the separate ngx_brotli module, which many distributions offer as an add-on package.

Through a CDN

CDNs such as Cloudflare compress responses at the edge, often with Brotli, even if your origin server only does Gzip. That is one of the easiest ways to get Brotli without touching server configuration.

A word on double compression

If your application (say, a PHP framework) compresses output and your server compresses it again, you can get broken pages or wasted CPU. Pick one layer to handle compression, usually the web server or CDN, and turn it off elsewhere.

Wrapping up

Text compression is one of the easiest and most reliable speed wins on the web. Gzip works everywhere; Brotli usually shrinks files further. Enable both, compress text but not images or video, and verify with a quick check in your browser's Network tab. It takes minutes and helps every visitor on every page.