Skip to content

Instantly share code, notes, and snippets.

@henk23
Last active September 21, 2024 06:46
Show Gist options
  • Save henk23/06a2b4eb251bc6aa7ec441ea560cedab to your computer and use it in GitHub Desktop.
Save henk23/06a2b4eb251bc6aa7ec441ea560cedab to your computer and use it in GitHub Desktop.
Caddy with json logs and fail2ban
# Fail2Ban v1.0.2 and Caddy v2.7.6
#
# Created this solution based on these 2 articles:
#
# https://muetsch.io/how-to-integrate-caddy-with-fail2ban.html
# https://www.kassner.com.br/en/2023/09/10/fail2ban-caddy-json-logs/
#
# However, the regex and date pattern shown in these posts did not quite work for me.
# This worked:
# /etc/fail2ban/filter.d/caddy.local
[Definition]
failregex = "client_ip":"<HOST>"(?:.*)"status":(?:403|404)
datepattern = "ts":{Epoch}
# /etc/caddy/Caddyfile
{
log {
output file /var/log/caddy/caddy.log
format json
}
}
(logging) {
log {
output file /var/log/caddy/{args[0]}.access.log
format json
}
}
www.example.com {
reverse_proxy localhost:8000
import logging www.example.com
}
# Catches all requests that are not handled above:
:80, :443 {
import logging catchall
respond "Access denied" 403 {
close
}
}
# /etc/fail2ban/jail.local
[caddy]
enabled = true
port = http,https
logpath = /var/log/caddy/*.log
maxretry = 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment