{
    "slug": "reverse_proxy_vs_load_balancer",
    "term": "Reverse Proxy vs Load Balancer",
    "category": "networking",
    "difficulty": "intermediate",
    "short": "A reverse proxy forwards client requests to backend servers, adding SSL termination, caching, and compression. A load balancer distributes traffic across multiple backends for scalability.",
    "long": "A reverse proxy (nginx, Caddy, Traefik) sits in front of one or more application servers: it terminates SSL, compresses responses, caches static content, handles slow clients, and hides backend topology. A load balancer (HAProxy, AWS ALB, nginx upstream) specifically distributes requests across multiple backend instances. In practice these overlap — nginx does both. PHP-FPM sits behind nginx as a reverse proxy that forwards PHP requests via FastCGI, serving static files directly.",
    "aliases": [
        "nginx reverse proxy",
        "HAProxy",
        "load balancer",
        "SSL termination"
    ],
    "tags": [
        "networking",
        "infrastructure",
        "devops",
        "nginx"
    ],
    "misconception": "A load balancer is always required for production — a single-server setup with nginx as a reverse proxy handles PHP-FPM, SSL, and static files correctly without load balancing.",
    "why_it_matters": "Nginx as a reverse proxy protects PHP-FPM from slow clients — without it, PHP workers are held open waiting for slow network uploads while nginx buffers the full request first.",
    "common_mistakes": [
        "Exposing PHP-FPM port (9000) directly — always put nginx in front; FPM has no authentication or rate limiting.",
        "Not buffering slow client responses in nginx — PHP workers are held until the client downloads the full response.",
        "Load balancing without health checks — unhealthy backends receive traffic without them.",
        "Round-robin balancing for stateful sessions without Redis — sticky sessions or centralised session storage is needed."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "load_balancing",
        "php_fpm",
        "tls_handshake",
        "gzip_compression"
    ],
    "prerequisites": [
        "load_balancing",
        "nginx_php_fpm_config",
        "ssl_certificate_types"
    ],
    "refs": [
        "https://nginx.org/en/docs/http/ngx_http_fastcgi_module.html"
    ],
    "bad_code": "# PHP-FPM exposed directly to internet:\n# docker-compose.yml:\nservices:\n  php:\n    image: php:8.3-fpm\n    ports:\n      - '9000:9000'  # NEVER expose FPM directly — no auth, protocol not HTTP",
    "good_code": "# nginx reverse proxy in front of PHP-FPM:\n# nginx.conf:\nserver {\n    listen 443 ssl http2;\n    ssl_certificate /etc/ssl/cert.pem;\n    location / {\n        fastcgi_pass php:9000;  # Internal only\n        fastcgi_buffers 16 16k;\n        fastcgi_buffer_size 32k;\n        include fastcgi_params;\n    }\n    location ~* \\.(css|js|png|jpg)$ {\n        expires 1y;\n        add_header Cache-Control 'public, immutable';\n    }\n}",
    "quick_fix": "A reverse proxy (Nginx) terminates SSL and routes to one PHP backend; a load balancer distributes across multiple backends — Nginx can do both for small PHP deployments",
    "severity": "medium",
    "effort": "medium",
    "created": "2026-03-15",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/reverse_proxy_vs_load_balancer",
        "html_url": "https://codeclaritylab.com/glossary/reverse_proxy_vs_load_balancer",
        "json_url": "https://codeclaritylab.com/glossary/reverse_proxy_vs_load_balancer.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": "[Reverse Proxy vs Load Balancer](https://codeclaritylab.com/glossary/reverse_proxy_vs_load_balancer) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/reverse_proxy_vs_load_balancer"
            }
        }
    }
}