> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Compression

> Explains enabling and testing gzip and Brotli in NGINX to compress text assets, reduce bandwidth and page load times, and tune compression versus CPU tradeoffs.

Imagine you're packing for a week in Hawaii. If you toss clothes into your suitcase without folding them, you’ll struggle to close it. Fold and compress your clothes and they take up less space — you can fit more. The same principle applies to web traffic: compression reduces file sizes so they use network bandwidth more efficiently.

When the server compresses a file, it sends a smaller payload to the browser. The browser decompresses it and displays the original resource. Without compression, the server sends full HTML, JavaScript, CSS, and other assets, which increases page load times—especially for users on slower networks or limited mobile plans.

![A slide titled "Data Compression" showing a laptop connected to a desktop monitor with files being packaged into a blue ZIP folder and a progress bar. Icons for different file types (HTML, JPEG, EPS) float above the ZIP to illustrate compression.](https://mintcdn.com/kodekloud-c4ac6d9a/ij3FNIPjp1w0Myd0/images/Nginx-for-Beginners/Performance/Compression/data-compression-zip-files-progress.jpg?fit=max\&auto=format\&n=ij3FNIPjp1w0Myd0\&q=85\&s=8692ced58cc97c4f375023785137afb1)

Using compression reduces transferred bytes, improves Time to First Byte (TTFB) as seen by end users, and generally produces a faster, more responsive site.

![A simple diagram titled "Fast Response" showing a server on the left connected by an arrow to a laptop browser on the right. The arrow is labeled "Sends only useful data," indicating selective data transfer.](https://mintcdn.com/kodekloud-c4ac6d9a/ij3FNIPjp1w0Myd0/images/Nginx-for-Beginners/Performance/Compression/fast-response-selective-data-transfer.jpg?fit=max\&auto=format\&n=ij3FNIPjp1w0Myd0\&q=85\&s=bc5ebb67928760eb9a88bbb92221f38a)

## What to compress

Compress text-based resources. These benefit the most:

| Compressible (good candidates) | Not recommended (already compressed) |
| - | - |
| `text/html`, `text/css`, `text/javascript` | `image/jpeg`, `image/png` |
| `application/javascript`, `application/json` | `video/mp4`, `audio/mp3` |
| `application/xml`, `text/xml` | `archive.zip`, other compressed archives |

![A slide titled "Supported Compression" showing six stylized file icons labeled CSS, HTML, XML, JSON, JS and JPEG inside a rounded box. A green checkmark appears beside the box indicating support.](https://mintcdn.com/kodekloud-c4ac6d9a/ij3FNIPjp1w0Myd0/images/Nginx-for-Beginners/Performance/Compression/supported-compression-file-types-check.jpg?fit=max\&auto=format\&n=ij3FNIPjp1w0Myd0\&q=85\&s=70bb960d19fa4e3900b4d2cf718ed173)

Most image formats and many media files are already compressed at creation time (e.g., Photoshop export, camera encoders). Attempting to compress them again gives little to no benefit and wastes CPU.

![A slide titled "Unsupported Compression" showing four file icons (AVI, MP4, MP3, ZIP) inside a rounded box with a red X to indicate they're not supported. The image includes a small "© Copyright KodeKloud" notice at the bottom.](https://mintcdn.com/kodekloud-c4ac6d9a/ij3FNIPjp1w0Myd0/images/Nginx-for-Beginners/Performance/Compression/unsupported-compression-file-icons.jpg?fit=max\&auto=format\&n=ij3FNIPjp1w0Myd0\&q=85\&s=5a5d05d496eb1e5be9feeebc892e0b10)

## Compression methods supported by NGINX

NGINX commonly supports two server-side compression algorithms:

* gzip — very widely supported and available in most NGINX packages. Uses `.gz` format. Has compression levels 1–9 (1 = fastest, least compression; 9 = slowest, most compression). Level 6 is a common default.
* Brotli (`br`) — often achieves better compression ratios than gzip and supports levels 0–11. Brotli is increasingly supported by browsers but may require additional modules for some NGINX builds.

![A slide about Gzip showing a file/zip icon and bullet points noting it was released in the 90s, uses the .gz format, is available on Linux/Unix, and is used via the gzip CLI. To the right is a compression-level scale from 1 to 9 with the default set at 6.](https://mintcdn.com/kodekloud-c4ac6d9a/ij3FNIPjp1w0Myd0/images/Nginx-for-Beginners/Performance/Compression/gzip-gz-linux-cli-compression-6.jpg?fit=max\&auto=format\&n=ij3FNIPjp1w0Myd0\&q=85\&s=24e34be413d2e46ffee21719f5f9240f)

### Example: compress a file with the gzip CLI

```bash theme={null}
$ gzip ubuntu-jammy-jellyfish.iso
$ ls -l ubuntu-jammy-jellyfish.*
-rw-r--r-- 1 user group 4294967296 ubuntu-jammy-jellyfish.iso
-rw-r--r-- 1 user group 234567890 ubuntu-jammy-jellyfish.iso.gz
```

Note: the sizes above are illustrative; the `.gz` file is the compressed output.

## Enabling gzip in NGINX

Most packaged NGINX installations already include gzip directives in the `http` block of `/etc/nginx/nginx.conf`. They may be commented out — enabling gzip involves a few key settings:

```nginx theme={null}
http {
    gzip on;
    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_buffers 16 8k;
    gzip_http_version 1.1;
    gzip_types
        text/plain
        text/css
        text/html
        text/xml
        text/javascript
        application/json
        application/javascript
        application/xml+rss;
}
```

Key parameters:

* `gzip on;` — enable gzip compression.
* `gzip_vary on;` — add `Vary: Accept-Encoding` so caches store correct variants.
* `gzip_proxied any;` — allow compression for proxied clients (useful when NGINX is reverse-proxying).
* `gzip_comp_level` — choose compression level (1–9).
* `gzip_buffers` — memory allocated to compression.
* `gzip_http_version` — minimum HTTP version required (HTTP/1.1 recommended).
* `gzip_types` — list of MIME types to compress. Adjust to match your app (e.g., fonts).

## Brotli in NGINX

Brotli typically produces smaller payloads than gzip at similar CPU cost, but availability depends on your NGINX build:

* NGINX Plus: Brotli available via NGINX tooling/modules.
* Open-source NGINX: Brotli often provided as a third-party module or distro-provided dynamic module; you may need to compile or install the module separately.

A typical Brotli configuration resembles gzip:

```nginx theme={null}
http {
    brotli on;
    brotli_comp_level 4;
    brotli_types
        text/plain
        text/css
        text/html
        text/xml
        text/javascript
        application/json
        application/javascript
        application/xml+rss;
}
```

If your distribution offers a dynamic Brotli module, enable and load it; otherwise, you can compile NGINX with the Brotli module (see the next section).

### Compiling NGINX with extra modules (example)

If you compile NGINX to add third-party modules (for example, open-source Brotli), a typical workflow looks like:

```bash theme={null}
wget https://nginx.org/download/nginx-1.27.0.tar.gz
tar zxf nginx-1.27.0.tar.gz
cd nginx-1.27.0

# Example configure command with additional modules (split for readability)
./configure \
  --sbin-path=/usr/local/nginx/nginx \
  --conf-path=/usr/local/nginx/nginx.conf \
  --pid-path=/usr/local/nginx/nginx.pid \
  --with-pcre=../pcre2-10.42 \
  --with-zlib=../zlib-1.2.13 \
  --with-http_ssl_module \
  --with-stream \
  --with-mail=dynamic \
  --add-module=/usr/build/nginx-rtmp-module \
  --add-dynamic-module=/usr/build/3party_module
```

Use `--add-module` (statically compiled) or `--add-dynamic-module` (loadable module). Compiling is more effort than installing a package, so weigh the benefits (e.g., Brotli) versus maintenance overhead.

Compression improves network performance but consumes CPU. Test different `gzip_comp_level` / `brotli_comp_level` values to balance CPU cost and compressed size, especially under high traffic.

## How to confirm compression is active

Browsers send an `Accept-Encoding` header to indicate supported encodings (e.g., `gzip`, `br`). The server responds with a `Content-Encoding` header when it compresses a resource.

Example request headers from a browser:

```http theme={null}
:authority: fav-kitties-compress-starter.glitch.me
:method: GET
:path: /main.bundle.js
:scheme: https
accept: */*
accept-encoding: gzip, deflate, br
accept-language: en-US,en;q=0.9
cache-control: no-cache
pragma: no-cache
```

Example response headers showing compression:

```http theme={null}
Content-Encoding: gzip
Content-Type: text/html; charset=UTF-8
Vary: Accept-Encoding
Cache-Control: private, max-age=0
Date: Fri, 27 May 2016 16:53:50 GMT
Server: gws
Status: 200
```

You can verify compression in multiple ways:

* Browser DevTools: Network tab → inspect a resource → check `Response Headers` for `Content-Encoding: gzip` or `Content-Encoding: br`.
* curl:

```bash theme={null}
curl -I -H "Accept-Encoding: br,gzip" https://example.com | grep -i content-encoding
# Example output:
# Content-Encoding: br
```

* Automated tests or monitoring that check response sizes and headers across your site.

Avoid compressing already-compressed binary formats (images, audio, video, ZIP). Compress text-based assets (HTML, CSS, JS, JSON, XML) and ensure `Vary: Accept-Encoding` is present so caches handle compressed/uncompressed responses correctly.

## Next steps

* Enable gzip or Brotli on a staging/test NGINX server.
* Measure before/after: compressed vs uncompressed response sizes in DevTools.
* Tune compression levels for acceptable CPU use and effective size reduction.
* Consider using a CDN that supports Brotli/gzip and automatic negotiation.

Links and references:

* [NGINX gzip documentation](https://nginx.org/en/docs/http/ngx_http_gzip_module.html)
* [Brotli compression](https://github.com/google/brotli)
* [How to test compression with curl](https://ec.haxx.se/usingcurl.html#Accept-Encoding)

> **[Watch Video](https://learn.kodekloud.com/user/courses/nginx-for-beginners/module/4a5db5c4-df5f-4291-84f0-013d1c4ce235/lesson/f9e0c6ca-f04d-4d94-8b23-b342c8161065)**


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.