Overview
Caddy Defender’sratelimit responder integrates with the caddy-ratelimit module to apply rate limiting selectively based on IP ranges. This allows you to enforce stricter limits on traffic from cloud providers, VPNs, or other suspicious sources while allowing normal traffic to flow freely.
Required: This feature requires both Caddy Defender and caddy-ratelimit to be installed.
How It Works
Theratelimit responder uses a simple but effective workflow:
- IP Matching: Defender checks if the client’s IP address matches any of the configured ranges
- Header Marking: If a match is found, Defender sets the
X-RateLimit-Applyheader totrue - Request Continuation: The request continues down the handler chain to caddy-ratelimit
- Rate Limiting: caddy-ratelimit applies rate limits only to requests with the marked header
- Bypass: Non-matching requests bypass rate limiting entirely
Configuration
Caddyfile Syntax
IP ranges to apply rate limiting. Supports CIDR notation and predefined service keys.
IP addresses to exclude from rate limiting.
JSON Configuration
Examples
Basic Rate Limiting
Apply rate limiting to OpenAI and Cloudflare IP ranges:Advanced API Protection
Protect an API endpoint with different rate limits for different IP ranges:Multi-Range Rate Limiting
Apply rate limiting to multiple IP ranges including custom CIDRs:Whitelist Trusted IPs
Exclude specific IPs from rate limiting:Combining with Other Responders
Use multiple Defender configurations for layered protection:Rate Limit Configuration Options
When configuring caddy-ratelimit with Defender, use these options:match header
Match requests with the
X-RateLimit-Apply: true header set by Defender.rate
Maximum number of requests per time unit. Format:
<number>r/<unit>Units: s (second), m (minute), h (hour)Examples: 5r/s, 100r/m, 1000r/hburst
Maximum number of requests allowed in a burst before rate limiting kicks in.
key
The key to use for rate limiting. Determines how requests are grouped.Common values:
{http.request.remote.host}- Rate limit by client IP{http.request.uri.path}- Rate limit by endpoint{http.request.header.X-API-Key}- Rate limit by API key
Use Cases
Protect API Endpoints from Scraping
Prevent AI services from overwhelming your API:Mitigate Brute Force Attacks
Limit requests from VPN and Tor exit nodes:Geographic Rate Limiting
Apply stricter limits to specific cloud regions:Internal vs External Traffic
Rate limit external cloud traffic while allowing internal traffic:Troubleshooting
Verify Headers are Set
Check if Defender is setting the rate limit header:Test Rate Limiting
Simulate requests from a blocked range:Check Handler Order
Ensure Defender comes before rate_limit:Debug Logs
Enable debug logging to see Defender’s IP matching:Implementation Details
Theratelimit responder is implemented in responders/ratelimit.go:9-18:
- Sets the
X-RateLimit-Applyheader totrue - Passes the request to the next handler (caddy-ratelimit)
- Does not modify the response
Requirements
Dependencies:
- caddy-ratelimit module installed
- Caddy Defender v0.5.0 or higher
Installing caddy-ratelimit
Build Caddy with both modules:Best Practices
- Start Conservative: Begin with higher rate limits and adjust based on actual traffic patterns
- Use Whitelist: Exclude known good IPs from rate limiting
- Monitor Logs: Watch for legitimate traffic being rate limited
- Combine Strategies: Use rate limiting for AI services, blocking for malicious IPs
- Test Thoroughly: Verify rate limits work as expected before deploying to production