Home / Configs / Servers / nginx rate limit config

nginx rate limit config: limit requests per IP address

Slow down abusive visitors with limit_req: a request rate per IP, a burst, and a 429 answer.

For nginx 1.18 or newer on Linux.

Rate limiting lets each visitor make only a set number of requests per second. It protects login pages and slow routes from bots and floods. Choose your rate and burst below and add the result to your server.

Create your config file

Type your own values. The file below updates as you type, and nothing you type leaves your browser.

Use localhost for local testing

/etc/nginx/conf.d/ratelimit.conf
limit_req_zone $binary_remote_addr zone=general:10m rate=10r/s;
limit_req_zone $binary_remote_addr zone=strict:10m rate=10r/m;
limit_req_status 429;

server {
  listen 80;
  server_name example.com;

  location / {
    limit_req zone=general burst=20 nodelay;
    proxy_pass http://127.0.0.1:3000;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
  }

  location = /login {
    limit_req zone=strict burst=5 nodelay;
    proxy_pass http://127.0.0.1:3000;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
  }
}

How this setup works

Diagram: Visitor, then nginx, then App or 429
Visitor (sends requests) → nginx (counts per IP) → App or 429 (too fast is refused)

Step-by-step setup

  1. Save the config above as /etc/nginx/conf.d/ratelimit.conf, or merge the lines into your own server block.
  2. Test the config: sudo nginx -t.
  3. Apply it: sudo systemctl reload nginx.
  4. Try it: for i in $(seq 1 60); do curl -s -o /dev/null -w "%{http_code}\n" http://example.com/; done. After the burst you should see 429.
  5. Watch the error log for lines that say limiting requests.

What the important lines mean

Common errors and how to fix them

unknown "binary_remote_addr" variable or limit_req_zone not allowed here
limit_req_zone must be in the http level (a file in conf.d is fine), not inside server or location.
Everyone is limited behind a proxy or CDN
nginx sees only the proxy's address. Use the real client address from the proxy header with the realip module, then key the zone on $binary_remote_addr.
Real visitors get 429
The rate is too low for pages that load many files. Raise the burst, or put the limit only on the paths that need it, such as login.
zero size shared memory zone
The zone name in limit_req must match one defined by limit_req_zone. Check for a typo.

Frequently asked questions

What does burst do?
It lets a visitor send a short spike above the rate. Requests beyond the burst are refused. Without it every extra request is refused at once.
Does rate limiting stop DDoS attacks?
It helps against simple floods from a few addresses. It does not stop large attacks from many addresses. Use a CDN or protection service for those.
Why 429 and not 503?
429 Too Many Requests tells clients that they are being limited. The nginx default is 503, which looks like a server failure.

Written for: nginx limit_req module (built in) · Updated 2026-10-11

Free tools that help

Fix a problem with this setup

More server configs

See all 41 config generators