Skip to content

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.

services:
  crowdsec:
    image: crowdsecurity/crowdsec:latest
    container_name: crowdsec
    volumes:
      - ./crowdsec/data:/var/lib/crowdsec
    cap_add:
      - NET_ADMIN

  caddy:
    image: caddy:latest
    container_name: caddy
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./Caddyfile:/etc/caddy/Caddyfile
    depends_on:
      - crowdsec

  crowdsec-bouncer:
    image: crowdsecurity/crowdsec-bouncer-caddy:latest
    container_name: crowdsec-bouncer
    environment:
      - CS_API=http://crowdsec:8080
      - CS_API_KEY=${CROWDSEC_API_KEY}
    volumes:
      - ./Caddyfile:/etc/caddy/Caddyfile

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:

:80, :443 {
    header X-Forwarded-For {remote_addr}
    # bouncer middleware — passes blocked IPs list to Caddy
    crowdsec {
        address http://crowdsec:8080
        api_key ${CROWDSEC_API_KEY}
    }
    reverse_proxy localhost:3000
}

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.

Tip

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.

cscli collections install crowdsecurity/http_rate_limit
cscli collections install crowdsecurity/anti_bot

After installation we enable the scenarios:

cscli scenarios enable crowdsecurity/http_rate_limit
cscli scenarios enable crowdsecurity/anti_bot

Typical scenarios for Caddy

ScenarioDescriptionAction
http_rate_limitRate limiting per IPBlock for 5m when > 100 req/min
anti_botAutomatic scanner detection via behaviorChallenge + block on repeat attempts
ssh_bruteforce(optional) SSH protection if Caddy sits next to a bastionNot 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:

# Manually add an IP for testing (replace with real one from `cscli bouncer list`)
cscli bouncer add 203.0.113.42

Now a request with this IP should return 403:

curl -s -o /dev/null -w "%{http_code}" http://localhost/index.html
# Expected result: 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:

  1. The Caddyfile contains header X-Forwarded-For {remote_addr} (or your upstream sends this header).
  2. The CS_API variable in the bouncer points to the correct CrowdSec host (in compose — http://crowdsec:8080).
  3. Bouncer logs: docker compose logs crowdsec-bouncer — look for failed to connect or invalid 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.

Warning

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.