Wie verwende ich fail2ban für Nginx?

Wie verwende ich fail2ban für Nginx?

Wie kann ich fail2ban auf einem Nginx-Server verwenden? Welche Regeln müssen in die jails.conf eingetragen werden?

Antwort1

Beginnen Sie mit unten http://snippets.aktagon.com/snippets/554-So sichern Sie einen nginx-Server mit Fail2Ban

Neuer Filter in /etc/fail2ban/nginx-dos.conf:

# Fail2Ban configuration file
#
# Generated on Fri Jun 08 12:09:15 EST 2012 by BeezNest
#
# Author: Yannick Warnir
#
# $Revision: 1 $
#

[Definition]
# Option:  failregex
# Notes.:  Regexp to catch a generic call from an IP address.
# Values:  TEXT
#
failregex = ^<HOST> -.*"(GET|POST).*HTTP.*"$

# Option:  ignoreregex
# Notes.:  regex to ignore. If this regex matches, the line is ignored.
# Values:  TEXT
#
ignoreregex =

In unserem jail.local haben wir (am Ende der Datei):

[nginx-dos]
# Based on apache-badbots but a simple IP check (any IP requesting more than
# 240 pages in 60 seconds, or 4p/s average, is suspicious)
# Block for two full days.
# @author Yannick Warnier
enabled = true
port    = http,8090
filter  = nginx-dos
logpath = /var/log/nginx/*-access.log
findtime = 60
bantime  = 172800
maxretry = 240

Wenn Sie alle Ressourcen Ihrer Site protokollieren (Bilder, CSS, JS usw.), wäre es natürlich sehr einfach, als normaler Benutzer auf diese Zahlen zuzugreifen. Um dies zu vermeiden, verwenden Sie die access_log off-Direktive von Nginx wie folgt:

 # Serve static files directly
        location ~* \.(png|jpe?g|gif|ico)$ {
                expires 1y;
                access_log off;
                try_files $uri $uri/ @rewrite;
                gzip off;
        }
        location ~* \.(mp3)$ {
                expires 1y;
                access_log off;
                gzip off;
        }
        location ~* \.(css)$ {
                expires 1d;
                access_log off;
        }
        location ~* \.(js)$ {
                expires 1h;
                access_log off;
        }

verwandte Informationen