How Nginx limits http resource requests
Prerequisite: nginx needs to have the ngx_http_limit_conn_module and ngx_http_limit_req_module modules. You can use the command 2>&1 nginx -v | tr ' ' '\n'|grep limit to check whether there are corresponding modules. If not, please recompile and install these two module.
The test version is: nginx version is 1.15
Limit the number of links
1. Use The limit_conn_zone directive defines the key and sets parameters for the shared memory zone (worker processes will use this zone to share a counter of key values). The first parameter specifies the expression to be evaluated as the key. The second parameter zone specifies the name and size of the zone:
limit_conn_zone $binary_remote_addr zone=addr:10m;
2. Use the limit_conn directive in the context of location {}, server {} or http {} to apply the limit. The first parameter is the value set above. The specified shared memory area name. The second parameter is the number of links allowed for each key:
location /download/ { limit_conn addr 1; }
When using the $binary_remote_addr variable as a parameter, it is based on the restriction of the ip address. You can also use the $server_name variable. Limit the number of connections to a given server:
http { limit_conn_zone $server_name zone=servers:10m; server { limit_conn servers 1000; } }
Limit request rate
Rate limiting can be used to prevent ddos, cc attacks, or to prevent upstream servers from being attacked at the same time Flooded with too many requests. This method is based on the leaky bucket algorithm, where requests arrive at the bucket at various rates and leave the bucket at a fixed rate. Before using rate limiting, you need to configure the global parameters of the "leaky bucket":
key - a parameter used to distinguish one client from another, usually the variable
shared memory zone - The name and size of the zone that holds the state of these keys (i.e. the "leaky bucket")
rate - Number of requests per second ( The request rate limit specified in r/s) or requests per minute (r/m) ("leaky bucket draining"). Requests per minute specifies a rate of less than one request per second.
These parameters are set using the limit_req_zone directive. This directive is defined at the http {} level - this approach allows applying different zones and requesting overflow parameters to different contexts:
http { #... limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s; }
With this configuration, a 10m bytes size with the name one will be created Shared memory area. This area holds the state of the client ip address set using the $binary_remote_addr variable. Note that $remote_addr also contains the client's IP address, while $binary_remote_addr holds a shorter binary representation of the IP address.
The optimal size of the shared memory area can be calculated using the following data: The value size of $binary_remote_addr ipv4 address is 4 bytes, and the storage state on 64-bit platforms takes up 128 bytes. Therefore, state information for approximately 16000 IP addresses takes up 1m bytes of this area.
If storage space is exhausted when nginx needs to add new entries, the oldest entries will be deleted. If the freed space is still not enough to accommodate the new record, nginx will return a 503 service unavailable status code, which can be redefined using the limit_req_status directive.
Once this zone is set, you can limit the request rate using the limit_req directive anywhere in the nginx configuration, especially server {}, location {} and http {} Context:
http { #... limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s; server { #... location /search/ { limit_req zone=one; } } }
Using the above configuration, nginx will process no more than 1 request per second under the /search/ route, and the way to delay processing these requests is that the total rate is no greater than the set rate. nginx will delay processing such requests until "bucket" (shared bucket one) is full. For requests to the full bucket, nginx will respond with a 503 service unavailable error (when limit_req_status does not have a custom set status code).
Limit bandwidth
To limit the bandwidth per connection, use the following limit_rate directive:
location /download/ { limit_rate 50k; }
With this setting, the client Will be able to download content at speeds of up to 50k/sec over a single connection. However, clients can open multiple connections to bypass this limit. Therefore, if the goal is to prevent download speeds greater than a specified value, the number of connections should be limited as well. For example, one connection per IP address (if using the shared memory region specified above):
location /download/ { limit_conn addr 1; limit_rate 50k; }
To impose a limit only after the client has downloaded a certain amount of data, use the limit_rate_after directive. It might be reasonable to allow the client to quickly download a certain amount of data (e.g. file header - movie index) and limit the rate at which the rest of the data is downloaded (making the user watch the movie instead of downloading).
limit_rate_after 500k; limit_rate 20k;
The following example shows a combined configuration for limiting the number of connections and bandwidth. The maximum number of connections allowed is set to 5 connections per client address, which works for most common cases as modern browsers typically have a maximum of 3 connections open at a time. At the same time, the location provided for download only allows one connection:
http { limit_conn_zone $binary_remote_address zone=addr:10m server { root /www/data; limit_conn addr 5; location / { } location /download/ { limit_conn addr 1; limit_rate_after 1m; limit_rate 50k; } } }
The above is the detailed content of How Nginx limits http resource requests. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











How to configure Nginx in Windows? Install Nginx and create a virtual host configuration. Modify the main configuration file and include the virtual host configuration. Start or reload Nginx. Test the configuration and view the website. Selectively enable SSL and configure SSL certificates. Selectively set the firewall to allow port 80 and 443 traffic.

Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

Create a container in Docker: 1. Pull the image: docker pull [mirror name] 2. Create a container: docker run [Options] [mirror name] [Command] 3. Start the container: docker start [Container name]

The methods that can query the Nginx version are: use the nginx -v command; view the version directive in the nginx.conf file; open the Nginx error page and view the page title.

How to configure an Nginx domain name on a cloud server: Create an A record pointing to the public IP address of the cloud server. Add virtual host blocks in the Nginx configuration file, specifying the listening port, domain name, and website root directory. Restart Nginx to apply the changes. Access the domain name test configuration. Other notes: Install the SSL certificate to enable HTTPS, ensure that the firewall allows port 80 traffic, and wait for DNS resolution to take effect.

When the Nginx server goes down, you can perform the following troubleshooting steps: Check that the nginx process is running. View the error log for error messages. Check the syntax of nginx configuration. Make sure nginx has the permissions you need to access the file. Check file descriptor to open limits. Confirm that nginx is listening on the correct port. Add firewall rules to allow nginx traffic. Check reverse proxy settings, including backend server availability. For further assistance, please contact technical support.
