apt install fail2ban
# Check regex fail2ban-regex /var/log/nginx/access.log nginx-404.conf --print-all-matched # Restart service fail2ban-client -t systemctl restart fail2ban # Check banned IPs and unban fail2ban-client status nginx-404 fail2ban-client set nginx-404 unbanip <IPADDR>
A selection of configurations for localhost protection.
[Definition] failregex = ^<ADDR> \S+ \S+ \[\] "[^"]*" 404(?:\s|$) ignoreregex = ^<ADDR> \S+ \S+ \[\] "(?:GET|HEAD) /(?:favicon\.ico|robots\.txt|apple-touch-icon[^ ]*) HTTP/[^"]*" 404(?:\s|$)
[nginx-404] enabled = true filter = nginx-404 port = http,https logpath = /var/log/nginx/access.log backend = auto usedns = no # 30 hits within 60 seconds maxretry = 30 findtime = 60 bantime = 3600 bantime.increment = true bantime.factor = 2 bantime.maxtime = 7d banaction = nftables-multiport ignoreip = 127.0.0.1/8 ::1
For common probe patterns and password spray:
[nginx-botsearch] enabled = true port = http,https logpath = /var/log/nginx/access.log maxretry = 10 findtime = 300 bantime = 3600 [nginx-http-auth] enabled = true maxretry = 10 findtime = 300 bantime = 3600
For SSH:
[sshd] enabled = true backend = systemd maxretry = 5 findtime = 600 bantime = 3600 banaction = nftables-multiport