Bad bots eat bandwidth, hammer your login pages, and scrape your content around the clock. On a small VPS, a single aggressive crawler can push CPU and memory high enough that real visitors get slow pages or errors. The fix does not need a paid firewall or a plugin. You can block bad bots with Nginx rate limiting using two built-in tools: limit_req for request rates and map blocks for user-agent filtering. This guide walks through a setup that works on a plain Ubuntu VPS with Nginx, and it keeps search engine crawlers and real visitors flowing while the junk traffic stops at the door.
What Bad Bots Do to a Small VPS
Most bot traffic is not a full DDoS attack. It is scrapers copying your articles, vulnerability scanners probing /wp-login.php and /xmlrpc.php, AI crawlers grabbing every page, and credential-stuffing scripts trying password lists against your login form. Each request is cheap on its own, but thousands of them per minute add up fast on a 1 or 2 GB VPS. PHP-FPM workers get tied up, the database slows down, and your monitoring starts showing spikes that look like real traffic growth but are not.
The first step is to see what is actually hitting your server. A quick look at the access log tells you more than any guess. Count requests by IP and by user agent, and the worst offenders usually stand out within minutes. If you need a refresher on log locations and formats, the guide to reading your Nginx access logs covers where each file lives and how to pull the useful lines out of it.
How Nginx Rate Limiting Works
Nginx rate limiting uses a shared memory zone to track how many requests each IP makes. The nginx limit_req_zone directive defines the zone, the key it tracks (usually the client IP), and the allowed rate. Then limit_req applies that zone inside a location or server block. Requests above the rate get a 503 response by default, or they wait in a short queue if you set a burst value.
Two settings shape how strict the limit feels. burst lets a client exceed the rate briefly, which matters because real browsers often fire several requests at once when loading a page. nodelay serves burst requests at once instead of spacing them out, so a normal page load still feels fast while a bot hammering hundreds of requests per second still hits the wall. For a deeper look at these settings, see the companion guide on rate limiting in Nginx.
How to Block Bad Bots with Nginx Rate Limiting Step by Step
Start with one general zone and one strict zone for login pages. Open your site config, usually in /etc/nginx/sites-available/your-site, and add the zones in the http context of /etc/nginx/nginx.conf:
limit_req_zone $binary_remote_addr zone=general:10m rate=10r/s; limit_req_zone $binary_remote_addr zone=login:10m rate=1r/s;
The general zone allows 10 requests per second per IP. That sounds low, but a human reading pages rarely passes it, while a scraper pulling dozens of URLs per second trips it at once. The login zone allows only 1 request per second, which is plenty for a person typing a password and far too slow for a brute-force script.
Now apply the zones in your server block:
server {
listen 443 ssl;
server_name example.com;
limit_req zone=general burst=20 nodelay;
location /wp-login.php {
limit_req zone=login burst=3 nodelay;
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
}
location / {
try_files $uri $uri/ /index.php?$args;
}
}
Always test before you reload. Run sudo nginx -t and only run sudo systemctl reload nginx when the test passes. A typo in the config takes the whole site down, so never skip the test. If you want a safe routine for this, the guide on testing a new Nginx config without taking your site down shows how to check changes before they go live.
Protect Login and API Endpoints Separately
One global limit is a start, but login pages, APIs, and search endpoints need their own rules. A scraper that respects the general limit can still hammer /wp-login.php at 10 requests per second, which is 600 password guesses a minute. The strict login zone above cuts that to 60, and fail2ban can cut it further by banning the IP after repeated 403 or 503 responses.
Apply the same idea to any endpoint that does real work: contact forms, password resets, comment submission, and JSON APIs. Static files, images, and cached pages can keep the looser general limit because Nginx serves them with almost no CPU. The guide on rate-limiting login and API endpoints goes further into per-location limits and the numbers that work for WordPress sites.
Block Bad Bot User Agents with a Map
Rate limiting slows bots down. User-agent filtering stops the known ones outright. A map block matches the User-Agent header against patterns and sets a flag you can act on:
map $http_user_agent $bad_bot {
default 0;
~*bytespider 1;
~*claudebot 1;
~*semrushbot 1;
~*mj12bot 1;
~*petalbot 1;
~*^$ 1;
}
server {
if ($bad_bot) {
return 403;
}
# rest of your config
}
Two warnings before you copy this. First, only block user agents you have actually seen misbehaving in your own logs. Lists copied from the internet go stale fast, and blocking a crawler you did not mean to block can hurt your search rankings or break a service you use. Second, the ~*^$ line blocks empty user agents, which is usually safe because real browsers always send one, but some monitoring tools and uptime checkers send none, so check your own traffic first. For the broader technique, including header checks and IP blocks, see the guide to blocking scrapers with Nginx rules.
Keep the Good Bots Coming
Not every bot is bad. Googlebot and Bingbot bring your pages into search results, and uptime monitors check that your site is alive. A rate limit of 10 requests per second rarely troubles them, since search crawlers pace themselves, but a strict user-agent block can shut them out by accident if your patterns are too broad.
Before you block a user agent, search your logs for it and ask what it does. If it belongs to a search engine you want, leave it alone. If you are unsure, leave it alone and let the rate limit handle it. User agents can be faked, so a bot that claims to be Googlebot is not proof it is one. The reliable check is a reverse DNS lookup on the IP, which should resolve to a googlebot.com or search.msn.com hostname. Do this check for any IP that claims to be a search crawler before you give it special treatment.
One more thing to check: if your VPS sits behind Cloudflare or another proxy, Nginx sees the proxy IP instead of the visitor IP, and rate limiting by $binary_remote_addr will throttle everyone at once. Set real_ip_header and set_real_ip_from for your proxy ranges first, then test with your own browser to confirm the limit applies per visitor, not per proxy.
Test the Setup and Watch the Logs
Test from your own machine with curl. Fire a burst of requests at a rate-limited page and watch for 503 responses:
for i in $(seq 1 30); do curl -s -o /dev/null -w "%{http_code}n" https://example.com/wp-login.php; done
The first few requests should return 200, then 503s should appear once the burst allowance runs out. That confirms the limit works. Then tail the access log and look for the pattern: the same IP, the same user agent, dozens of requests, all 503. That is a bot hitting the wall.
To make rate-limit hits easy to spot, add $limit_req_status to your log format in nginx.conf. It records PASSED, DELAYED, or REJECTED for each request, so you can count rejections without guessing from status codes. Check the logs daily for the first week. If real users complain about errors, raise the rate or the burst value. If bots keep coming, tighten the login zone or add their user agent to the map.
When Rate Limiting Is Not Enough
Nginx rate limiting handles most bot noise, but it has limits. It cannot stop a distributed attack spread across thousands of IPs, because each IP stays under the limit on its own. It cannot tell a polite scraper from a real user when both request at the same pace. And it does nothing against bots that rotate user agents and IPs.
When you reach that point, add a layer instead of tightening Nginx until real users suffer. Fail2ban watches your logs and bans repeat offenders at the firewall level, which is stronger than a 503 because the connection never reaches Nginx. A CDN with bot management, such as Cloudflare, can filter traffic before it touches your VPS at all. Both pair well with the setup in this guide: Nginx handles the steady background noise, and the extra layer handles the attacks that rate limiting alone cannot stop.