>_ IzzyLab

Hands-on task

This task is step 19 of the course “nginx basics”

Rate-limit requests

hard~20 min

The nginx site (/etc/nginx/sites-enabled/app) currently responds to any stream of requests. Set up rate limiting: about one request per second per address, with a small allowance for bursts — 2–3 requests. When the limit is exceeded, nginx must respond with an error (503), and after a pause serve requests normally again. The site must still serve its page.

What you'll practise:
  • nginx
  • limit_req_zone
  • limit_req
  • burst
  • rate limiting

Topic breakdown

The nginx rate limiter implements the "leaky bucket" algorithm: limit_req_zone sets up shared memory where the counter state is stored per key (usually the client address), and limit_req enables the check where needed. The zone declaration is only allowed in the http context, so it is put in a separate conf.d file, while it is applied in server/location. The burst parameter sets the size of the queue for bursts: without nodelay such requests are served with a delay, smoothing the flow; with nodelay they are served immediately, but anything beyond the limit is still rejected. Exceeding the limit returns 503; the code can be changed with limit_req_status.

What's checked

  1. a stream of concurrent requests hits the limit
  2. after a pause the site responds with 200 again
  3. a small burst gets through
  4. the limiting zone is declared

What it looks like