Prerequisites
Before going forward, make sure you have the following prerequisites:- A server with root access
- A domain name or IP address
- A web server that can be used to serve static files
- Nginx installed and configured on the machine
Adding Rate Limiting
Nginx provides a simple way to add rate limiting using thelimit_req_zone and limit_req directives.
1
Edit the main configuration file to define a rate limit zone
2
Add the following to the http block
limit_req_zone is used in Nginx to define a shared memory zone that will be used to store the state of rate limits for incoming requests. Here’s a breakdown of the specific directive you provided:$binary_remote_addr: This is a variable that holds the client’s IP address in a binary format. Using the binary format saves memory, which is important when dealing with large numbers of requests.zone=mylimit:10m: This specifies the name and size of the shared memory zone used to store the state of rate limits.mylimitis the name of the zone.10mmeans that the size of the zone is 10 megabytes. This size dictates how many IP addresses and their request states can be stored in memory. A 10MB zone can typically store about 160,000 states (given that each state takes about 64 bytes).
rate=2r/s: This sets the request rate limit.2r/smeans that each IP address is allowed to make 2 requests per second.
limit_req_zone in your Nginx configuration, it sets up a system to control how many requests a client can make to your server in a given time frame. This can help protect your server from being overwhelmed by too many requests from a single client, which can be a form of denial-of-service attack.3
Edit your server block configuration to apply the rate limit
4
Add the following inside the server block
mylimit zone.burst=20allows a burst of up to 20 requests beyond the defined rate. So, even if the rate limit is set to 2 requests per second, the burst allows up to 20 requests to be made in a second. The requests exceeding the rate limit will be rejected immediately.nodelaymeans that requests that exceed the rate limit should be rejected immediately rather than delayed.
5
Test and reload Nginx
Summary
In this tutorial, we have learned how to do rate limit via Nginx.References
Next: Setup SSL for Nginx
Continue your journey by learning how to set up SSL for Nginx on Ubuntu.

