{
    "slug": "gzip_compression",
    "term": "Gzip / Brotli Compression",
    "category": "performance",
    "difficulty": "beginner",
    "short": "Compressing HTTP responses server-side reduces transfer size significantly, especially for text-based assets.",
    "long": "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.",
    "aliases": [
        "HTTP compression",
        "gzip encoding",
        "deflate compression",
        "brotli"
    ],
    "tags": [
        "performance",
        "http",
        "infrastructure"
    ],
    "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."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "cdn",
        "http_caching"
    ],
    "prerequisites": [
        "nginx_php_fpm_config",
        "brotli_compression",
        "http_caching"
    ],
    "refs": [
        "https://developer.mozilla.org/en-US/docs/Web/HTTP/Compression",
        "https://nginx.org/en/docs/http/ngx_http_gzip_module.html"
    ],
    "bad_code": "# nginx — gzip not configured:\nserver {\n    # gzip off; (default)\n    # All responses sent uncompressed — HTML/CSS/JS 5-10x larger than needed\n}\n\n# Correct:\ngzip on;\ngzip_types text/html text/css application/javascript application/json;\ngzip_min_length 1000;",
    "good_code": "; php.ini — enable zlib output compression\nzlib.output_compression = On\nzlib.output_compression_level = 6  ; 1-9, 6 is good balance\n\n; Or in nginx (preferred — offloads from PHP)\ngzip on;\ngzip_vary on;\ngzip_min_length 1024;              ; don't compress tiny responses\ngzip_types text/plain text/css application/json application/javascript\n           text/xml application/xml image/svg+xml;\ngzip_comp_level 6;\n\n; Brotli (better compression, HTTPS only)\nbrotli on;\nbrotli_types text/plain text/css application/json application/javascript;\nbrotli_comp_level 6;",
    "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",
    "severity": "high",
    "effort": "low",
    "created": "2026-03-15",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/gzip_compression",
        "html_url": "https://codeclaritylab.com/glossary/gzip_compression",
        "json_url": "https://codeclaritylab.com/glossary/gzip_compression.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[Gzip / Brotli Compression](https://codeclaritylab.com/glossary/gzip_compression) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/gzip_compression"
            }
        }
    }
}