Rate limiting for bots: what it stops and what it misses

Intermediate4 min readUpdated

GET /learn/implementation/are-rate-limits-enough-to-stop-bots/ Accept text/html text/markdown

Short answer

Rate limits stop floods from a single source and simple scripts that send requests as fast as they can. They miss distributed attacks that spread requests across thousands of residential IP addresses and stay under any per-IP threshold. Key limits on more than the IP address, such as the account, session, device fingerprint, card BIN or endpoint, and pair them with bot detection.

A rate limit counts requests for a key over a time window and refuses the excess. The key decides what the limit can catch. Most default setups key on the client IP address, which is the identifier an attacker finds cheapest to change.

What does rate limiting stop?

Rate limits work well against traffic that is fast and concentrated:

  • Floods from one server or a small group of servers, including scrapers that fetch pages as fast as the network allows.
  • Simple scripts that retry a login or a coupon code in a tight loop.
  • Vulnerability scanners and hidden-file searches that send hundreds of requests a minute.
  • Your own bugs, such as a mobile app release that retries forever.

They are also cheap. A limit at the CDN or reverse proxy rejects traffic before your application or database does any work.

What does rate limiting miss?

Attackers who expect rate limits spread their traffic out. A credential stuffing run through residential proxies can send one or two login attempts from each of thousands of home IP addresses. Each address looks like a person who mistyped a password. No per-IP threshold low enough to catch that is safe for real users.

Other gaps:

  • Card testing often runs at a few attempts per minute, well inside normal checkout volume.
  • Scrapers pace themselves to stay under limits they have measured.
  • Shared addresses work against you. Carrier-grade NAT (many mobile users sharing one public IP), corporate proxies and university networks put hundreds of real people behind one address, so a strict per-IP limit blocks them first.
  • With IPv6, one user can control a whole /64 range of addresses. Limit on the /64 prefix if your tool supports it.

What should you rate limit on besides IP?

Pick keys that the attacker has to spend money or effort to rotate:

  • Failed logins per username, across all IPs, catch attacks on one account. For stuffing across many accounts, watch the site-wide failure rate, as described in how to detect credential stuffing.
  • A session or cookie counter works once sensitive actions require a valid session. A bot that drops cookies to reset its counter then fails the session check.
  • Device, browser and TLS fingerprints such as JA4 survive IP rotation. Thousands of IPs sharing one fingerprint is a strong sign of one operator.
  • The card BIN (the first six to eight digits, which identify the issuing bank) and a hash of the card number expose card testing. Many declines on one BIN in minutes is the classic pattern.
  • A global limit per endpoint, across all clients, works as a circuit breaker. Cap signups or OTP messages per minute for the whole site, so the attack stalls even when every other key rotates.
  • API keys and user tokens are the natural key for authenticated APIs.
  • Failures say more than raw volume. Count declined payments, 401 responses and invalid coupon codes separately from total requests.

Most of these keys live in your application, not in the web server. Use the server or CDN for IP and path limits, and the application for account, card and failure counts.

How do you rate limit bots in nginx?

This config limits login attempts to 10 per minute per IP address, allows a short burst of 5, and answers the excess with 429 Too Many Requests, the status defined in RFC 6585:

nginx
http {
    limit_req_zone $binary_remote_addr zone=login_ip:10m rate=10r/m;
    limit_req_status 429;

    server {
        server_name example.com;

        location = /login {
            limit_req zone=login_ip burst=5 nodelay;
            proxy_pass http://127.0.0.1:8080;
        }
    }
}

rate=10r/m allows one request every six seconds on average. burst=5 lets a client go five requests over that rate before nginx rejects anything, and nodelay serves those burst requests at once instead of spacing them out. The nginx limit_req docs put one megabyte of zone at about 8,000 states on 64-bit platforms, so 10m tracks roughly 80,000 addresses at once.

If nginx sits behind a CDN or load balancer, every request appears to come from the proxy. Use the realip module so $binary_remote_addr is the visitor's address:

nginx
set_real_ip_from 192.0.2.0/24;
real_ip_header X-Forwarded-For;
real_ip_recursive on;

Replace the range with your proxy's published ranges. You can stack a second limit on another key, such as an API key header:

nginx
limit_req_zone $http_x_api_key zone=per_key:10m rate=60r/m;

Then add limit_req zone=per_key burst=20; in the same location as the IP limit. nginx does not count requests with an empty key, so keep the IP limit for clients that send no key. Also reject unknown keys in your application, or an attacker can send a new random key with each request.

Rate limits at the CDN

CDN rate limiting runs before traffic reaches you and scales with the CDN. Plans differ. On Cloudflare, lower plans count by IP only, while counting by header, cookie, ASN or JA4 fingerprint needs the Enterprise Advanced Rate Limiting option. Check the Cloudflare rate limiting rules docs for your plan.

Tools that help

StopBots is published by Kairal, which is one of the tools listed. The list is not a ranking.

  • nginx limit_req is built into nginx and free, and it is enough for per-IP and per-path limits on a single site.
  • Cloudflare rate limiting rules apply limits at the edge, with more counting options on higher plans.
  • fail2ban is open source and bans addresses that keep hitting your nginx or Apache limits, which turns a short rejection into a longer block.
  • CrowdSec is open source, detects abusive patterns in logs and shares a community blocklist of addresses seen attacking other sites.
  • Kairal links device, network and behavioural signals across sessions, so a distributed attack from many IP addresses shows up as one actor, with protection set per endpoint.

Common questions

Does rate limiting stop bots?

It stops bots that are fast and come from a few addresses. Bots that rotate through proxy pools and send a handful of requests per address pass any per-IP limit, so against a distributed attack a rate limit slows the attacker down at best.

What status code should a rate limit return?

429 Too Many Requests, defined in RFC 6585, is the standard choice, optionally with a Retry-After header that tells well behaved clients when to come back. nginx returns 503 by default, so set limit_req_status 429.

What is a good rate limit for a login page?

There is no universal number. As a starting point, try about 10 login attempts per minute per IP address and 5 failed attempts per username in 15 minutes, then adjust from your logs by checking how many tries real users need before they succeed or reset their password.