Nginx How to configure the server to restrict access speed _nginx

Source: Internet
Author: User

With Nginx to build a station students, often limited speed demand. The development of testing phase in the local speed limit simulation of the environment, convenient debugging. The operation will limit the speed of the attachment, limit the speed of each user's access, limit the link speed of each IP and other requirements.

Just encountered a bug in the network is very card in the case to reproduce, local debugging access to the machine too fast, configuration nginx success to achieve speed limit purposes, here to share out.

Simple configuration, only 3 lines, open the "Nginx root/conf/nginx.conf" configuration file modified as follows:

 http{... 
 
   Limit_zone one $binary _remote_addr 10m; 
 
   ..... server { 
 
     location/{ 
 
       ... 
 
      .. Limit_conn one 2; 
 
       Limit_rate 40k; 
 
     } 
 
   } 
 
  

The above configuration explanation: Limit_zone defines a container that stores session state for each IP. This example defines a container of 10m size named one, which is used in later limit_conn. LIMIT_CONN specifies that each visitor can only establish two links, limit_rate limit the speed of each link to no more than 40K. Therefore, the above configuration restricts user access to this site with a total speed limit of 80K.

Attribute Description Limit_zone

Grammar:

Copy Code code as follows:
Limit_zone zone_name $variable memory_max_size

Scope: http

Limit_conn

Grammar:

Copy Code code as follows:
Limit_conn Zone_name allow each client to establish the number of links

Scopes: HTTP, server, location

Limit_rate

Grammar:

Copy Code code as follows:
Limit_rate the maximum rate per link

Scopes: HTTP, server, location

Case Case 1: Download resources are placed under the http://domain/download/path, download speed limit of 100K for each visitor, and can only link 1 download links at the same time.

 http{ 
 
   Server { 
 
     location/download/{ 
 
       ... 
 
       .. Limit_conn one 1; 
 
       Limit_rate 100k; 
 
     } 
 
   } 

 

Case 2: The maximum speed per visitor to the site is no more than 100K, and 5 links can be established.


http{ 
 
   Server { 
 
     location/download/{ 
 
       ... 
 
       .. Limit_conn one 5; 
 
       Limit_rate 20k; 
 
     } 
 
   } 
 
  

Since linit_rate is a speed limit for each link, the above example has 5 links to ensure that the total speed does not exceed 100K each link does not exceed 20K

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.