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 use nginx to realize distributed current limit

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of how to use nginx to achieve distributed current limit, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this article on how to use nginx to achieve distributed current limit. Let's take a look at it.

1. Preface

Generally exposed systems will have a large number of requests during promotions or hacker attacks. In order to protect the system from being destroyed by high concurrent traffic, it is necessary to limit the current.

two。 First deploy a program that exposes the interface

What I deploy here is a spring boot project that exposes the following interfaces, which is very simple.

Exposes a restful interface where a get request returns hello world.

Deploy this program to the linux server. Deployment steps will no longer be repeated, Baidu spring boot deployment can be done on its own.

Here I would like to recommend a framework learning exchange group. Communication and learning group number: 575745314 will share some videos recorded by senior architects: spring,mybatis,netty source code analysis, high concurrency, high performance, distributed, micro-service architecture principles, jvm performance optimization, distributed architecture, etc. these become the necessary knowledge system for architects. You can also get free learning resources, which have benefited a lot.

3. Create a nginx profile named nginx.conf

Create a configuration file called nginx.conf, with the following complete content

The current limit section of the configuration file explains:

As mentioned above, the current-limiting configuration of nginx has only two lines of code.

First line:

Limit_req_zone: is the current limit statement.

$binary_remote_addr: it means that the current is limited according to the client ip. For example, the above configuration limits the request frequency of each client ip to once a second. If you play hooligans twice a second, the current limit will return a http 503error to you.

Zone=perip: indicates that the line current limit configuration is identified by the name perip, and will be referred to later by the name perip (that is, the current limit configuration can be defined as multiple)

10m: indicates that the storage space of client ip is 10mb, 1mb stores more than 10,000 ip, and 10mb stores more than 100,000 ip. Search binary_remote_addr in this article to locate the relevant explanation.

Rate=1r/s: indicates that the frequency is one request per second.

The second line:

Indicates that on the myserver cluster, a current-limiting configuration named perip is used

4. Deploy nginx with docker

Copy the nginx.conf configuration file created in the previous step to the linux directory, / root/nginx/ (the directory can be arbitrary), and then deploy the nginx environment with a docker command

What is exposed here is port 8080. Through port 8080, you can access the load balancer node in the nginx configuration, that is, 192.168.10.253 ip port ip. This ip port corresponds to the first step of creating a deployed hello world program.

Access the helloworld interface defined in the first step with code

I am here to execute 10 get requests per second, which is larger than the rate=1r/s request configured in nginx, so I will see 503 errors, as shown below.

If you change the code to execute the get request once a second, there will be no error. You can give it a try.

This is the end of the article on "how to use nginx to achieve distributed current limitation". Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to use nginx to achieve distributed current limit". If you want to learn more, you are 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

Internet Technology

Wechat

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

12
Report