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

Brotli vs gzip Compression

performance PHP 5.0+ Intermediate

Also Known As

gzip brotli content-encoding response compression

TL;DR

Brotli (br) compresses 15-25% better than gzip for text content — use Brotli for supported browsers (all modern ones), gzip as fallback, both configured at the server level not PHP.

Explanation

HTTP response compression reduces transfer size by 60-80% for text content (HTML, CSS, JS, JSON). gzip: universal support, good compression. Brotli: designed for web, 15-25% better compression than gzip, slightly slower to compress (pre-compress static files), supported by all modern browsers via Accept-Encoding: br. Configure at nginx/Apache level — never use PHP's ob_gzhandler() in production as it adds CPU overhead on every request without the benefits of server-level caching. Pre-compress static assets at build time.

Common Misconception

PHP's ob_gzhandler is equivalent to server-level compression — PHP-level compression runs on every request without caching; nginx gzip compresses once and caches the result for static files.

Why It Matters

A 200KB JavaScript bundle served without compression takes 4x longer to download than compressed — enabling Brotli on static assets costs nothing and reduces page load time for every user.

Common Mistakes

  • ob_gzhandler() in production PHP — use nginx/Apache compression instead.
  • Not pre-compressing static assets — serve pre-built .br and .gz files directly.
  • Compressing already-compressed content (images, videos) — wastes CPU with no benefit.
  • No Vary: Accept-Encoding header — CDNs may serve compressed content to clients that don't support it.

Code Examples

✗ Vulnerable
// PHP-level compression — runs on every request:
// php.ini:
zlib.output_compression = On
// or:
ob_start('ob_gzhandler');
// CPU overhead per request, no caching, misses static file benefits
✓ Fixed
# nginx — server-level Brotli + gzip:
brotli on;
brotli_comp_level 6;
brotli_types text/plain text/css application/javascript application/json;

gzip on;
gzip_comp_level 5;
gzip_types text/plain text/css application/javascript application/json;
gzip_vary on;  # Add Vary: Accept-Encoding

# Pre-compressed static assets:
location ~* \.(js|css|html)$ {
    gzip_static on;    # Serve .gz if exists
    brotli_static on;  # Serve .br if exists
}

Added 16 Mar 2026
Edited 22 Mar 2026
Views 47
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 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 1 ping F 2 pings S 1 ping S 1 ping M 0 pings T 0 pings W 0 pings T 2 pings F 1 ping S 0 pings S 1 ping M 0 pings T 0 pings W 1 ping T
No pings yesterday
Amazonbot 13 Google 5 Perplexity 5 SEMrush 3 Unknown AI 2 Ahrefs 1
crawler 26 crawler_json 3
DEV INTEL Tools & Severity
🟡 Medium ⚙ Fix effort: Low
⚡ Quick Fix
Let Nginx handle gzip/brotli compression, not PHP — Nginx compresses much faster with less CPU and can cache compressed output; PHP's ob_gzhandler duplicates work Nginx already does
📦 Applies To
PHP 5.0+ web
🔗 Prerequisites
🔍 Detection Hints
ob_gzhandler used when Nginx handles compression; zlib.output_compression enabled alongside Nginx gzip; no compression headers on text responses
Auto-detectable: ✗ No curl lighthouse chrome-devtools
⚠ Related Problems
🤖 AI Agent
Confidence: Medium False Positives: Medium ✓ Auto-fixable Fix: Low Context: File

✓ schema.org compliant