Exclusive Metin2 game shield on FreeBSD: Automatic siege mode, VIP list, and whitelist from your dashboard. See how it works
Create a server

Guide

How to Limit Requests in Nginx (Rate Limiting)

A bot that brute-forces passwords on your login form or a client that constantly calls your API can overload PHP or the database. Nginx can limit the number of requests it accepts from each IP address and respond with a 429 error to the rest, before they reach your application. In this guide, you’ll secure your login and API and test them without blocking anyone.

How It Works

  • limit_req controls the rate: for example, 5 requests per minute per IP. Any excess requests are rejected.
  • burst allows for a slight leeway beyond the limit, so that a user who enters the wrong password twice is not locked out.
  • limit_conn controls the simultaneous connections per IP.

1. Define the zones

The zones store the counter for each IP address in memory. Define them in a /etc/nginx/conf.d/ file, which is loaded within the http block. With 10 MB, you can store about 160,000 addresses:

# /etc/nginx/conf.d/limites.conflimit_req_zone $binary_remote_addr zone=login:10m rate=5r/m;limit_req_zone $binary_remote_addr zone=api:10m rate=10r/s;limit_conn_zone $binary_remote_addr zone=porip:10m;limit_req_status 429;limit_conn_status 429;

By default, Nginx responds with a 503 error to rejected requests; limit_req_status 429 returns the correct code, 429 (too many requests).

2. Apply them where needed

Don't limit the entire website to the same rate: a single page loads dozens of files. Apply the limits to sensitive paths, within the server block:

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 /api/ {    limit_req zone=api burst=20 nodelay;    limit_conn porip 20;    proxy_pass http://127.0.0.1:3000;}

Warning: A location = block for a single PHP file must include its own fastcgi_pass. If you don't include it, Nginx may serve the source code instead of executing it. Copy the PHP-FPM version from your location ~ \.php$ block.

3. Try it out first without blocking anyone (dry run)

With limit_req_dry_run on; and limit_conn_dry_run on; in the same block, Nginx doesn't reject anything: it just logs the requests and connections it would have rejected in the error log. Leave it for a day and check who would have been affected:

sudo nginx -t && sudo systemctl reload nginxsudo grep -E 'limiting (requests|connections)' /var/log/nginx/error.log | tail

If IP addresses from real users appear, increase the rate or the burst. When you're satisfied, remove the two lines of dry_run and run sudo nginx -t && sudo systemctl reload nginx again.

4. If your website is behind a proxy or CDN

Behind Cloudflare or another proxy, all requests come from the proxy's IP addresses, and the limit would apply to all your visitors at once. Have Nginx use the visitor's actual IP address with the realip module:

# /etc/nginx/conf.d/realip.conf (un set_real_ip_from por cada rango de tu proxy)set_real_ip_from 173.245.48.0/20;set_real_ip_from 103.21.244.0/22;real_ip_header CF-Connecting-IP;

Add all the ranges published by your provider, for both IPv4 and IPv6. Accept that header only from those ranges: if you accept it from any IP address, an attacker could spoof it and bypass the limit.

5. Exclude your own IP addresses

If your office or monitoring system makes a lot of requests, exclude them from the limit. An empty key isn't counted; this version of the login zone replaces the one from step 1:

geo $limitar {    default      1;    203.0.113.10 0;}map $limitar $clave_limite {    0 "";    1 $binary_remote_addr;}limit_req_zone $clave_limite zone=login:10m rate=5r/m;

6. Check to see if it works

From an IP address that isn't on the whitelist, send several requests in a row to the login page:

for i in $(seq 1 8); do  curl -s -o /dev/null -w "%{http_code}\n" https://example.com/wp-login.phpdone

With rate=5r/m and burst=3, the first four requests return a 200 status code, and the subsequent ones return a 429 status code. After that, Nginx accepts a new request every 12 seconds.

ext step

The Nginx limit curbs abuse, but it doesn't block the IP address. Combine it with Fail2Ban: its nginx-limit-req filter reads those rejections in the error log and blocks persistent IP addresses at the firewall. If your website grows, choose a Cloud VPS with CPU headroom to handle traffic spikes.

Related guides