The implementation of nginx current limiting scheme of in three ways

  • 2020-05-14 06:09:35
  • OfStack

By checking the official nginx documents, I found three ways to limit the current of nginx.

1, limit_conn_zone

2, limit_req_zone

3, ngx_http_upstream_module

The first two can only be used on the client side (i.e., single 1ip current limit), and the documentation is very complete. However, after testing, it is found that the results described in the official documentation cannot be achieved (maybe there is something wrong with the testing method).

Here is a brief introduction to the first two:

1, limit_conn_zone

1.1 nginx configuration


http{ 
 limit_conn_zone $binary_remote_addr zone=one:10m; 
 server 
 { 
   ...... 
  limit_conn one 10; 
  ...... 
 } 
} 

The "limit_conn one 10" can be placed in the server layer for the entire server, or in the location layer for a single location.
This configuration indicates that the client can only have 10 concurrent connections.

1.2 the results

[

The ab tool 20 requests nginx in parallel, as you can see
Complete requests: 20
Failed requests: 9

]

(the reason why the number of concurrent connections of one ip in the nginx configuration is 10 and the number of success in the result is +1 is unknown; You can also see in nginx's log that there are 9 requests returned to 503.)

2, limit_req_zone

2.1 nginx configuration


http{ 
 limit_req_zone $binary_remote_addr zone=req_one:10m rate=1r/s; 
 server 
 { 
   ...... 
  limit_req zone=req_one burst=120; 
  ...... 
 } 
} 

Where "limit_req zone=req_one burst=120" can be placed on the server layer for the entire server, or in location only for a single location.

rate=1r/s means that each address can only be requested once per second, which means that the token bucket burst=1201 has a total of 120 tokens, and only one new token is added every second. After the issuance of 120 tokens, the additional requests will be returned to 503.

3, ngx_http_upstream_module

3.1 introduction

As an excellent load balancing module, it is the one I use most in my work. In fact, this module provides the back-end current limiting function we need. According to the official documentation, this module has one parameter: max_conns can limit the current of the server, but it can only be used in the commercial version of nginx. However, after the release of nginx1.11.5, this parameter has been officially removed from the commercial version, which means that it can be used as long as we upgrade the widely used nginx1.9.12 and 1.10 versions of nginx1.9.12 in production. (tests showed that the nginx service could not be started with the addition of this parameter in the old version of nginx.)

3.2 configuration


upstream xxxx{ 
 
 server 127.0.0.1:8080 max_conns=10; 
 
 server 127.0.0.1:8081 max_conns=10; 
 
} 

3.3 results (inconvenient screenshot)

Send 20, 30, 40 concurrent requests to nginx using the ab tool on two machines:

It can be seen that no matter how many concurrent requests are made, there are only 12 successful requests and 2 more successful times. Meanwhile, in the test result of 1.2, the number of successful times is also +1. Here are two machines. So I'm going to assume that the number of successful requests is going to be +1 for the client and +1 for the client.

Note: there are several other important points. max_conns is for a single server in upstream, not all; nginx has a parameter: worker_processes, max_conns is for each worker_processes;

Attached are installation steps for ab tools (reproduced, source unknown)


#ab Running needs dependencies apr-util Package, the installation command is:  
yum install apr-util 
# Install dependencies  yum-utils In the yumdownload  Tools, if not found  yumdownload  Command can  
yum install yum-utils 
cd /opt 
mkdir abtmp 
cd abtmp 
yum install yum-utils.noarch 
yumdownloader httpd-tools* 
rpm2cpio httpd-*.rpm | cpio -idmv 
# Once the operation is complete   Will produce a 1 a  usr  directory  ab Here's the file usr  In the directory  
# Simple instructions for use  
./ab -c 100 -n 10000 http://127.0.0.1/index.html 
#-c 100  That is: concurrent each time 100 a  
#-n 10000  That is:   A total of sending 10000 A request  

Related articles: