Set up Fail2Ban for Nginx on a VPS

Any VPS exposed to the internet is constantly scanned and probed by bots, especially sites running on Nginx. Fail2Ban blocks these IPs automatically. This guide configures Fail2Ban for Nginx on Ubuntu step by step.

What is Fail2Ban

Fail2Ban is a security tool that watches the logs of services like SSH, Nginx and mail. When it sees an IP repeatedly misbehaving — many failed logins, or an abnormally high request rate — it tells the firewall (iptables/nftables) to temporarily ban that IP. This stops brute-force attacks on its own, without you watching the screen.

Step 1 — Install Fail2Ban

sudo apt update sudo apt install fail2ban -y sudo systemctl enable --now fail2ban

Step 2 — Create jail.local

Don't edit jail.conf directly. Create jail.local instead so your settings aren't overwritten on update:

[DEFAULT] bantime = 1h findtime = 10m maxretry = 5 ignoreip = 127.0.0.1/8 ::1 YOUR_TRUSTED_IP

Step 3 — Enable jails for Nginx

Add Nginx jails in the same file to cover HTTP auth guessing and bots scanning for files:

[nginx-http-auth] enabled = true port = http,https logpath = /var/log/nginx/error.log [nginx-botsearch] enabled = true port = http,https logpath = /var/log/nginx/access.log maxretry = 2

Step 4 — Restart and check

sudo systemctl restart fail2ban sudo fail2ban-client status nginx-http-auth

This shows how many IPs are banned and which logs are being watched.

Tip: Always add your office or home IP to ignoreip so you don't lock yourself out after a mistyped password.

Unbanning an IP

To release an IP that was banned by mistake, run:

sudo fail2ban-client set nginx-http-auth unbanip 1.2.3.4

Adding a Rate-Limit Jail (nginx-req-limit)

Beyond the built-in jails you can defend against request floods that trigger Nginx's own limit_req directive. Create a custom filter at /etc/fail2ban/filter.d/nginx-req-limit.conf:

[Definition] failregex = limiting requests, excess:.* by zone .*, client: <HOST> ignoreregex =

Then add the jail to jail.local:

[nginx-req-limit] enabled = true port = http,https filter = nginx-req-limit logpath = /var/log/nginx/error.log maxretry = 10 bantime = 30m

This jail blocks any IP that exceeds the Nginx rate limit more than 10 times within the findtime window — providing an additional layer of protection against application-layer flood attacks.

Monitoring Fail2Ban in Real Time

Regular monitoring lets you confirm that Fail2Ban is working correctly and see which IPs are currently banned.

List all active jails

sudo fail2ban-client status

Inspect a specific jail

sudo fail2ban-client status nginx-http-auth sudo fail2ban-client status nginx-botsearch

Stream the log live

sudo tail -f /var/log/fail2ban.log

The log shows every Ban and Unban event with the matching jail and IP address, making it easy to analyse which attack patterns are most common.

Pro tip: Run sudo iptables -L f2b-nginx-http-auth -n --line-numbers to query the iptables chain directly and see currently blocked IPs without waiting for a log update.

Incremental Ban Times

Fail2Ban supports incremental ban times that grow longer each time the same IP is banned again, discouraging repeat attackers.

[DEFAULT] bantime.increment = true bantime.factor = 1 bantime.formula = ban * (1 << (ban - 1)) * banFactor bantime.maxtime = 24h bantime.overalljails = true
Ban # Duration Notes
1 1 hour Base bantime value
2 2 hours Increment kicks in
3 4 hours Exponential growth
4+ Up to 24 hours Capped by bantime.maxtime

Email Alerts on New Bans

Fail2Ban can send an email alert whenever a new IP is banned, using the built-in sendmail-whois action. Add these settings to the DEFAULT section of jail.local:

[DEFAULT] destemail = [email protected] sender = [email protected] mta = sendmail action = %(action_mwl)s

The action_mwl (mail with log) action sends an email with the relevant log lines attached, so you can immediately trace the source of an attack. If sendmail is not installed, use mailutils as a replacement:

sudo apt install mailutils -y

VPS Security Checklist

Fail2Ban is one important layer of VPS security, but a well-hardened server needs multiple defences working together. Work through this checklist after setting up Fail2Ban.

Security is an ongoing process. Revisit your configuration at least quarterly and follow CVE announcements for software running on your VPS to stay ahead of newly discovered vulnerabilities.

Frequently asked questions

What is Fail2Ban?

Fail2Ban is a Linux security tool that reads log files, detects IPs making repeated failed logins or attacks, and automatically tells the firewall to temporarily block them. It greatly reduces brute-force and bot attacks.

How is Fail2Ban different from a UFW firewall?

UFW is a firewall with static rules that open or close ports. Fail2Ban works dynamically: it watches logs and bans suspicious IPs for a period of time. The two complement each other and are often used together.

What do I do if I get blocked myself?

Run fail2ban-client set unbanip to unban your own IP, and add trusted IPs to ignoreip so they are never banned again.

VPS with full root to harden security yourself

AsiaGB VPS gives you full root access to install Fail2Ban and tune freely, starting at ฿500/month.

View VPS Plans