← CodeClarityLab Home
Browse by Category
+ added · updated 7d
← Back to glossary

Gzip / Brotli Compression

performance Beginner

Also Known As

HTTP compression gzip encoding deflate compression brotli

TL;DR

Compressing HTTP responses server-side reduces transfer size significantly, especially for text-based assets.

Explanation

Gzip and the newer Brotli algorithm compress HTTP response bodies before transmission, reducing bandwidth and improving load times — HTML, CSS, and JS typically compress by 60–90%. In PHP applications, compression can be applied by the web server (Nginx gzip, Apache mod_deflate) rather than in PHP itself (avoid ob_gzhandler as it conflicts with streaming and partial content). Enable compression for text-based MIME types (text/html, text/css, application/json), exclude already-compressed binary formats (images, videos), and set Vary: Accept-Encoding when using compression.

Common Misconception

Gzip should be applied to all response types for maximum performance. Gzip is effective for text-based content (HTML, CSS, JS, JSON) but counterproductive for already-compressed formats like JPEG, PNG, ZIP, and video — compressing them wastes CPU and often increases response size.

Why It Matters

Gzip compresses HTTP responses by 60-90% for text content — reducing bandwidth costs and improving page load times with minimal CPU overhead.

Common Mistakes

  • Not enabling gzip for text responses (HTML, CSS, JS, JSON, XML) — the most impactful compression targets.
  • Enabling gzip for already-compressed formats (images, video, zipped files) — wastes CPU with no size benefit.
  • Using PHP ob_gzhandler() instead of server-level compression — server-level is more efficient.
  • Not checking that Content-Encoding: gzip is actually in responses — easy to misconfigure and never notice.

Code Examples

✗ Vulnerable
# nginx — gzip not configured:
server {
    # gzip off; (default)
    # All responses sent uncompressed — HTML/CSS/JS 5-10x larger than needed
}

# Correct:
gzip on;
gzip_types text/html text/css application/javascript application/json;
gzip_min_length 1000;
✓ Fixed
; php.ini — enable zlib output compression
zlib.output_compression = On
zlib.output_compression_level = 6  ; 1-9, 6 is good balance

; Or in nginx (preferred — offloads from PHP)
gzip on;
gzip_vary on;
gzip_min_length 1024;              ; don't compress tiny responses
gzip_types text/plain text/css application/json application/javascript
           text/xml application/xml image/svg+xml;
gzip_comp_level 6;

; Brotli (better compression, HTTPS only)
brotli on;
brotli_types text/plain text/css application/json application/javascript;
brotli_comp_level 6;

Added 15 Mar 2026
Edited 22 Mar 2026
Views 35
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 1 ping S 2 pings S 0 pings M 0 pings T 1 ping W 0 pings T 0 pings F 3 pings S 0 pings S 0 pings M 1 ping T 0 pings W 0 pings T 1 ping F 1 ping S 0 pings S 0 pings M 1 ping T 0 pings W 0 pings T
No pings yet today
No pings yesterday
Google 12 Perplexity 7 Amazonbot 6 Unknown AI 2 Ahrefs 2 SEMrush 2 ChatGPT 1
crawler 29 crawler_json 3
DEV INTEL Tools & Severity
🟠 High ⚙ Fix effort: Low
⚡ Quick Fix
Enable gzip in Nginx: gzip on; gzip_types text/html text/css application/javascript application/json; gzip_min_length 1000 — reduces transfer size 60-80% for text assets
📦 Applies To
any web
🔗 Prerequisites
🔍 Detection Hints
Nginx config without gzip on; PHP serving uncompressed CSS/JS; no Content-Encoding: gzip in response headers
Auto-detectable: ✓ Yes lighthouse curl webpagetest
⚠ Related Problems
🤖 AI Agent
Confidence: Medium False Positives: Low ✓ Auto-fixable Fix: Low Context: File

✓ schema.org compliant