What is Caddy Defender?
Caddy Defender is a powerful middleware for Caddy that allows you to block or manipulate requests based on the client’s IP address. It’s particularly useful for:- Blocking AI scrapers from services like OpenAI, DeepSeek, and GitHub Copilot
- Preventing unwanted traffic from specific cloud providers or IP ranges
- Polluting AI training data by returning garbage responses
- Rate limiting suspicious traffic
- Protecting your content while serving legitimate users
Installation
Pull the Docker Image
The easiest way to use Caddy Defender is with the pre-built Docker image:
For other installation methods (building from source, using xcaddy, etc.), see the Installation Guide.
Run Caddy with Docker
Start the Caddy server with your configuration:Replace
$(pwd) with the full path to your Caddyfile directory if needed.Basic Configuration
Thedefender directive follows this syntax:
Available Responders
| Responder | Description | Required Fields |
|---|---|---|
block | Returns 403 Forbidden response | None |
custom | Returns custom message with configurable status code | message |
drop | Drops the connection without response | None |
garbage | Returns random garbage data to pollute AI training | None |
redirect | Returns 308 Permanent Redirect | url |
ratelimit | Marks requests for rate limiting (requires caddy-ratelimit) | None |
tarpit | Streams data slowly to stall bots and waste resources | None |
Predefined IP Ranges
Caddy Defender includes built-in IP ranges for popular services:For a complete list of available ranges and regional options (like
aws-us-east-1), see the Configuration Reference.Complete Example: Block AI Scrapers
Here’s a production-ready configuration that blocks AI scrapers while serving legitimate users:Advanced Example: Multi-Strategy Protection
Combine multiple defense strategies for comprehensive protection:Real-World Production Example
Here’s a complete configuration for a production website:Common Use Cases
Block AI Training
Prevent AI services from scraping your content for training data
Protect API Endpoints
Rate limit or block automated API access from cloud providers
Pollute Training Data
Return garbage data to confuse AI scrapers and waste their resources
Whitelist Trusted IPs
Allow specific IPs through while blocking entire ranges
Testing Your Configuration
Test Blocked Range (Simulation)
You can test blocking behavior by adding your IP to a custom range:Expected:
403 Forbidden responseCheck Logs
Monitor Caddy logs to see which requests are being blocked:Look for defender-related entries showing matched IP ranges.
Troubleshooting
Defender Not Blocking Requests
Check directive order
Check directive order
Ensure The defender middleware must be processed before your content handlers.
defender is ordered correctly in your global options:Verify IP range syntax
Verify IP range syntax
IP ranges must be valid CIDR notation or predefined keys:Use
plugin.go:26 to see all predefined range keys.Check client IP detection
Check client IP detection
If you’re behind a reverse proxy or CDN, Caddy might see the proxy’s IP instead of the client’s. Configure your reverse proxy to forward the real IP:
Accidentally Blocking Legitimate Users
Use whitelist for exceptions
Use whitelist for exceptions
If you need to block a range but allow specific IPs:
Use custom responder instead of block
Use custom responder instead of block
Return a helpful message instead of blocking:
Test before deploying
Test before deploying
Always test your configuration locally before deploying to production to avoid locking out legitimate traffic.
Performance Issues
IP matching is highly optimized
IP matching is highly optimized
Caddy Defender uses bart, a high-performance routing table implementation, so IP matching is extremely fast even with thousands of ranges.
Avoid excessive tarpit usage
Avoid excessive tarpit usage
While tarpit is great for wasting bot resources, too many concurrent tarpit connections can consume server resources:
Next Steps
Configuration Reference
Explore all configuration options and advanced features
All Examples
See more examples including tarpit, rate limiting, and combinations
Responder Types
Learn about all available response strategies in detail
IP Ranges
Complete list of predefined IP ranges and how to add custom ones
Need help? Check out the GitHub repository or open an issue.