Nginx Rate Limiting for WordPress Login and REST API Endpoints

Nginx rate limiting for the WordPress login, XML-RPC and the REST API

A brute-force attack on wp-login.php is mainly a load problem: every attempt starts PHP, loads WordPress and checks a password hash, which is deliberately slow. A security plugin blocks the attacker only after all that work is done. Nginx can limit the request rate per IP before PHP is involved, at almost no cost.

Problem: During a credential stuffing attack the access log shows thousands of POST requests per minute to wp-login.php and xmlrpc.php. PHP-FPM runs out of workers, and the shop is slow for real customers.

Solution: Define limit_req_zone zones per IP for the login, for XML-RPC and for the REST API, apply them with limit_req and a small burst in exact location blocks, answer with 429, exclude trusted IPs with a geo and map pair, and test the limits with a short loop of curl requests.

Zones in the http block

# /etc/nginx/conf.d/ratelimit.conf  (included in the http block)
geo $ha_trusted {
    default        0;
    127.0.0.1      1;
    203.0.113.0/24 1;   # office network, replace with your own
}

# An empty key means: not limited. Trusted IPs get an empty key.
map $ha_trusted $ha_limit_key {
    0 $binary_remote_addr;
    1 "";
}

limit_req_zone $ha_limit_key zone=ha_login:10m  rate=6r/m;
limit_req_zone $ha_limit_key zone=ha_xmlrpc:10m rate=2r/m;
limit_req_zone $ha_limit_key zone=ha_rest:10m   rate=10r/s;

limit_req_status 429;
limit_req_log_level warn;

limit_req has no if option; the way to exclude addresses is the key: requests with an empty key are not counted. $binary_remote_addr takes 4 bytes for IPv4 (16 for IPv6), so 10 MB of zone memory holds the state of about 160,000 addresses. The rate is the long-term average: 6r/m means one request every ten seconds.

Apply them in the server block

location = /wp-login.php {
    limit_req zone=ha_login burst=5 nodelay;
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php8.2-fpm.sock;
}

location = /xmlrpc.php {
    limit_req zone=ha_xmlrpc burst=2 nodelay;
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php8.2-fpm.sock;
}

location ^~ /wp-json/ {
    limit_req zone=ha_rest burst=40 nodelay;
    try_files $uri $uri/ /index.php?$args;
}

The exact locations must contain the PHP handling themselves: when Nginx picks location = /wp-login.php, the general location ~ \.php$ block is not used for that request. burst=5 nodelay lets a person mistype the password a few times without waiting, and everything above that gets 429 at once.

The REST limit applies only to pretty URLs under /wp-json/. WordPress also answers the REST API at /?rest_route=/..., which goes through index.php; whoever wants to limit that path too needs a map on $arg_rest_route. The block editor sends many REST requests in a short time, so keep the REST rate generous, or exclude logged-in editors by their IP range.

Test

sudo nginx -t && sudo systemctl reload nginx

for i in $(seq 1 10); do
  curl -s -o /dev/null -w "%{http_code}\n" -X POST https://example.com/wp-login.php
done
# 200 ... (burst), then 429

Rejected requests appear in the error log with the zone name (limiting requests, excess: ... by zone "ha_login"), which is also the quickest way to see whether real users hit the limit.

Behind a CDN or proxy

Behind Cloudflare or a load balancer, $binary_remote_addr is the address of the proxy, and all visitors share one limit. Restore the real client IP with the realip module first:

set_real_ip_from 173.245.48.0/20;   # one line per range of your CDN
real_ip_header CF-Connecting-IP;

NOTE: The Nginx limit works per IP. A distributed attack from thousands of addresses stays below any sensible per-IP rate, so keep the WordPress-side protection as well: rate limiting in the WordPress login counts failures per user name. If XML-RPC is not used at all (no Jetpack, no mobile app), block it completely instead of limiting it: protect wp-config.php and xmlrpc.php (with deny all; in the location = /xmlrpc.php block on Nginx).