Why limit request rates
Every site has finite PHP workers, database connections and CPU time. A human opens a page and reads it; a bot can repeat the same request dozens of times per second, and a handful of bots can take most of the capacity. Rate limiting puts a clear ceiling on what one source may consume. Nginx ships two standard modules for this: limit_req caps requests per unit of time and limit_conn caps simultaneous connections. Both work before the request reaches PHP. They are configured at server level, so they apply to a VPS or your own server; on shared hosting the provider manages such limits.
How limit_req works
The module uses a leaky-bucket algorithm. You declare a zone once in the http block and apply it where needed:
http {
limit_req_zone $binary_remote_addr zone=perip:10m rate=10r/s;
limit_req_zone $binary_remote_addr zone=login:10m rate=5r/m;
limit_req_status 429;
}
According to the Nginx documentation, a one-megabyte zone holds about 16 thousand states, so 10 MB covers tens of thousands of active IPs. Rejected requests get 503 by default; 429 Too Many Requests is semantically better and is not confused with a server error in monitoring.
burst and nodelay
A browser fires several requests per page, so a bare rate is too strict. burst adds a small queue for excess requests. Without nodelay queued requests are released at the configured rate and users feel a delay; with nodelay requests within the burst are served immediately and only those beyond it are rejected.
server {
server_name misol.uz;
location / {
limit_req zone=perip burst=20 nodelay;
try_files $uri $uri/ /index.php?$args;
}
location = /wp-login.php {
limit_req zone=login burst=3 nodelay;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/run/php/php8.2-fpm.sock;
}
}
Nginx 1.15.7 and later also support delay=, which serves part of the queue immediately and delays the rest. Adjust the PHP-FPM socket path to your PHP version.
limit_conn for concurrent connections
http {
limit_conn_zone $binary_remote_addr zone=conn_perip:10m;
limit_conn_status 429;
}
server {
limit_conn conn_perip 20;
location /downloads/ {
limit_conn conn_perip 2;
limit_rate 500k;
}
}
Avoid very low values: many users in an office or on a mobile network can share one public IP.
Exempting trusted addresses
Requests with an empty key are not counted, so geo and map can exempt monitoring or office networks:
geo $limit {
default 1;
203.0.113.10 0;
192.0.2.0/24 0;
}
map $limit $limit_key {
0 "";
1 $binary_remote_addr;
}
limit_req_zone $limit_key zone=perip:10m rate=10r/s;
Behind a CDN or proxy, restore the real client IP with set_real_ip_from and real_ip_header first, or the limit will hit everyone at once.
Testing and monitoring
nginx -t && systemctl reload nginx
for i in $(seq 1 30); do curl -s -o /dev/null -w '%{http_code}
' https://misol.uz/wp-login.php; done
grep 'limiting requests' /var/log/nginx/error.log | tail
An electronics web shop in Tashkent noticed competitor bots scraping its whole catalogue every day and slowing the site in the evenings. It first enabled limit_req_dry_run on; (available since Nginx 1.17.1), which only logs would-be rejections. After a week of log analysis it picked a threshold that did not affect real buyers and switched enforcement on. Choose limits from real traffic, not guesses; on a sayt.uz VPS the Nginx configuration is fully under your control.