How does Nginx handle gzip compression?
See how Nginx gzip compression shrinks text responses to save bandwidth, with gzip_types, gzip_comp_level, and gzip_static configuration examples.
Expected Interview Answer
Nginx compresses HTTP responses on the fly using the gzip module, shrinking text-based content before sending it to clients that advertise `Accept-Encoding: gzip`. You enable it with `gzip on;` and tune which content types, sizes, and compression levels to apply.
When gzip is enabled, Nginx checks the client's `Accept-Encoding` header, compresses matching responses, and adds `Content-Encoding: gzip`. You control behavior with directives like `gzip_types` (which MIME types to compress), `gzip_min_length` (skip tiny responses), and `gzip_comp_level` (1-9 trade-off between CPU and ratio). Because gzip on already-compressed formats like JPEG or ZIP wastes CPU for little gain, you target text assets such as HTML, CSS, JS, and JSON. For static files you can precompress and serve `.gz` versions with the `gzip_static` module to avoid repeated compression work.
- Reduces bandwidth and transfer size significantly for text
- Speeds up page loads for users on slower connections
- Configurable per content type and minimum size
- gzip_static can serve precompressed files to save CPU
- Transparent to clients via standard HTTP headers
AI Mentor Explanation
Think of a team packing kit for an away tour. Bulky text-like items — jerseys, towels, flags — get vacuum-compressed to fit more into each bag, while already-tight items like the hard cricket ball gain nothing from squeezing. Nginx gzip works the same way: it compresses roomy text responses (HTML, CSS, JS) before shipping them, but skips already-compact formats like images where squeezing wastes effort for no space saved.
Step-by-Step Explanation
Step 1
Enable gzip
Add `gzip on;` in the http, server, or location block of your Nginx config.
Step 2
Select content types
List compressible MIME types with `gzip_types` such as text/css, application/javascript and application/json.
Step 3
Set a minimum size
Use `gzip_min_length` to skip very small responses where compression overhead outweighs savings.
Step 4
Tune the level
Pick a `gzip_comp_level` between 1 and 9, balancing CPU cost against compression ratio (often 4-6).
Step 5
Consider precompression
Enable `gzip_static on;` to serve prebuilt .gz files for static assets and save runtime CPU.
Step 6
Verify
Check the response for `Content-Encoding: gzip` using curl or browser dev tools.
What Interviewer Expects
- Knowing `gzip on;` enables compression
- Reliance on the client's Accept-Encoding header
- Awareness of gzip_types and gzip_min_length
- Understanding the CPU vs ratio trade-off in gzip_comp_level
- Not compressing already-compressed formats like images
Common Mistakes
- Compressing already-compressed formats like JPEG or ZIP
- Setting gzip_comp_level to 9 and overloading the CPU
- Forgetting to add relevant MIME types to gzip_types
- Compressing tiny responses where overhead exceeds savings
- Assuming gzip works without the client sending Accept-Encoding
Best Answer (HR Friendly)
“Gzip lets Nginx squeeze text-based web files smaller before sending them, so pages load faster and use less data. You turn it on, tell Nginx which file types to compress, and it automatically compresses responses for browsers that support it.”
Code Example
gzip on;
gzip_comp_level 5;
gzip_min_length 256;
gzip_vary on;
gzip_types
text/plain
text/css
application/javascript
application/json
image/svg+xml;
# Serve precompressed .gz files when available
gzip_static on;Follow-up Questions
- What does the gzip_vary directive do?
- Why should you avoid gzipping images and video?
- How does gzip_static differ from on-the-fly gzip?
- What is the trade-off of a high gzip_comp_level?
- How does Brotli compression compare to gzip in Nginx?
MCQ Practice
1. Which directive enables gzip compression in Nginx?
`gzip on;` activates the gzip module for the given context.
2. Which content type is a poor candidate for gzip?
JPEG images are already compressed, so gzipping them wastes CPU for negligible size reduction.
3. What does gzip_min_length control?
`gzip_min_length` skips responses below the given size where compression overhead outweighs savings.
Flash Cards
How do you enable gzip? — Add `gzip on;` and list compressible types with `gzip_types`.
What header triggers gzip? — The client's `Accept-Encoding: gzip`; Nginx replies with `Content-Encoding: gzip`.
What does gzip_comp_level tune? — The 1-9 trade-off between CPU usage and compression ratio.
What is gzip_static? — Serving precompressed .gz files for static assets to avoid runtime compression.