Skip to main content
Think of packing a suitcase for a week in Hawaii. If you stuff clothes without folding them, they take more space and are harder to close. Fold them neatly and you fit more. Compression does the same for data: it reduces the size of files sent over the network so they consume less bandwidth and arrive faster. When an HTTP server compresses a response, it sends a smaller encoded version to the browser. The browser then decompresses and renders the original content. Without compression, the server sends full HTML, JavaScript, CSS, and other assets — increasing load times and data usage for end users, especially on mobile or metered connections.
An illustration labeled "Compression" showing a sanitation worker wheeling a recycling bin and carrying a trash bag. To the right, a conveyor belt feeds recyclables into a large compactor/machine marked with a recycling symbol.
The end result of proper compression: less data transferred, faster page loads, and a better user experience.
An infographic titled "Fast Response" showing a server on the left sending only useful data (icons and an arrow) to a browser displayed on a laptop on the right. The components are labeled "Server" and "Browser."

What resources should you compress?

Text-based resources usually compress well and should be enabled for compression:
  • HTML, CSS, JavaScript
  • JSON, XML
  • RSS, SVG, text files
  • Font files (e.g., font/woff, font/woff2) — can be included selectively
Binary media and many archive formats are already compressed; recompressing them yields little benefit and wastes CPU:
  • JPEG, PNG (use modern formats like WebP or AVIF instead for better compression)
  • MP4, MP3, AVI, ZIP, TAR
A slide titled "Supported Compression" showing a rounded-square graphic of six file icons labeled CSS, HTML, XML, JSON, JS and JPEG, with a green checkmark indicating support.
A slide titled "Unsupported Compression" showing four file icons (AVI, MP4, MP3, ZIP) inside a rounded box. A red X next to the box indicates those formats are not supported.
Table — quick guidance

Supported algorithms in NGINX

NGINX supports two widely used compression algorithms: gzip and Brotli.

gzip

  • Widely supported across browsers and servers (legacy compatibility).
  • Compression levels 1–9 (1 = fastest, 9 = best compression, default commonly 6).
  • Available as a built-in NGINX feature on most distributions.
A slide about Gzip showing an icon and brief facts (released in the 90s, .gz file format, available on Linux/Unix, use the gzip CLI). It also shows a compression-level scale from 1 to 9 with 6 marked as the default.
Example: compress a file using the gzip CLI
Recommended minimal nginx.conf gzip configuration (place inside the http { ... } block, often in /etc/nginx/nginx.conf):
Notes:
  • gzip on; — enable gzip compression.
  • gzip_comp_level — set CPU vs. compression trade-off (1–9). Level 6 is a sensible default.
  • gzip_types — list MIME types to compress (add font/woff and font/woff2 if desired).
  • gzip_proxied any; — allows compression for proxied requests.

Brotli

  • Better compression ratios in many cases (levels 0–11).
  • Modern browsers advertise Brotli via br in Accept-Encoding.
  • Not always built into stock NGINX; often provided via the third-party ngx_brotli module or available in vendor packages.
A presentation slide titled "Brotli - Nginx Plus" showing the Brotli logo with the words "brotli" and "Debian/Ubuntu" beside it. The slide also has a small "© Copyright KodeKloud" note in the corner.
Brotli is commonly added to open-source NGINX via the ngx_brotli module. See the module repository for installation options and instructions: Comparison (gzip vs Brotli) If you decide to build NGINX from source to include third-party modules (e.g., Brotli), the typical sequence is:
Compiling NGINX adds complexity and is usually unnecessary unless you require a specific third-party module not available from your vendor.

How the server and browser negotiate compression

Clients tell servers which encodings they accept via the Accept-Encoding request header. Example request header from a browser:
If the server chooses gzip or Brotli, it responds with a Content-Encoding response header indicating the encoding used. Example response headers:
To confirm compression is active:
  1. Open your browser DevTools → Network tab.
  2. Select a resource and inspect Response Headers for Content-Encoding.
  3. Confirm the resource is smaller than the uncompressed version (DevTools shows transfer size vs resource size).
Avoid compressing already-compressed formats (MP4, MP3, ZIP, most JPEGs). Compress text-based assets (HTML, CSS, JS, JSON, XML) to get the best savings with minimal CPU overhead.

Summary and recommendations

  • Enable gzip by default for broad compatibility; start with gzip_comp_level 6.
  • Use Brotli if you can add/enable the module and want better compression ratios for text assets — balance the Brotli level against CPU cost.
  • Do not compress already-compressed media and archive formats; instead adopt modern image formats (WebP/AVIF) where appropriate.
  • Verify using browser DevTools (check Accept-Encoding and Content-Encoding headers).
If you want to try this hands-on, enable gzip in your NGINX configuration, reload NGINX, and use browser DevTools to verify Content-Encoding behavior for HTML, CSS, and JS resources.

Watch Video