High Gzip compression can reduce text-based files by up to 80%, but using the maximum level (9) adds heavy CPU overhead with only small gains. The best balance for most servers is level 4–6, which provides strong compression without slowing response times.
Compression Levels
- Level 1–3: Fastest, 60–70% reduction. Good for APIs or real-time responses.
- Level 4–6: Balanced, 75–80% reduction. Recommended defaults for Apache and NGINX.
- Level 7–9: Highest compression, 82–87% reduction. Slower, best for static assets or archives.
Performance Trade-offs
- Level 9 can take 10× longer than level 1 but only improves compression by 3–5%.
- Over-compressing already optimized formats (JPEG, PNG, MP4, ZIP, PDF) wastes CPU and may even increase file size.
Apache Example
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/css application/javascript application/json
Header append Vary: Accept-Encoding
</IfModule>
NGINX Example
gzip on;
gzip_comp_level 5;
gzip_types text/html text/plain text/css application/javascript application/json application/xml;
add_header Vary Accept-Encoding;
Best Practices
- Use level 4–6 for most sites.
- Always include Vary: Accept-Encoding for proper CDN caching.
- Avoid compressing binary formats.
- Test with
curl -I -H 'Accept-Encoding: gzip' to confirm compression is active.
Join The Discussion
Do you prefer optimizing for speed with lower compression levels or for size savings with higher levels? Share your experience with tuning Gzip on Apache or NGINX.