Skip to content
DevOps AI ToolKit
Newsletter
All prompts
AI for NGINX Difficulty: Beginner ClaudeChatGPTCursor

NGINX gzip & Brotli Compression Prompt

Configure response compression correctly — the right MIME types, a sane compression level, no double-compressing already-compressed assets, and the Vary header — so you cut bandwidth without burning CPU or corrupting binary downloads.

Target user
Engineers enabling gzip/Brotli on a site or API and unsure what to compress
Difficulty
Beginner
Tools
Claude, ChatGPT, Cursor

The prompt

You are a senior infrastructure engineer who enables compression deliberately. You know that compressing already-compressed assets wastes CPU, that the compression level has diminishing returns, and that a missing `Vary` header confuses caches.

I will provide:
- The content I serve (HTML, JSON APIs, JS/CSS, images, downloads): [DESCRIBE]
- Whether the Brotli module is available: [yes / no / unsure]
- Traffic profile (CPU-bound? bandwidth-bound? CDN in front?): [DESCRIBE]
- Any large dynamic responses worth streaming: [DESCRIBE]

Build the config:

1. **gzip baseline** — `gzip on;`, a `gzip_types` list of the text-based MIME types worth compressing (HTML is implicit), `gzip_comp_level` set sensibly (explain why 9 is usually wasted CPU vs 5-6), `gzip_min_length`, and `gzip_vary on;`. Explain each.

2. **Don't compress the uncompressible** — make clear you exclude images, video, and already-gzipped archives; explain the CPU waste and the rare corruption risk on proxied pre-compressed responses.

3. **Brotli (if available)** — the `brotli on;`/`brotli_types`/`brotli_comp_level` block, and `brotli_static`/`gzip_static` to serve precompressed `.br`/`.gz` files if you have them. If the module isn't present, say so and don't emit directives that fail `nginx -t`.

4. **Proxy interaction** — note `gzip_proxied` behavior so you don't accidentally skip or double-compress responses from an upstream, and the `Vary: Accept-Encoding` implication for shared caches.

Output: (a) the complete commented `http`/`server`-level block, (b) a short table of what is and isn't compressed and why, (c) a `curl -H "Accept-Encoding: gzip, br" -I` test confirming the `Content-Encoding` response header, plus `nginx -t`. Validate with `nginx -t` and reload — confirm the Brotli module exists before shipping its directives.

Why this prompt works

Compression is the rare tuning knob that’s almost free wins until it isn’t, and the failure modes are unintuitive. The two that bite people are compressing things that are already compressed (JPEGs, MP4s, ZIP archives — pure CPU waste, occasionally corruption on proxied pre-compressed payloads) and cranking gzip_comp_level to 9 under the belief that more is better, when levels past 5 or 6 cost meaningful CPU for a rounding-error reduction in size. This prompt makes both an explicit, justified decision rather than a copy-pasted default.

The Vary: Accept-Encoding detail is small but load-bearing. Without it, a shared cache or CDN can serve a gzipped response to a client that didn’t ask for one, or cache a single variant across clients with different capabilities. Asking for gzip_vary on; and the proxy-interaction notes keeps compression from quietly breaking caching layers downstream.

The Brotli guardrail reflects a real deployment headache: Brotli isn’t in stock NGINX builds, so directives like brotli on; fail nginx -t outright if the module isn’t compiled in. By making the model confirm availability before emitting those lines — and fall back to gzip cleanly — the prompt prevents a config that blocks your reload, and the curl -I encoding check proves the negotiation actually works instead of assuming it.

Related prompts

Newsletter

Free: the DevOps AI Incident-Triage Cheat Sheet

Subscribe and we’ll send you the one-page cheat sheet — plus weekly AI prompts, automation ideas, and tool reviews for infrastructure engineers. One email a week. No spam, unsubscribe anytime.

  • AI Incident-Triage Cheat Sheet (PDF)
  • Access to 2,104 DevOps AI prompts
  • One practical workflow email per week