If you run a self-hosted WordPress site, a fail2ban WordPress setup is the cheapest security upgrade you will ever install. Every public WordPress site gets a steady stream of password-guessing requests against wp-login.php and xmlrpc.php — and every one of those requests is an uncached PHP request that occupies a PHP-FPM worker, fills your logs, and sooner or later guesses a weak password.
Fail2ban fixes this at the server, before the requests ever reach PHP. It reads your Nginx access log, counts failed login attempts per IP address, and bans the offenders at the firewall for a while. This guide sets it up properly on Ubuntu 24.04 with Nginx: the install, the WordPress filter, the jail, how to test it before you trust it, and the Cloudflare gotcha that breaks most setups. Every command was verified against current 2026 sources on 16 November 2026.
Why your WordPress login page is under constant attack
WordPress powers an enormous share of the web, and its login URL is the same on every site: /wp-login.php. Bots do not need to find your site — they scan the internet and try the same URL everywhere. A failed login is cheap for the bot and expensive for you: WordPress has to load, check the password against the database, and render the login form again.
That is the part most guides skip. Brute-force protection is not only about stopping a break-in. It is about stopping thousands of wasted PHP requests a day from chewing through your workers and your logs. A server-side ban removes the noise at the firewall, so the requests never reach WordPress at all.
Two WordPress quirks make fail2ban a good fit here:
- A failed login on
wp-login.phpre-displays the form with HTTP status 200, while a successful login answers with a 302 redirect. So “POST to wp-login.php answered with 200” is a reliable signal for a failed attempt. xmlrpc.phpalways answers 200, and one request to it can try many passwords at once — so there you simply count POST requests.
How fail2ban works (in one minute)
Fail2ban has three moving parts:
- A filter is a set of regular expressions that match “bad” log lines and extract the IP address. Filters live in
/etc/fail2ban/filter.d/. - A jail connects a filter to a log file and sets three numbers: how many matches (
maxretry) within what window (findtime) trigger a ban, and for how long (bantime). - An action performs the ban — normally a firewall rule, through nftables, iptables, or UFW.
WordPress ships no built-in fail2ban filter, so you write one. That is the whole job, and it is about twenty lines of config.
What you need before you start
- An Ubuntu 24.04 VPS with sudo access. Ubuntu 24.04 ships fail2ban 1.0.2; the latest upstream release is 1.1.1, released 15 August 2026. The packaged version is fine for this guide — Ubuntu does not backport new fail2ban versions to LTS releases, so 24.04 stays on 1.0.2 with security fixes only.
- Nginx serving your WordPress site, with access logs at
/var/log/nginx/access.login the defaultcombinedformat. (Apache users: the same filter works — see step 7.) - Your own IP address, so you can whitelist it and never ban yourself.
Step 1 — Install fail2ban and confirm the SSH jail works
Install fail2ban and nftables (the firewall backend Ubuntu 24.04 uses by default):
sudo apt update
sudo apt install -y fail2ban nftables
sudo systemctl enable --now fail2ban
On Ubuntu 24.04, a stock install already protects SSH out of the box. The package drops in /etc/fail2ban/jail.d/defaults-debian.conf, which enables the sshd jail with backend = systemd (it reads the systemd journal) and banaction = nftables. Confirm it is running:
fail2ban-client --version
sudo fail2ban-client status
sudo fail2ban-client status sshd
Note the version gotcha: the original 24.04 package (1.0.2-3) crashed on start because Python 3.12 dropped the asynchat module. The fixed release, 1.0.2-3ubuntu0.1, comes from the Ubuntu updates repository — which is why apt update comes first. If fail2ban-client status errors, update and reinstall before doing anything else.
Step 2 — Write jail.local (and never touch jail.conf)
Fail2ban reads its jail configuration in a fixed order, and a value read later wins: jail.conf, then jail.d/*.conf, then jail.local, then jail.d/*.local. Never edit jail.conf — package upgrades overwrite it. Your settings go in /etc/fail2ban/jail.local, which upgrades never touch.
Create it with your own IP whitelisted first, so a typo can never lock you out:
sudo nano /etc/fail2ban/jail.local
[DEFAULT]
# Never ban these. Put your own fixed IP or range here FIRST.
ignoreip = 127.0.0.1/8 ::1 203.0.113.10
bantime = 1h
findtime = 10m
maxretry = 5
# Ubuntu 24.04 already sets these in jail.d/defaults-debian.conf.
# Repeat them here so the file is correct on any release.
backend = systemd
banaction = nftables
banaction_allports = nftables[type=allports]
There is one line in here that people miss on Ubuntu 24.04, and it is the reason most “fail2ban never bans” reports exist: backend = systemd is the default, so a jail that reads a log file — like the WordPress jail below — inherits the journal backend, starts without a warning, and never bans anything. The fix is in step 4: log-file jails get backend = auto in their own section.
If you would rather see bans in sudo ufw status, set banaction = ufw instead. The ufw action adds each ban as a UFW rule, which blocks the address on every port. With the nftables default, bans live in fail2ban’s own f2b-table, which is checked before UFW’s rules — a banned IP is rejected even if UFW allows the port, but ufw status will not list it. Check nftables bans with sudo nft list table inet f2b-table.
Step 3 — Write the WordPress filter
Create the filter file. Its name becomes the filter name you reference in the jail:
sudo nano /etc/fail2ban/filter.d/wordpress-login.conf
[Definition]
failregex = ^<HOST> -[^"]*"POST /wp-login\.php[^"]*" 200
^<HOST> -[^"]*"POST /xmlrpc\.php[^"]*" 200
ignoreregex =
Two things to notice:
- The
200at the end of the wp-login line is the whole trick — it counts only failed logins, because a successful login redirects with 302 and never matches. - The xmlrpc line counts every POST, because xmlrpc always answers 200 whether the password was right or not. If your site genuinely uses XML-RPC (the WordPress mobile app, Jetpack, or a remote publishing tool), remove
|xmlrpc\.phpfrom the filter or you will ban your own app. If nothing uses it, the safer move is to blockxmlrpc.phpin Nginx entirely — see the mistakes section.
The regex assumes Nginx’s default combined log format, where each line starts with the client address. If you customised your log_format, adjust the pattern to match your lines — and test it, which is step 5.
Step 4 — Add the WordPress jail
Append the jail to the same jail.local:
[wordpress-login]
enabled = true
port = http,https
filter = wordpress-login
logpath = /var/log/nginx/access.log
backend = auto
maxretry = 5
findtime = 10m
bantime = 1h
This bans an IP for one hour after 5 failed login attempts within 10 minutes. The backend = auto line is the one from step 2 — without it, this jail silently reads the journal instead of your Nginx log and never fires.
If each site has its own access log, use a pattern such as logpath = /var/log/nginx/*access.log. And while you are here, add the recidive jail, which ships in the stock config and bans repeat offenders for much longer — an IP banned five times in a day gets a week-long ban:
[recidive]
enabled = true
logpath = /var/log/fail2ban.log
banaction = %(banaction_allports)s
bantime = 1w
findtime = 1d
maxretry = 5
Now validate the config and restart:
sudo fail2ban-client -t
sudo systemctl restart fail2ban
sudo fail2ban-client status wordpress-login
fail2ban-client -t catches syntax errors before they take the service down. If the status output shows the jail with zero banned IPs, it is running — zero is expected until a bot trips it.
Step 5 — Test the filter before you trust it
A filter that matches nothing gives you a false sense of security. A filter that matches too much bans real visitors. Test both with fail2ban-regex, which runs a filter against a log without banning anyone:
# Against your real access log
sudo fail2ban-regex /var/log/nginx/access.log /etc/fail2ban/filter.d/wordpress-login.conf
# Against a single synthetic line, to see exactly what matches
sudo fail2ban-regex '203.0.113.44 - - [16/Nov/2026:10:15:02 +0000] "POST /wp-login.php HTTP/1.1" 200 4521 "https://example.com/wp-login.php" "Mozilla/5.0"' /etc/fail2ban/filter.d/wordpress-login.conf --print-all-matched
Look at the “Matched” count. If your real log shows matches, the filter works. If it shows zero but you know bots hit your login page, check the log format first — a custom log_format is the usual culprit.
Then prove the whole chain end to end. From a machine you do not care about (a phone on mobile data, never your own whitelisted IP), submit five wrong passwords to your login page, then check:
sudo fail2ban-client status wordpress-login
sudo tail -f /var/log/fail2ban.log
You should see the test IP under “Banned IP list” and a Ban 203.0.113.44 line in the log. Unban it when you are done:
sudo fail2ban-client set wordpress-login unbanip 203.0.113.44
On fail2ban 1.x you can also list every ban across all jails with sudo fail2ban-client banned.
Step 6 — Behind Cloudflare? Fix the real IP first
This is the gotcha that breaks most fail2ban WordPress setups. With Cloudflare’s orange cloud on, every request reaches Nginx from a Cloudflare edge IP — so your access log shows Cloudflare addresses, not visitor addresses. A jail that counts those either bans nothing useful or, worse, bans the proxy itself and locks out every visitor at once.
Fix it before you trust the jail. Cloudflare sends the real visitor address in a header called CF-Connecting-IP on every plan, free included. Tell Nginx to trust Cloudflare’s ranges and take the client address from that header, using the ngx_http_realip_module (bundled in Ubuntu’s Nginx package — confirm with nginx -V 2>&1 | grep -o with-http_realip_module):
sudo nano /etc/nginx/conf.d/cloudflare-realip.conf
# Trust Cloudflare edge IPs and restore the real visitor IP
# Full current ranges: https://www.cloudflare.com/ips/
set_real_ip_from 173.245.48.0/20;
set_real_ip_from 103.21.244.0/22;
set_real_ip_from 103.22.200.0/22;
set_real_ip_from 103.31.4.0/22;
set_real_ip_from 141.101.64.0/18;
set_real_ip_from 108.162.192.0/18;
set_real_ip_from 190.93.240.0/20;
set_real_ip_from 188.114.96.0/20;
set_real_ip_from 197.234.240.0/22;
set_real_ip_from 198.41.128.0/17;
set_real_ip_from 162.158.0.0/15;
set_real_ip_from 104.16.0.0/13;
set_real_ip_from 104.24.0.0/14;
set_real_ip_from 172.64.0.0/13;
set_real_ip_from 131.0.72.0/22;
set_real_ip_from 2400:cb00::/32;
set_real_ip_from 2606:4700::/32;
set_real_ip_from 2803:f800::/32;
set_real_ip_from 2405:b500::/32;
set_real_ip_from 2405:8100::/32;
set_real_ip_from 2a06:98c0::/29;
set_real_ip_from 2c0f:f248::/32;
real_ip_header CF-Connecting-IP;
real_ip_recursive on;
Then test and reload Nginx, and confirm your access log now shows real visitor IPs:
sudo nginx -t && sudo systemctl reload nginx
sudo tail /var/log/nginx/access.log
Two warnings. First, Cloudflare’s IP ranges change — keep the list in sync with the authoritative list at cloudflare.com/ips, or a request from a new edge IP will not be trusted and $remote_addr will revert to the Cloudflare address. Second, lock your origin so it only accepts connections from Cloudflare; if an attacker can reach the origin directly, they can send a forged CF-Connecting-IP header and you are back to trusting a header anyone can write.
Step 7 — The Apache version
On Apache, the filter is identical — only the log path changes. Point the jail at Apache’s access log:
[wordpress-login]
enabled = true
port = http,https
filter = wordpress-login
logpath = /var/log/apache2/access.log
backend = auto
maxretry = 5
findtime = 10m
bantime = 1h
Apache’s default combined format starts with the client address too, so the same regex matches. If you run Apache behind Cloudflare, use mod_remoteip with RemoteIPHeader CF-Connecting-IP and Cloudflare’s ranges in RemoteIPTrustedProxy — the same idea as the Nginx fix above.
What fail2ban will not do for you
Fail2ban stops repeated attempts from the same address. It does not stop a single determined attempt, it does not authenticate anyone, and it does not encrypt anything. Three things still matter more than any jail:
- Strong passwords and two-factor authentication. A bot that guesses the right password on the first try walks straight past fail2ban. Turn on 2FA for every admin account — that is the control that makes brute force pointless.
- Block xmlrpc.php if nothing uses it. Most sites do not need XML-RPC at all. A one-line Nginx location block that returns 403 for it removes the whole attack surface instead of merely rate-limiting it.
- Keep WordPress and its plugins updated. Brute force is the noisy attack; the quiet one is an unpatched plugin. Fail2ban does nothing about that.
Fail2ban also complements, rather than replaces, a login-limiting plugin. A plugin counts attempts inside WordPress and can show a lockout message; fail2ban counts them at the server and drops the traffic before PHP runs. Run both if you like — they do not conflict — but if you only pick one, pick the server-side one: it protects your workers as well as your passwords.
7 mistakes that break a fail2ban WordPress setup
- Forgetting
ignoreip. Test from a whitelisted IP and you will ban yourself mid-debug. Your own address goes inignoreipbefore anything else. - Editing
jail.conf. The next package upgrade overwrites it and your jail silently disappears. Overrides live injail.localorjail.d/*.localonly. - Missing
backend = autoon log-file jails. Ubuntu 24.04 defaults to the systemd journal backend, so a jail pointed at/var/log/nginx/access.logstarts cleanly and bans nothing. This is the single most common failure. - Counting 302s as failures. A filter without the
200status match bans people who log in successfully — including you, five logins in ten minutes. - Banning Cloudflare’s IPs. Behind a CDN, the log shows proxy addresses. Restore the real IP first (step 6) or your first ban takes the whole site down for everyone.
- Setting
bantimetoo long on a shared network. A day-long ban on a shared office or university NAT address locks out every legitimate user behind it. Start at one hour; letrecidivehandle the repeat offenders. - Never testing the filter.
fail2ban-regextakes ten seconds. A filter that matches nothing is worse than no filter — it is a false sense of security.
The 5-minute verification checklist
Run these after you finish. They catch the mistakes that lead to lockouts and silent non-protection:
- Config valid:
sudo fail2ban-client -tprints no errors. - Jail running:
sudo fail2ban-client status wordpress-loginshows the jail with your log path. - Filter matches:
fail2ban-regexagainst your real log returns a non-zero match count. - Real IPs in the log:
tail /var/log/nginx/access.logshows visitor addresses, not Cloudflare ranges. - End-to-end ban works: five wrong passwords from a test IP land it in the banned list, and
unbanipclears it. - Your IP is whitelisted: your address is in
ignoreipinjail.local.
That is the whole setup: about half an hour, one filter file, one jail, and a test. Your login page stops being the cheapest target on your server — and your PHP workers stop spending their day answering bots.
Further Reading & References
- Install fail2ban on Ubuntu 24.04, 22.04 and 26.04 (with UFW) — fail2ban.dev, checked against fail2ban 1.1.1, October 2026. The per-release version table and the nftables-vs-UFW explanation this guide follows.
- Fail2ban releases, versions & end-of-life dates — versio.io, updated 16 November 2026. Latest version 1.1.1, released 15 August 2026.
- Fail2ban for WordPress on Nginx: wp-login & xmlrpc (2026) — Saizul Amin. The filter this guide uses, plus the Cloudflare real-IP trap and the 200-vs-302 signal explained in detail.
- Stop Brute-Force Attacks with fail2ban — SSH, Nginx and WordPress Jails — Liga Hosting. A complete jail.local with SSH, Nginx and WordPress jails, and the fail2ban-regex testing workflow.
- Restoring original visitor IPs — Cloudflare Docs. The official guide for logging the real visitor IP at your origin, including the Nginx real_ip configuration.
- Install Fail2ban on Ubuntu 24.04: stop SSH bots — SSD Nodes. The Ubuntu 24.04 defaults — 1.0.2, nftables, systemd backend — and why the packaged defaults already protect SSH.
- Fail2ban Guide 2026: Protect Your VPS from SSH Brute Force Attacks — CloudPicked. Includes a WordPress login protection section and the 1.1.x version facts.
- How to install and configure Fail2ban in 2026 — OuiHeberg. Debian 12/13 and Ubuntu 22.04/24.04 install, jail.local layout, and the Nginx jail setup.
- WordPress Security Headers: Add Them Without Breaking Your Site (2026) — HowToShipIt. The other half of this hardening pair: headers that stop clickjacking, MIME sniffing, and script injection.
- How to Set Up Automated VPS Backups: A Complete Guide (2026) — HowToShipIt. Because hardening reduces the odds of a breach — backups are what save you when one still happens.
Verified 16 November 2026 against the fail2ban project, Ubuntu 24.04 package defaults, and the Cloudflare documentation linked above.



