Adding CrowdSec to a self-hosted server: banning attackers before they knock
My home server runs a dozen public sites behind Nginx Proxy Manager. CrowdSec now reads the proxy's logs, bans anyone scanning or brute-forcing, and blocks thousands of known attackers from a community list. Here's the whole setup, including the one-word mistake that made it ignore every log line.
Everything public on my home server goes through one front door: Nginx Proxy Manager (NPM), which handles HTTPS and routes each web address to the right app in Docker. That's a dozen sites open to the internet: a blog, a members area, ebook and audiobook libraries, a game server panel. Anything open to the internet gets scanned, constantly. I wanted something watching the door.
CrowdSec does exactly that. It reads logs, spots attack patterns (vulnerability scans, probing for admin pages, brute-forcing logins) and bans the IP addresses responsible. It also shares what it sees with a global network and, in return, gives you a community blocklist of addresses already caught attacking other people. It's free and open source.
How the pieces fit
CrowdSec is split into two parts, and that split matters for a Docker setup:
- The engine reads logs and makes decisions ("ban this IP for 4 hours"). It runs as a Docker container next to everything else.
- A bouncer enforces those decisions. I used the firewall bouncer, installed directly on the Docker host, so banned addresses are dropped by the Linux firewall before they ever reach NPM or any app.
There's one Docker-specific catch. Ports that Docker publishes (NPM's 80 and 443) don't go through the firewall's usual INPUT chain: Docker forwards them through its own rules. Docker provides a chain called DOCKER-USER for exactly this, so the bouncer has to hook into that one too.
Step 1: The engine
Tell CrowdSec where NPM's logs are. NPM keeps one access log and one error log per site. This goes in an "acquisition" file:
# crowdsec/acquis.d/npm.yaml
filenames:
- /var/log/npm/proxy-host-*_access.log
- /var/log/npm/proxy-host-*_error.log
- /var/log/npm/fallback_access.log
labels:
type: nginx-proxy-managerThen add the engine to the Docker Compose file, with NPM's log folder mounted read-only:
crowdsec:
image: crowdsecurity/crowdsec:latest
container_name: crowdsec
environment:
- TZ=Etc/UTC
- COLLECTIONS=crowdsecurity/nginx-proxy-manager crowdsecurity/base-http-scenarios crowdsecurity/http-cve crowdsecurity/whitelist-good-actors
ports:
- "127.0.0.1:8180:8080" # local API, only for the bouncer on this host
volumes:
- ./crowdsec/config:/etc/crowdsec
- ./crowdsec/data:/var/lib/crowdsec/data
- ./crowdsec/acquis.d:/etc/crowdsec/acquis.d:ro
- /opt/npm/data/logs:/var/log/npm:ro
restart: unless-stoppedThe collections are bundles of parsers and detection rules: one that understands NPM's log format, general web attack patterns, known web CVE exploits, and a whitelist so legitimate crawlers aren't banned. The local API is published only on 127.0.0.1, so nothing else on the network can talk to it. Port 8080 was already taken on my host, hence 8180.
docker compose up -d crowdsec
docker exec crowdsec cscli collections list
docker exec crowdsec cscli capi statusWhat you should see: the four collections enabled, and "Pulling community blocklist is enabled".
The mistake: one word in a label
My first attempt used type: npm as the label. Everything looked fine: the container ran, and it was reading the log files. But the metrics showed every line as unparsed:
docker exec crowdsec cscli metrics show acquisitioncscli explain shows exactly which parser accepted or rejected a line, and it's the tool to reach for here. It showed the NPM parser never even tried. Reading the parser itself explained why: it only looks at logs whose type starts with nginx-proxy-manager. With type: nginx-proxy-manager, every line parsed. The log format had been fine all along.
docker exec crowdsec cscli explain --file /tmp/sample.log --type nginx-proxy-managerThe same test also showed my own traffic (my uptime monitor, and me browsing from home) being whitelisted. CrowdSec ignores private network addresses by default, so you can't lock yourself out from inside the house.
Step 2: The firewall bouncer
The bouncer comes from CrowdSec's own package repository. On Debian 12:
install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://packagecloud.io/crowdsec/crowdsec/gpgkey | gpg --dearmor -o /etc/apt/keyrings/crowdsec.gpg
echo "deb [signed-by=/etc/apt/keyrings/crowdsec.gpg] https://packagecloud.io/crowdsec/crowdsec/debian/ bookworm main" > /etc/apt/sources.list.d/crowdsec.list
apt update
apt install -y crowdsec-firewall-bouncer-iptablesIt installs stopped, because it can't find a CrowdSec engine on the host: ours is in a container. Give it an API key from the engine and point it at the local port. These three settings change in /etc/crowdsec/bouncers/crowdsec-firewall-bouncer.yaml:
docker exec crowdsec cscli bouncers add host-firewall -o raw # prints the API key onceapi_url: http://127.0.0.1:8180/
api_key: PASTE_THE_KEY_HERE
iptables_chains:
- INPUT
- DOCKER-USERchmod 600 /etc/crowdsec/bouncers/crowdsec-firewall-bouncer.yaml
systemctl enable --now crowdsec-firewall-bouncerOne more thing for reboots: the bouncer hooks into a chain Docker creates, and talks to an engine Docker runs, so it should start after Docker:
mkdir -p /etc/systemd/system/crowdsec-firewall-bouncer.service.d
cat > /etc/systemd/system/crowdsec-firewall-bouncer.service.d/after-docker.conf <<'EOF'
[Unit]
After=docker.service
Wants=docker.service
[Service]
Restart=on-failure
RestartSec=30
EOF
systemctl daemon-reloadWhat you should see: the bouncer active, and new rules sending both chains to CrowdSec:
iptables -S DOCKER-USER | grep -i crowdsec
ipset list -t | grep -E "^Name|entries"Within seconds the community blocklist arrived: about 14,500 IPv4 and 400 IPv6 addresses, already blocked.
Step 3: Prove it works
Ban an address from the documentation-only range (it isn't a real host), check it lands in the firewall, then remove it:
docker exec crowdsec cscli decisions add --ip 203.0.113.77 --duration 5m --reason "bouncer test"
sleep 20
for s in $(ipset list -n | grep crowdsec); do ipset test $s 203.0.113.77 && echo "found in $s"; done
docker exec crowdsec cscli decisions delete --ip 203.0.113.77My first check looked in the wrong place and said the ban was missing. The bouncer keeps three separate lists: the community blocklist, CrowdSec's own detections, and bans added by hand. The test ban was in the third. All three are enforced by the same firewall chain.
Did it catch anything?
Within a few minutes of going live, yes: a server in a hosting company's network went looking for sensitive files (the classic .env, .git and backup-file probes) and got banned for both http-probing and http-sensitive-files. That's the background noise every public server gets, and now it stops at the firewall.
docker exec crowdsec cscli alerts list # what was detected
docker exec crowdsec cscli decisions list # who is banned right now
docker exec crowdsec cscli decisions delete --ip 1.2.3.4 # unban someoneThings worth knowing
- It shares data. To get the community blocklist, CrowdSec registers your server anonymously and reports the IPs that attack you. No account needed; you can turn sharing off, but then you lose the list.
- You can't lock yourself out from home, because private addresses are whitelisted. From outside (a phone on mobile data, say), a few hundred 404s in a row could get you banned. If that happens, unban yourself with
cscli decisions deletefrom home. - Check the parser before trusting it:
cscli metricsandcscli explainshow in seconds whether your logs are being understood. "Running" isn't the same as "working".
Total time: about half an hour, most of it spent on one word in a label.