{"slug": "why-your-streaming-llm-ai-webhooks-buffer-behind-docker-reverse-proxies-and-the", "title": "Why Your Streaming LLM & AI Webhooks Buffer Behind Docker Reverse Proxies (And the 25-Line Caddy v2 Fix)", "summary": "A developer published a 25-line Caddy v2 configuration for running streaming LLM and AI agent webhook backends behind Docker reverse proxies, addressing SSE token buffering and 504 Gateway Timeout failures. The setup uses flush_interval -1, stream_timeout 30m, and a 300s response_header_timeout on a dedicated handle block for streaming routes, while keeping gzip/zstd compression off those paths. It also recommends persistent /data and /config Docker volumes to avoid re-requesting Let's Encrypt certificates on every restart.", "body_md": "`flush_interval -1`, `response_header_timeout 300s`, and `stream_timeout 30m` on your streaming/webhook `handle` block, and keep `encode gzip` outside that route.\nYour streaming AI chat or multi-step agent webhook works smoothly on `http://localhost:8000`. Then you put Docker and a reverse proxy in front of it for HTTPS—and suddenly token-by-token SSE streaming stalls, or long reasoning chains crash with **`504 Gateway Timeout`**.\n\nBelow is the exact **25-line Caddy v2 production configuration** we deploy in 2026 for streaming LLMs, FastAPI AI workers, and n8n webhooks—plus zero-downtime reloads.\n\n| Reverse Proxy / Edge Option | Auto Let's Encrypt TLS | SSE Streaming Config Effort | Zero-Downtime Reload | Monthly Infra Cost | \n|---|---|---|---|---|\n| **Caddy v2 in Docker on **Hostinger VPS** ** | **Built-in automatic** | 3 directives ( `flush_interval -1` + timeouts) | `caddy reload` (instant) | **~$6.49–$8.79/mo** flat VPS | \n| **Nginx + Certbot on VPS** | Manual Certbot cron | Requires `proxy_buffering off` ,`X-Accel-Buffering` , chunked headers | `nginx -s reload` | ~$6.49–$8.79/mo flat VPS | \n| **Managed Edge on **Railway** ** | **Built-in automatic** | Zero config (managed proxy) | Managed rolling deploys | ~$15–$45+/mo (24/7 metered) | \n\n`text/event-stream` (SSE) LLM outputs, that destroys perceived latency.`504` errors.`encode gzip zstd` on an SSE route causes the compressor to wait for enough bytes to build a compression dictionary, stalling individual tokens.`Caddyfile` (Copy-Paste Ready)\nSplit your streaming/webhook paths into a dedicated `handle` block so normal static/JSON routes still benefit from fast defaults while AI routes stream immediately:\n\n```\n{\n    email ops@yourdomain.com\n}\n\nai.yourdomain.com {\n    # Dedicated non-buffered route for LLM SSE streams & slow AI agent webhooks\n    handle /v1/chat/completions* {\n        reverse_proxy http://ai-backend:8000 {\n            # Flush every SSE chunk immediately (disables response buffering)\n            flush_interval -1\n            stream_timeout 30m\n            transport http {\n                # Wait up to 5 minutes for first token / multi-step agent reply\n                response_header_timeout 300s\n                keepalive 3m\n            }\n            header_up X-Forwarded-For {remote_host}\n            header_up X-Forwarded-Proto {scheme}\n        }\n    }\n\n    # Standard API / UI routes (safe to compress)\n    handle {\n        encode zstd gzip\n        reverse_proxy http://ai-backend:8000 {\n            transport http {\n                response_header_timeout 60s\n            }\n        }\n    }\n}\n```\n\nAlways persist `/data` and `/config` in named Docker volumes. If you rebuild Caddy without a persistent `/data` volume, every container restart requests a brand-new Let's Encrypt certificate—hitting Let's Encrypt's weekly rate limit within an hour.\n\n```\nservices:\n  caddy:\n    image: caddy:2-alpine\n    restart: unless-stopped\n    mem_limit: 256m\n    ports:\n      - \"80:80\"\n      - \"443:443\"\n    volumes:\n      - ./Caddyfile:/etc/caddy/Caddyfile:ro\n      - caddy_data:/data\n      - caddy_config:/config\n    networks:\n      - ai-net\n\nvolumes:\n  caddy_data:\n  caddy_config:\n\nnetworks:\n  ai-net:\n    driver: bridge\n```\n\nNote that inside Docker, Caddy connects to your upstream service via the Docker Compose service name (`http://ai-backend:8000` or `http://n8n-main:5678`), **never** `http://localhost:8000`.\n\nWhen you update routes or timeouts in `Caddyfile`, reload Caddy in-place without dropping active AI streams or TLS sockets:\n\n```\ndocker compose exec caddy caddy validate --config /etc/caddy/Caddyfile\ndocker compose exec caddy caddy reload --config /etc/caddy/Caddyfile\n```\n\nVerify unbuffered streaming immediately using `curl -N` (`--no-buffer`):\n\n```\ncurl -N -H \"Accept: text/event-stream\" https://ai.yourdomain.com/v1/chat/completions\n```\n\nIf tokens print line-by-line immediately instead of waiting until the stream closes, your proxy is production-ready.\n\nRun Caddy + Docker on **Hostinger VPS** for your AI APIs, RAG backends, and **n8n** webhooks—and avoid self-hosting marketing landing pages or email funnels on the same box. Offloading lead capture, checkout funnels, and automated email sequences to **Systeme.io** ($0 Free / $27/mo Startup) keeps your VPS footprint minimal and isolates customer-facing marketing uptime from backend deployments.\n\nReverse proxies buffer responses by default to optimize packet framing. Setting `flush_interval -1` inside Caddy's `reverse_proxy` block disables response buffering so every SSE token chunk streams to the client immediately.\n\nSet `response_header_timeout 300s` inside `transport http` to allow up to 5 minutes for initial LLM prefill or multi-step agent execution, and set `stream_timeout 30m` for lengthy streaming generations.\n\nIt can delay token delivery if compression buffers chunks before flushing. Isolating your SSE/streaming endpoint into its own `handle` block without `encode gzip` ensures instant token-by-token delivery.\n\nRun `docker compose exec caddy caddy validate --config /etc/caddy/Caddyfile` followed by `docker compose exec caddy caddy reload --config /etc/caddy/Caddyfile`. Caddy applies the new config gracefully with zero downtime.\n\n**Open-Source Starter Repo:** Grab the ready-to-deploy Docker Compose + Caddy + PostgreSQL + Redis stack on GitHub: [https://github.com/Soph1yzzz/n8n-ai-agent-docker-stack](https://github.com/Soph1yzzz/n8n-ai-agent-docker-stack)\n\n**Question for builders:** What is your current monthly bill (or biggest production bottleneck) running AI agents or webhooks—and are you self-hosting or staying on managed cloud? Drop your stack in the comments.\n\n*Full benchmarks, cost tables, and updated 2026 blueprints on [AgentStack](https://soph1yzzz.github.io/agentstack-hq/posts/docker-caddy-reverse-proxy-ai-webhooks-2026/).*", "url": "https://wpnews.pro/news/why-your-streaming-llm-ai-webhooks-buffer-behind-docker-reverse-proxies-and-the", "canonical_source": "https://dev.to/soph1yzzz/why-your-streaming-llm-ai-webhooks-buffer-behind-docker-reverse-proxies-and-the-25-line-caddy-v2-3cj8", "published_at": "2026-12-01 12:02:58+00:00", "updated_at": "2026-12-01 12:13:43.920152+00:00", "lang": "en", "topics": ["ai-infrastructure", "ai-agents", "large-language-models", "mlops", "developer-tools"], "entities": ["Caddy", "Docker", "Let's Encrypt", "FastAPI", "n8n", "Nginx", "Railway", "Hostinger"], "also_reported_by": [], "takeaway": "Setting flush_interval -1, stream_timeout 30m, and a 300s response_header_timeout on a dedicated Caddy handle block for SSE routes, with compression kept off that path, stops token buffering and 504s on streaming LLM backends.", "recommendation": {"advice": "Put streaming LLM and webhook routes in their own Caddy handle block with flush_interval -1 and long timeouts, and keep encode gzip/zstd off those paths so tokens flush immediately.", "playbook": "inference-serving", "playbook_url": "https://wpnews.pro/playbooks/inference-serving"}, "alternates": {"html": "https://wpnews.pro/news/why-your-streaming-llm-ai-webhooks-buffer-behind-docker-reverse-proxies-and-the", "markdown": "https://wpnews.pro/news/why-your-streaming-llm-ai-webhooks-buffer-behind-docker-reverse-proxies-and-the.md", "text": "https://wpnews.pro/news/why-your-streaming-llm-ai-webhooks-buffer-behind-docker-reverse-proxies-and-the.txt", "jsonld": "https://wpnews.pro/news/why-your-streaming-llm-ai-webhooks-buffer-behind-docker-reverse-proxies-and-the.jsonld"}}