參考:http://www.javashuo.com/article/p-ahhqnbpg-eh.htmlnginx
一、安裝fail2ban:服務器
yum -y install epel-releasetcp
yum -y install fail2banpost
二、配置fail2ban:.net
修改/etc/fail2ban/jail.confrest
[default] destemail = abc@def.com sender = nginx-1@def.com #這裏須要修改發送和接收郵件的郵箱 [nginx-get-404] enabled = true port = http,https filter = nginx action = iptables[name=nginx, port=http, protocol=tcp] %(mta)s-whois[name=%(__name__)s, sender="%(sender)s", dest="%(destemail)s", protocol="%(protocol)s", chain="%(chain)s"] #這裏增長了郵件通知 logpath = /var/log/nginx/access.log bantime = 3600 findtime = 60 maxretry = 5
增長/etc/fail2ban/filter.d/nginx.confcode
[Definition] failregex = <HOST> -.*- .*HTTP/1.* 404 .*$ ignoreregex = <HOST> -.*- .*GET /www/common/.*$ #ignoreregex是忽略的地址,防止因頁面代碼寫的很差引起的錯判,若是不須要能夠留空
修改/etc/postfix/mail.cfblog
主要是修改releyhost = 10.0.30.1ip
自己有內部郵件的smtp轉發服務器get
三、檢查fail2ban規則
fail2ban-regex /var/log/nginx/access.log /etc/fail2ban/filter.d/nginx.conf /etc/fail2ban/filter.d/nginx.conf --print-all-match --print-all-ignore
注意:上面有2次用到/etc/fail2ban/filter.d/nginx.conf,是由於我配置了ignoreregex的內容
四、啓動fail2ban和postfix
service postfix restart service fail2ban restart
五、檢查fail2ban
fail2ban-client status fail2ban-client status ngixn-get-404
分別檢查的是fail2ban的總開關狀態,和其中nginx-get-404功能的掃描狀態