Caddy does not ship rate limiting in the standard binary — the policy decisions (per IP, per token, per route) and storage decisions (in-memory, Redis-backed) vary enough that the community plugin mholt/caddy-ratelimit is what we deploy. This article is the production configuration we apply at clients: zone definitions, key strategies, distributed storage for multi-node fleets, and the alert thresholds that catch abuse before it pages the application.
How to verify
A working rate limit returns 429 when exceeded and lets normal traffic through:
for i in $(seq 1 50); do curl -o /dev/null -s -w '%{http_code}\n' https://api.example.com/login; done | sort | uniq -c
curl -i https://api.example.com/login # one shot, headers
caddy list-modules | grep -i ratelimit
sudo journalctl -u caddy --since '5m ago' | grep -i 'rate\|429'
The first loop hits the endpoint 50 times rapidly. The output should show a transition from 200 to 429 after the limit. The single curl shows the rate-limit headers Caddy adds (X-RateLimit-Limit, X-RateLimit-Remaining, Retry-After).
What’s happening
The caddy-ratelimit plugin is a Caddy module that enforces per-key request limits using a sliding window or fixed window algorithm. The plugin registers a handler that runs early in the request pipeline; on every request it:
- Computes the limit key (IP, header, cookie, or any combination via placeholders).
- Looks up the current counter for the key.
- If the counter is below the limit, increments and lets the request through.
- If at the limit, returns 429 with
Retry-Afterand a configurable response body.
Storage matters. The default is in-memory — fine for a single Caddy instance, useless for a multi-node deployment where requests are spread across nodes. For multi-node, the plugin supports Redis or any storage backend that exposes atomic increment.
Zones partition the rate limit space. You can have multiple zones with different keys and limits — one zone for global IP-based throttling (1000/min), another for per-user-token API access (100/min), another for login endpoints (5/min). Routes opt into zones via matcher.
The procedure
-
Build Caddy with the rate limit plugin. Stock Caddy does not include it:
xcaddy build \ --with github.com/mholt/caddy-ratelimit \ --output /usr/local/bin/caddy sudo systemctl restart caddySee xcaddy custom builds for the build mechanics.
-
Configure a global IP rate limit. Cap any single IP at 1000 requests per minute across the entire site:
{ order rate_limit before basicauth } api.example.com { rate_limit { zone global_ip { key {client_ip} events 1000 window 1m } } reverse_proxy localhost:3000 }The
orderdirective in the global block tells Caddy where in the request pipeline to run the rate limit handler. Thezonedefines the key ({client_ip}is the source IP), event count (1000), and time window (1m). -
Per-endpoint stricter limits. Login endpoints need tight bounds; API queries can be looser:
api.example.com { rate_limit { zone login { match { path /login /signup /password/reset } key {client_ip} events 5 window 1m } zone api_global { key {client_ip} events 1000 window 1m } } reverse_proxy localhost:3000 }Routes matching the
loginzone’s matcher count against the login bucket (5/min). All other traffic counts againstapi_global(1000/min). A request matches multiple zones can be hit by any of them. -
Key by authenticated user, not IP. When the request carries an auth token, limit by token so a NAT’d corporate network does not share a budget:
api.example.com { rate_limit { zone per_token { key {http.request.header.authorization} events 100 window 1m } } reverse_proxy localhost:3000 }{http.request.header.authorization}is the full Authorization header value (includingBearer). For a cleaner key, use a placeholder that the application sets, or strip the prefix in avarsdirective earlier in the chain. -
Distributed storage for multi-node Caddy. When you run multiple Caddy instances behind a load balancer, in-memory counters do not share state:
{ storage redis { host redis.internal port 6379 } } api.example.com { rate_limit { distributed zone global_ip { key {client_ip} events 1000 window 1m } } reverse_proxy localhost:3000 }distributedopts the rate limiter into the global storage backend. Each node increments the same counter, so the limit is enforced across the whole fleet. -
Allowlist trusted IPs. Office IPs, CI/CD agents, the alerting box — these should bypass:
api.example.com { @trusted client_ip 198.51.100.0/24 203.0.113.10 handle @trusted { reverse_proxy localhost:3000 } rate_limit { zone global { key {client_ip} events 100 window 1m } } reverse_proxy localhost:3000 }The
@trustedhandle runs first and skips the rate_limit block. Order in the Caddyfile determines execution order forhandleblocks. -
Custom 429 response. The default 429 body is plain text. Replace with JSON for API clients:
api.example.com { rate_limit { zone global { key {client_ip} events 100 window 1m } } handle_errors { @ratelimit expression {err.status_code} == 429 handle @ratelimit { header Content-Type application/json respond `{"error": "rate_limited", "retry_after_seconds": 60}` 429 } } reverse_proxy localhost:3000 }See Caddy error handling for the broader pattern.
Operational notes
- The
{client_ip}placeholder is Caddy’s view of the immediate peer. If Caddy is behind a CDN or load balancer, that is the CDN’s IP, not the user’s. Configuretrusted_proxiesso{client_ip}reflects the actual source. - Memory growth is bounded by the number of distinct keys in the window. For IP-based limits, that is roughly your unique-visitor count. For token-based limits, it is bounded by your active token count. Both are fine; per-cookie limits without bounds can grow indefinitely if cookies are random per request.
- Redis-backed distributed limits add per-request latency (1-5ms typically). For very high-throughput edges, the in-memory mode plus per-node limits divided by node count is faster, at the cost of per-node enforcement (a burst on one node may exceed the global “true” limit).
- The plugin’s
eventsis a count, not a rate.events 100, window 1mis “100 events per rolling 1-minute window.” It will allow 100 in the first second of a minute, then nothing until enough seconds elapse — burst-permissive within the window. - Test 429 responses in staging — a misconfigured matcher can rate-limit your own monitoring probes and trigger false alerts. Allowlist Prometheus, your uptime checker, and your alerting webhook source IPs.
Stack Harbor configures rate limiting as part of the abuse protection layer for client edges — zones tied to actual endpoints, distributed storage for multi-node fleets, and the 429 rate monitored as a metric (a sudden surge tells you about either an abuse spike or a legitimate traffic event the application could not absorb). If you have an edge that takes a beating from automated traffic, this is the kind of work covered by our Managed Operations practice.