The configuration method by which the Nginx server limits access speed

  • 2020-05-09 19:56:31
  • OfStack

Students who use Nginx to build stations often have speed limits. The development test stage simulates the public network environment in the local speed limit, which is convenient for debugging. When put into operation, there will be requirements such as limiting the speed of attachments, limiting the access speed of each user, and limiting the link speed of each IP.

I just encountered an Bug, which can only be reappeared when the network is stuck. The local debugging and access speed of the machine is too fast. The configuration of Nginx successfully achieves the goal of speed limit.

Configuration is simple, just 3 lines, open "nginx root /conf/ nginx.conf "configuration file is modified as follows:


 http{ 
 
    ...  
 
   limit_zone one $binary_remote_addr 10m; 
 
    ...  
 
   server { 
 
     location / { 
 
        ...  
 
      limit_conn one 2; 
 
       limit_rate 40k; 
 
     } 
 
   } 
 
 } 

limit_zone defines one container for each IP to store the session state. This example defines a 10m container called one, which is used later in limit_conn. limit_conn specifies that only two links can be established per visitor, and limit_rate limits the speed of each link to 40K. Therefore, the above configuration limits the total speed limit for users to access this site to 80K.

Property description limit_zone

Grammar:

limit_zone zone_name $variable memory_max_size

Scope: http

limit_conn

Grammar:

limit_conn zone_name  The number of links allowed per client 

Scope: http, server, location

limit_rate

Grammar:

limit_rate  The maximum rate per link 

Scope: http, server, location

Case 1: the download resource is placed in the http://domain/download/ path, with a speed limit of 100K per visitor, and only one download link can be linked at the same time.


 http{ 
 
   server { 
 
     location /download/ { 
 
        ...  
 
       limit_conn one 1; 
 
       limit_rate 100k; 
 
     } 
 
   } 

 }

  case 2: each visitor can visit the site at a maximum speed of 100K and can establish five links.

 


http{ 
 
   server { 
 
     location /download/ { 
 
        ...  
 
       limit_conn one 5; 
 
       limit_rate 20k; 
 
     } 
 
   } 
 
 } 

Since linit_rate limits the speed of each link, the above example has 5 links, and each link is limited to 20K if the total speed is guaranteed not to exceed 100K


Related articles: