CrowdSec: Caddy bouncer
Caddy exposed to the internet immediately faces scanning and brute-force attempts. Deploying CrowdSec as a Caddy bouncer provides reverse-proxy-aware blocking against a global intelligence feed without touching the application layer. Below is a minimal command cycle, compose file, and Caddyfile configuration that works out of the box.
Installation via Docker Compose
Building the environment takes a few minutes. We use official CrowdSec and Caddy bouncer images. The bouncer expects a connection to the CrowdSec API and receives the list of blocked IPs from it.
Run: docker compose up -d. After startup the bouncer connects to the CrowdSec API and starts serving blocked IPs to Caddy through middleware. The CROWDSEC_API_KEY variable is generated once in the CrowdSec interface or via cscli bouncer list.
Caddy Configuration with Bouncer
Caddy itself does not know about reputation IPs, so the bouncer is embedded into the request processing chain. In Caddyfile we enable the crowdsec plugin:
The crowdsec block tells Caddy to use the CrowdSec backend. If a request comes from an IP listed as badips, Caddy returns 403. Don’t forget to reload Caddy: docker compose restart caddy.
When using TLS in production make sure Caddy‑forwarded X-Forwarded-For is correctly parsed by the upstream, otherwise blocking will apply to the provider’s external IP rather than the client IP.
CrowdSec Setup and Scenarios
Without collections the bouncer has no rules. We install the minimal collection for HTTP traffic covering typical reverse‑proxy attacks.
After installation we enable the scenarios:
Typical scenarios for Caddy
| Scenario | Description | Action |
|---|---|---|
http_rate_limit | Rate limiting per IP | Block for 5m when > 100 req/min |
anti_bot | Automatic scanner detection via behavior | Challenge + block on repeat attempts |
ssh_bruteforce | (optional) SSH protection if Caddy sits next to a bastion | Not needed for pure reverse‑proxy, but often installed together |
Rules are stored in /etc/crowdsec/scenarios.d/. After changing rules reload the database: cscli update.
Checking Blocking and Typical Error
Blocking can be verified with a single request. Example via curl with a fake IP from the block list:
Now a request with this IP should return 403:
Typical error: 400 Bad Request or empty response
If Caddy returns 400 instead of 403, the most common cause is missing X-Forwarded-For header at the upstream level or incorrect CrowdSec bouncer backend address. Check:
- The Caddyfile contains
header X-Forwarded-For {remote_addr}(or your upstream sends this header). - The
CS_APIvariable in the bouncer points to the correct CrowdSec host (in compose —http://crowdsec:8080). - Bouncer logs:
docker compose logs crowdsec-bouncer— look forfailed to connectorinvalid api key.
After fixing, restart the services: docker compose up -d. Blocking takes effect within 1‑2 minutes after the IP enters the crowdsec list.
Do not store CS_API_KEY in plain text in the repository. Use a .env file and .gitignore, and in production — Docker Swarm/K8s secrets. A leaked key allows anyone to add/remove IPs from your block list.