如何为 Nginx 使用 fail2ban?

THp*_*ubs 2 security nginx fail2ban

如何在 Nginx 服务器上使用 fail2ban?在 jails.conf 中放置的规则是什么?

atv*_*tvt 12

从下面开始 http://snippets.aktagon.com/snippets/554-How-to-Secure-an-nginx-Server-with-Fail2Ban

/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 =
Run Code Online (Sandbox Code Playgroud)

在我们的 jail.local 中,我们有(在文件末尾):

[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
Run Code Online (Sandbox Code Playgroud)

当然,如果您要记录站点的所有资源(图像、css、js 等),作为普通用户,获取这些数字将非常容易。为了避免这种情况,请使用 Nginx 的 access_log off 指令,如下所示:

 # 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;
        }
Run Code Online (Sandbox Code Playgroud)