Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize current limit in Nginx

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly shows you "how to achieve Nginx current limit", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to achieve Nginx current limit" this article.

Nginx current limit

Nginx provides us with request limit module (ngx_http_limit_req_module), token bucket algorithm based traffic limit module (ngx_stream_limit_conn_module), which can easily control token rate, customize current limit, and achieve basic current limit control.

Request limit

The ability to request restrictions comes from the ngx_http_limit_req_module module. To use it, you need to first define the limited reference standard and state cache size in the http configuration section.

Limit_req_zone can only be configured within the scope of http

$binary_remote_addr indicates the IP address requested by the client

Variable name defined by mylimit itself

Frequency of rate requests, how many requests are allowed per second

Limit_req corresponds to limit_req_zone, and burst represents the number of requests cached, that is, the task queue.

The following configuration defines the use of the client's IP as a reference and uses a 10m state cache. The rate=1r/s at the end indicates that requests for each IP are accepted only once per second.

Is the 10m state cache space enough? The official answer is that 1m of cache space can serve 32000 IP addresses in 32-bit systems and 16000 IP addresses in 64-bit systems, so it needs to be adjusted on your own. If the state cache is exhausted, all subsequent requests will receive a 503 (Service Temporarily Unavailable) error.

Script code

# defines a mylimit buffer (container) with a request frequency of 1 request per second (nr/s) limit_req_zone $binary_remote_addr zone=mylimit:10m rate=1r/s;server {listen 70 Location / {# nodelay does not delay processing # burst is configured to overload processing, which can be simply understood as the queue mechanism # the same IP can only be sent once in a second (1r/s). Here, caching 3 requests is configured, which means that only 4 tasks are allowed to respond successfully in the same second, while other task requests fail (503 error) limit_req zone=mylimit burst=3 nodelay;proxy_pass http://localhost:7070;}}

Test code

In order to facilitate the provision of JAVA and AB test code here..

#-n is the total number of times the stress test is executed #-c is the specified number of concurrency ab-n 5-c 5 http://192.168.0.133:70/indexpackage com.battcn.limiting;import org.springframework.http.ResponseEntity;import org.springframework.web.client.RestTemplate;import java.util.concurrent.CompletableFuture;import java.util.concurrent.ExecutionException;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors / * * @ author Levin * @ since 2018-7-27 0027 * / public class NginxLimiterTest {public static void main (String [] args) throws ExecutionException, InterruptedException {ExecutorService service = Executors.newFixedThreadPool (5); for (int I = 0; I

< 6; i++) { CompletableFuture.supplyAsync(() ->

{final ResponseEntity entity = new RestTemplate () .getForEntity ("http://192.168.0.133:70/index", String.class); return entity.getBody ();}, service) .thenAccept (System.out::println);} service.shutdown ();}}

Test log

The log of the AB test results JAVA is not posted here. One of the five requests is problematic, and the one with the problem is the one that has been rejected.

[root@localhost myconf] # ab-n 5-c 5 http://192.168.0.133:70/indexDocument Path: / indexDocument Length: 34 bytesConcurrency Level: 5Time taken for tests: 0.002 secondsComplete requests: 5Failed requests: 1 (Connect: 0, Receive: 0, Length: 1, Exceptions: 0) concurrency limit

The function of Nginx concurrency restriction comes from the ngx_http_limit_conn_module module, just like the request configuration, you need to define the reference standard and state cache before using it.

Limit_conn_zone can only be configured within the scope of http

$binary_remote_addr indicates the IP address requested by the client

Variable name (buffer) defined by myconn itself

Limit_rate limits transmission speed

Limit_conn corresponds to limit_conn_zone, limiting the number of network connections

The following configuration defines the use of the client's IP as a reference and uses a 10m state cache. Only one request connection is allowed per IP, and the maximum transmission speed is 1024KB.

Script code

# defines a myconn buffer (container) limit_conn_zone $binary_remote_addr zone=myconn:10m;server {listen 70 location / {# only one connection is allowed per IP # limit the transmission speed (N * limit_rate if there are N concurrent connections, N * limit_rate) limit_rate 1024kth proxy pass http://localhost:7070;}} above is all the content of the article "how Nginx achieves current limiting", thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report