How to Rate Limit Nginx to Stop Brute Force and Scraping Bots
To rate limit Nginx, define a shared memory zone that tracks requests per address, then apply that zone where abuse happens: login pages, search, and API endpoints. Done right, rate limiting slows brute force logins and scraping bots to a crawl while real visitors never notice it. Done wrong, it blocks real users behind shared IPs or breaks pages that load many assets at once. This guide shows the settings, the tuning, and the tests that keep it on the right side of that line.
Rate limiting is not a login security replacement. It buys time and raises the cost of an attack, but a weak password still falls to a slow attack given enough days. Pair it with strong passwords, key-only SSH, and Fail2ban, which our Fail2ban guide for SSH and Nginx covers. Rate limiting handles the volume; those tools handle the access itself.
Nginx Limit Req Zone Setup in the Http Block
Zones live in the http context, usually in /etc/nginx/nginx.conf or a conf file included from it. The nginx limit_req_zone directive below defines the zone before any location uses it. A basic zone looks like this:
limit_req_zone $binary_remote_addr zone=login:10m rate=5r/m;
The key, $binary_remote_addr, tracks each client IP in binary form, which uses less memory than the text form. The zone name, login, is how you refer to it later. The size, 10m, holds roughly 160,000 addresses, which is plenty for a small server. The rate, 5r/m, allows five requests per minute per address. That rate suits a login page, where a real user tries once or twice, but it would break a normal page that loads twenty assets, which is why you apply different zones to different locations.
Apply the zone inside a location block:
location = /wp-login.php { limit_req zone=login burst=3 nodelay; }
The burst value allows short spikes above the rate, and nodelay serves those burst requests at once instead of queuing them. Without a burst, a login page that also loads a stylesheet and a script from the same location could trip the limit on its first load. With a burst of 3, a normal login passes and a brute force script that tries fifty passwords in a minute hits the cap after the first few.
Brute Force Protection Nginx Login and API Limits
Protect each sensitive endpoint with its own zone and rate. Login pages get a slow rate, such as 5 per minute. Search gets a medium rate, such as 30 per minute, because real users search more than they log in. APIs get a rate that matches your documented use: a public API that a script polls every ten seconds needs a higher rate than a login form, and an API key check can replace IP-based limits entirely for known clients.
For WordPress, protect wp-login.php and xmlrpc.php together, or disable XML-RPC if you do not use it, as our guide to disabling XML-RPC explains. A bot that cannot reach the login endpoint through one path will try the other, so leaving one open while limiting the other only moves the attack. Check the access log after you apply limits and look for 503 responses, which are the sign that Nginx is enforcing the zone. A zone with no 503s in a week is either set too loose or pointed at an endpoint nobody attacks.
Scraping Bot Blocking with Connection and Request Limits
Scrapers differ from brute force bots: they read many pages fast instead of hammering one endpoint. A general zone for the whole site, such as 30 requests per second with a burst of 50, slows a scraper that fetches hundreds of pages a minute while leaving normal browsing alone. Add a connection limit too, with limit_conn_zone and limit_conn, to cap simultaneous connections per address:
limit_conn_zone $binary_remote_addr zone=addr:10m;limit_conn addr 10;
A browser opens a handful of connections at once, while a scraper opens dozens. The connection limit catches the second pattern even when the request rate looks normal. Combine both limits, and a scraper must slow down and open fewer connections, which is usually enough to make your site a poor target compared with an unprotected one.
Also block the easy cases at the firewall and app layer. Our UFW guide covers the base rules, and a robots.txt file that disallows aggressive crawlers helps with polite bots, though it never stops a rude one. Rate limits are the enforcement behind the polite request.
Rate Limit Nginx Without Blocking Real Visitors
The risk with rate limiting is shared IPs. An office, a school, or a mobile carrier can put hundreds of real users behind one address, and a per-IP limit then blocks them all for one heavy user. Watch for this in the error log: many 503s from one address that also shows normal browsing patterns, such as varied user agents and full page loads, points at a shared IP rather than a bot. Options include raising the rate, whitelisting the address with a geo or map block that sets a different key, or moving the limit to a per-session key for logged-in users.
Test from a normal browser before you trust the config. Load the homepage, log in, search, and open several pages in quick succession, and confirm no 503 appears. Then test the limit itself with a small script that hits the login endpoint twenty times fast and confirm it gets 503s after the burst. A limit you have not tested both ways is a guess, and guesses in production turn into support tickets from blocked users.
Tune Limits from Logs and Handle 503 Responses
Tune from the access log, not from defaults. Count requests per IP per minute for your busiest endpoint with a log summary, and set the rate above the busiest real user but below the bot pattern. A rate that blocks the top one percent of real users is too tight. A rate that lets a bot make a thousand requests a minute is too loose. Re-check monthly, because traffic patterns change with new features and new marketing, and a limit tuned for last year can block this year normal use.
Decide what a limited client sees. The default 503 response is fine for bots, but a custom error page that explains the limit helps a real user who trips it by accident. Set it with limit_req_status 429; to return the more accurate 429 code, and log limited requests so you can tell a real block from a server error. When a limit starts blocking real traffic, raise the rate first and investigate second, because an outage for real users costs more than a few extra bot requests while you tune.