Managing Requests in Nginx with the ngx_http_limit_req_module

nginx ngx_http_limit_req_module limit_req_status
03 April 2025

Managing Requests in Nginx with the ngx_http_limit_req_module

Hello friends! Today we want to talk about one of the features of Nginx that helps us in managing the traffic of our websites. This feature is known as ngx_http_limit_req_module. This module gives us the ability to limit incoming requests and thereby mitigate DDOS attacks or any abnormal traffic.

When a lot of traffic comes to our server, it might be the case that the server cannot respond well, and this may decrease service quality. By using limit_req, we can limit the number of requests a user or IP can make in a specified timeframe. This can help us ensure that our site remains responsive in the face of heavy traffic without feeling that the server is overwhelmed.

The ngx_http_limit_req_module provides several parameters that we can adjust to customize it. One of these parameters is limit_req_status. This parameter allows us to set a specific code for responses to requests that exceed the allowed limit, so we can easily see how many requests were rejected and why.

Now let's see how we can activate this capability and write the necessary code. Below is an example of Nginx configurations that we will examine.

http {
# limited request configurations
limit_req_zone $binary_remote_addr zone=mylimit:10m rate=1r/s;

server {
listen 80;
server_name example.com;

location / {
limit_req zone=mylimit burst=5 nodelay;
limit_req_status 503;
# other content }
}
}

Code Analysis

Now let’s analyze the code line by line:


1. Defining Limited Request:
limit_req_zone $binary_remote_addr zone=mylimit:10m rate=1r/s;
In this line, we create a new limit named mylimit which allocates 10 megabytes of memory and states that at least one request is allowed every second.
2. Defining Server:
server {
This line begins a server block where we can define specific configurations related to a particular server.
3. Binding to a Specific Port:
listen 80;
Here, the server is listening on port 80.
4. Limiting Requests:
limit_req zone=mylimit burst=5 nodelay;
In this section, we set the burst amount to 5, meaning that if needed, we can handle 5 additional requests without delay (without hang-up).
5. Defining Response Status:
limit_req_status 503;
Here, we specify that if the number of requests exceeds the limit, the server should respond with status code 503.

FAQ

?

What is the limit_req module in Nginx and what is its usage?

?

How can I check the limited status of requests in Nginx?

?

How can I limit the number of requests in Nginx?

?

Can I reduce DDoS attacks using limit_req?

?

What is the maximum number of requests I can set?