
Brute-forcing wp-login.php looks harmless at first. Then it starts chewing CPU, bloating logs, and triggering PHP-FPM slow requests.
This walkthrough shows you how to set up Fail2ban for WordPress on an Nginx-based VPS. The jail targets real login abuse, works behind proxies/CDNs, and helps avoid locking out editors.
You’ll put two things in place:
- Nginx logging that clearly captures WordPress login traffic (including XML-RPC and any login-style endpoints you actually use).
- Fail2ban filters + jails that ban the correct IP, plus a repeatable way to test and verify bans.
This guide targets Ubuntu 24.04/26.04 LTS and Debian 12/13. AlmaLinux/Rocky notes are included where paths differ. You’ll need root or sudo.
Prerequisites and a quick reality check
Before you change anything, confirm Fail2ban is the right tool for the job. If you already run behind Cloudflare, a WAF rule can stop many drive-by scans.
Fail2ban still earns its keep at the origin. It blocks abusive IPs before they reach PHP-FPM. It also cuts log noise.
- Nginx serving WordPress (PHP-FPM).
- Access logs enabled for the WordPress vhost.
- A firewall in place (UFW or firewalld). Fail2ban can work with either.
Fail2ban works best on a VPS. You control both the firewall and the logs.
If you’d rather not own the OS layer, managed VPS hosting from HostMyCode is a simpler path for hardening and ongoing tuning.
Step 1: Confirm Nginx logs include wp-login.php (and the right client IP)
The most common WordPress + Fail2ban failure is banning your proxy/CDN instead of the attacker. Fix client IP handling first. Then build the jail.
1A) Verify your access log has login requests
On your server, run:
sudo tail -n 200 /var/log/nginx/access.log | grep -E "wp-login\.php|xmlrpc\.php"
If you use per-site logs, check your vhost in /etc/nginx/sites-enabled/ for an access_log directive. Then tail that file instead.
You’re looking for lines like:
POST /wp-login.php HTTP/2.0" 200
POST /xmlrpc.php HTTP/2.0" 403
1B) If you use Cloudflare / a reverse proxy, restore the real client IP
If the same IP shows up on every request (often your CDN or load balancer), Fail2ban will ban the wrong host. Fix Nginx real IP handling before you do anything else.
For Cloudflare on Nginx: edit /etc/nginx/nginx.conf (or a file in /etc/nginx/conf.d/) and add:
# Trust Cloudflare (IPv4)
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;
# Trust Cloudflare (IPv6)
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 reload Nginx:
sudo nginx -t && sudo systemctl reload nginx
Verify: load any page on your site once. Then check the newest access log entry. The IP should match your real client IP, not Cloudflare.
If you aren’t on Cloudflare but you do have a load balancer or reverse proxy, use its header (commonly X-Forwarded-For). Trust only the proxy IP ranges. Don’t trust the whole internet.
Related reading: if bans behave oddly or you suspect parsing issues, this general Nginx jail guide walks through the basics before you add WordPress-specific rules: Set Up Fail2ban for Nginx on Ubuntu VPS.
Step 2: Install Fail2ban and enable log backend
On Ubuntu/Debian:
sudo apt update
sudo apt install -y fail2ban
On AlmaLinux/Rocky (EPEL required on some minimal installs):
sudo dnf install -y epel-release
sudo dnf install -y fail2ban
Start and enable:
sudo systemctl enable --now fail2ban
sudo fail2ban-client ping
You should get pong.
2A) Create a safe local override file
Don’t edit /etc/fail2ban/jail.conf directly. Put your changes in jail.d so updates don’t wipe them.
sudo mkdir -p /etc/fail2ban/jail.d
Also check that Fail2ban is set to follow log files cleanly:
sudo fail2ban-client get logtarget
sudo fail2ban-client get loglevel
The defaults work for most servers. If you later need more detail, set loglevel = INFO in /etc/fail2ban/fail2ban.local.
Step 3: Make Nginx log WordPress login failures in a way Fail2ban can match
WordPress “failed login” responses aren’t consistent. Depending on plugins and auth flows, you can still get a 200 on failure.
That makes “ban on 401/403” unreliable.
Instead, log the login endpoints to a dedicated file. Then ban by request rate. This better matches what brute-force tools actually do.
Add this to your site’s server block (your WordPress vhost in /etc/nginx/sites-available/example.com):
# Log only WordPress auth endpoints to a dedicated file
location = /wp-login.php {
access_log /var/log/nginx/wp-login-access.log;
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
}
location = /xmlrpc.php {
access_log /var/log/nginx/wp-login-access.log;
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
}
Update the PHP-FPM socket to match your stack. Ubuntu 24.04 commonly uses PHP 8.3, but some servers run 8.2/8.4.
If you don’t use snippets/fastcgi-php.conf, keep your existing FastCGI settings. Only add the access_log line.
Create the log file and set safe permissions:
sudo touch /var/log/nginx/wp-login-access.log
sudo chown www-data:adm /var/log/nginx/wp-login-access.log
sudo chmod 0640 /var/log/nginx/wp-login-access.log
Reload Nginx:
sudo nginx -t && sudo systemctl reload nginx
Verify: open /wp-login.php in your browser, then:
sudo tail -n 5 /var/log/nginx/wp-login-access.log
You should see your request.
Step 4: Create a Fail2ban filter for WordPress login abuse
Next, match repeated hits to /wp-login.php and /xmlrpc.php, regardless of HTTP status.
Real users don’t hammer login endpoints 30 times in two minutes. Bots do.
Create a filter file:
sudo tee /etc/fail2ban/filter.d/nginx-wordpress-login.conf >/dev/null <<'EOF'
[Definition]
# Matches requests to wp-login.php or xmlrpc.php in the dedicated log.
# Works with standard Nginx combined log format.
failregex = ^<HOST> - .* "(GET|POST) /(wp-login\.php|xmlrpc\.php)\b.*" .*\s*$
ignoreregex =
EOF
Test the filter against your log:
sudo fail2ban-regex /var/log/nginx/wp-login-access.log /etc/fail2ban/filter.d/nginx-wordpress-login.conf
After you’ve visited the login page once, “Matches” should be greater than zero.
Step 5: Create the jail (with sane defaults and a lockout-resistant allowlist)
Now connect the filter to a jail. The defaults below are a solid starting point.
They catch brute force, but they still tolerate normal usage.
Create /etc/fail2ban/jail.d/wordpress-login.conf:
sudo tee /etc/fail2ban/jail.d/wordpress-login.conf >/dev/null <<'EOF'
[nginx-wordpress-login]
enabled = true
filter = nginx-wordpress-login
logpath = /var/log/nginx/wp-login-access.log
# Ban behavior
findtime = 10m
maxretry = 15
bantime = 2h
# Firewall action (Ubuntu/Debian typically uses nftables or iptables underneath)
# action defaults are fine in most installs; override only if you know your stack.
# Safety: allow your office IPs, monitoring, and admin VPN ranges.
# Add your own IP(s) here. Keep 127.0.0.1.
ignoreip = 127.0.0.1/8 ::1
EOF
Important: Add your admin IPs or VPN ranges to ignoreip now. Don’t wait until after you get locked out.
For example:
ignoreip = 127.0.0.1/8 ::1 203.0.113.10 198.51.100.0/24
Restart Fail2ban:
sudo systemctl restart fail2ban
Verify the jail is loaded:
sudo fail2ban-client status
sudo fail2ban-client status nginx-wordpress-login
You should see the jail name and a Currently banned count (usually 0 at first).
Step 6: Test bans without accidentally banning yourself
Run your test from an IP you can afford to lose. Your phone on mobile data is usually the easiest option.
6A) Generate repeated requests
From the test client (not the server), run:
for i in $(seq 1 20); do curl -s -o /dev/null -X POST https://example.com/wp-login.php; done
Wait 10–30 seconds, then check the jail on the server:
sudo fail2ban-client status nginx-wordpress-login
If the test IP is banned, it will appear under Banned IP list.
6B) Confirm the ban is active at the firewall level
On Ubuntu with an nftables backend, you can often confirm by listing rules. The exact command varies by distro and Fail2ban action.
The most reliable check is practical. From the banned client, try loading the login page again. It should fail immediately or time out.
If you use UFW and want Fail2ban to write UFW rules explicitly, switch the action to ufw. On most current installs, the default action is fine.
6C) Unban yourself (if you got it wrong)
To unban a specific IP:
sudo fail2ban-client set nginx-wordpress-login unbanip 203.0.113.50
If you also managed to lock yourself out of SSH, use your hosting console/VNC or provider rescue mode. This is where managed hosting can save time during security changes.
Step 7: Reduce false positives (real users, password managers, and SSO)
Fail2ban only knows “frequency,” not intent. Legitimate users can still trigger bans if:
- A password manager retries aggressively.
- An SSO plugin polls authentication endpoints.
- A proxy/CDN still hides real client IPs.
Three practical adjustments usually fix it:
Option A: Increase maxretry but shorten bantime
If your team logs in often, try:
maxretry = 25
bantime = 30m
Option B: Ignore known corporate/VPN ranges
Add only the ranges you actually control to ignoreip. Keep it tight.
Don’t exempt huge networks just to avoid a few bans.
Option C: Separate jail for xmlrpc.php
If you don’t use XML-RPC, blocking it at Nginx is cleaner than banning endless scanners.
Add to your WordPress server block:
location = /xmlrpc.php {
return 403;
}
Reload Nginx, and remove xmlrpc.php from the Fail2ban filter if you want to keep the signal focused.
Step 8: Add a second jail for “wp-admin with auth failures” (optional, useful)
Some bots skip wp-login.php and hammer /wp-admin/ directly. WordPress usually redirects unauthenticated requests back into the login flow.
You may still want to suppress the extra noise.
Create an additional filter:
sudo tee /etc/fail2ban/filter.d/nginx-wordpress-admin.conf >/dev/null <<'EOF'
[Definition]
# Match wp-admin hits. Most unauth requests will be 302 to wp-login.php.
failregex = ^<HOST> - .* "(GET|POST) /wp-admin/\b.*" (301|302|403)\s.*$
ignoreregex =
EOF
And a jail (reusing the same log file):
sudo tee /etc/fail2ban/jail.d/wordpress-admin.conf >/dev/null <<'EOF'
[nginx-wordpress-admin]
enabled = true
filter = nginx-wordpress-admin
logpath = /var/log/nginx/wp-login-access.log
findtime = 10m
maxretry = 30
bantime = 1h
ignoreip = 127.0.0.1/8 ::1
EOF
Restart and verify:
sudo systemctl restart fail2ban
sudo fail2ban-client status nginx-wordpress-admin
Step 9: Log rotation so your dedicated WordPress auth log doesn’t grow forever
Nginx’s default logrotate setup may not include your custom file. Add a dedicated rule:
sudo tee /etc/logrotate.d/nginx-wp-login >/dev/null <<'EOF'
/var/log/nginx/wp-login-access.log {
daily
rotate 14
missingok
notifempty
compress
delaycompress
sharedscripts
postrotate
[ -s /run/nginx.pid ] && kill -USR1 `cat /run/nginx.pid`
endscript
}
EOF
Test:
sudo logrotate -d /etc/logrotate.d/nginx-wp-login
Step 10: Quick diagnostics when bans don’t trigger
- No matches in fail2ban-regex: Your log format differs, or you’re reading the wrong file. Run
tail -n 20on the log and adjust the regex to match what you actually see. - Bans hit your proxy IP: Real IP headers aren’t configured, or you trusted the wrong sources. Fix Nginx
real_ipfirst. - Jail shows 0 bans but you see brute force: The attack may be targeting
/wp-json/auth endpoints (plugin dependent). Log that endpoint to its own file, then create another filter. - You get banned as an admin: Add your IP to
ignoreip, then reduce sensitivity. Also check whether your office uses rotating NAT IPs.
To inspect Fail2ban’s own logs:
sudo journalctl -u fail2ban --since "30 min ago" --no-pager
Step 11: Tie-in hardening that actually reduces login pressure
Fail2ban is reactive. You’ll get better results if you also remove easy attack paths.
- Disable XML-RPC if you don’t use it (shown above).
- Change the default admin username (never use “admin”).
- Enforce MFA for administrator accounts.
- Cache properly so login scans don’t drag the whole site down. Redis object caching helps sites with heavy admin usage; HostMyCode has a practical Redis setup: Set Up Redis for WordPress With Nginx and PHP-FPM.
If you’re also dealing with “too many redirects” or maintenance-mode lockups during updates, these guides can help:
Summary: what you built and how to keep it stable
You added a dedicated Nginx access log for WordPress authentication endpoints. Then you configured Fail2ban to ban IPs that hit those endpoints too aggressively.
You validated the regex, confirmed bans apply, and set log rotation. That keeps the setup predictable over time.
If you want this kind of server-side hardening without staying on top of OS updates and firewall changes, run WordPress on a HostMyCode VPS or hand it off to managed VPS hosting so the tuning keeps pace as traffic changes.
Brute-force traffic becomes a hosting issue long before it turns into an app issue. If you want predictable performance during login attacks, HostMyCode can run WordPress on a VPS with full root control. Or choose managed VPS hosting and have our team handle security tuning, updates, and monitoring.
FAQ
Will this block legitimate users who forget their password?
Not if you keep maxretry reasonable (15–25 in 10 minutes) and add known admin ranges to ignoreip. Most people won’t submit 15 rapid-fire attempts.
Should I ban on 401/403 only?
No. WordPress login failures often return 200, so status-only rules miss plenty of abuse. Logging the endpoints to a dedicated file and banning on request rate is more reliable.
Do I still need this if I use Cloudflare?
Cloudflare helps, but origin-side bans reduce load and keep logs cleaner. Just make sure Nginx restores the real client IP so you don’t ban Cloudflare.
What’s a safe bantime for WordPress login scans?
Start with 1–2 hours. If the same IPs keep returning, move to 12 hours. If you have lots of shared-IP corporate users, keep bans shorter and rely on rate limiting plus MFA.
Can I do this on a shared hosting plan?
Usually not. Fail2ban needs OS access. On shared hosting, rely on a WordPress security plugin, WAF rules, and MFA. If you want OS-level control, use a VPS.