You can setup Nginx to cache responses, including responses with an error such as a 404. However, as far as I know, you cannot count the number of errors (but that shouldn't matter).
The only issue here is that if you create a new page and earlier it was cached as a 404, then it won't work. At least, not immediately (depending on how long your cache takes to timeout).
That will definitely help greatly, though. All the hits that would otherwise go to your backend and generate a 404 will be stopped at the Nginx instance and return the exact same 404 error over and over again.
Another way, is to define a location and run a command (which could then run ipset
to add the IP address). So something like:
location /phpmyadmin.php {
content_by_lua_block {
os.execute("/usr/bin/block-ip.sh")
}
}
I'm not too sure how you could handle many paths, however.
My own experience with fail2ban
is that it's rather slow and "backward" (not proactive). If you really want to completely block an IP address, though, the easiest is to have your app. send a message to the front end where you can run iptables
to block the IP. For that you need to forward the original IP which Nginx doesn't do by default, but it's easy to add an X-Forwarded-For
header that your app. can then send back to the small tool you're using to add IPs to your iptables
. Note also you should not directly add it to iptables
. Instead you should use a list. For that look at the ipset
.
If you can't change your apps for such and want to use fail2ban
, you'd have to check the Nginx logs and detect those paths you do not like.