1. Foreword
Generally, systems that are exposed to the outside world will receive a large number of requests during promotions or hacker attacks. In order to protect the system from being overwhelmed by the high concurrent traffic that arrives instantly, , you need to limit the current.
2. First deploy a program that exposes the interface to the outside world
What I deploy here is a spring boot project, which is exposed as follows The interface is very simple
Exposed a get request to return hello world restful interface.
Deploy this program to the linux server. The deployment steps are not I won't go into details again, you can deploy it by yourself using Baidu spring boot.
I would like to recommend an architecture learning and exchange group to everyone. Communication and learning group number: 575745314. It will share some videos recorded by senior architects: spring, mybatis, netty source code analysis, principles of high concurrency, high performance, distributed, microservice architecture, jvm performance optimization, distributed architecture, etc. These have become a necessary knowledge system for architects. You can also receive free learning resources and benefit a lot so far
3. Create an nginx configuration file named nginx.conf
Create a configuration file named nginx.conf, the complete content is as follows
Explanation of the current limiting part in the configuration file:
As above, The current limiting configuration of nginx has only two lines of code.
The first line:
limit_req_zone: is the current limiting statement.
$ binary_remote_addr: means to limit the current based on the client IP. For example, the current limiting configuration above limits the request frequency of each client IP to once per second. If you act rogue twice per second, the current will be limited and an http 503 error will be returned. Here you go.
zone=perip: Indicates that the name perip is used to identify this line of current limiting configuration. This line of current limiting configuration will be referenced by the name perip later (that is to say, the current limiting configuration can be defined For multiple)
10m: Indicates that the space to store client IP is 10mb, 1mb can store more than 10,000 ips, 10mb can store more than 100,000 ips, reference explanation: ... In this article Search binary_remote_addr to locate relevant explanations.
rate=1r/s: Indicates that the frequency is one request per second.
Second line:
Indicates that on the myserver cluster, use the current limiting configuration named perip
4. Use docker to deploy nginx
Replace the nginx.conf created in the previous step Configuration file, copy it to the linux directory, under /root/nginx/ (the directory can be arbitrary), and then use a docker command to deploy the nginx environment
The 8080 port is exposed here. The load balancing node in the nginx configuration can be accessed through the 8080 port, that is, the 192.168.10.253:8090 ip port. This ip port corresponds to the hello world program that is created and deployed in the first step.
Use code to access the third The helloworld interface defined in one step
I execute 10 get requests per second, which is already greater than the rate=1r/s configured in nginx, which requests once per second. So you will see a 503 error, as follows.
If you change the code and execute the get request once a second, the error will not be reported. You can try it
The above is the detailed content of How to use nginx to implement distributed current limiting. For more information, please follow other related articles on the PHP Chinese website!

NGINXUnit can be used to deploy and manage applications in multiple languages. 1) Install NGINXUnit. 2) Configure it to run different types of applications such as Python and PHP. 3) Use its dynamic configuration function for application management. Through these steps, you can efficiently deploy and manage applications and improve project efficiency.

NGINX is more suitable for handling high concurrent connections, while Apache is more suitable for scenarios where complex configurations and module extensions are required. 1.NGINX is known for its high performance and low resource consumption, and is suitable for high concurrency. 2.Apache is known for its stability and rich module extensions, which are suitable for complex configuration needs.

NGINXUnit improves application flexibility and performance with its dynamic configuration and high-performance architecture. 1. Dynamic configuration allows the application configuration to be adjusted without restarting the server. 2. High performance is reflected in event-driven and non-blocking architectures and multi-process models, and can efficiently handle concurrent connections and utilize multi-core CPUs.

NGINX and Apache are both powerful web servers, each with unique advantages and disadvantages in terms of performance, scalability and efficiency. 1) NGINX performs well when handling static content and reverse proxying, suitable for high concurrency scenarios. 2) Apache performs better when processing dynamic content and is suitable for projects that require rich module support. The selection of a server should be decided based on project requirements and scenarios.

NGINX is suitable for handling high concurrent requests, while Apache is suitable for scenarios where complex configurations and functional extensions are required. 1.NGINX adopts an event-driven, non-blocking architecture, and is suitable for high concurrency environments. 2. Apache adopts process or thread model to provide a rich module ecosystem that is suitable for complex configuration needs.

NGINX can be used to improve website performance, security, and scalability. 1) As a reverse proxy and load balancer, NGINX can optimize back-end services and share traffic. 2) Through event-driven and asynchronous architecture, NGINX efficiently handles high concurrent connections. 3) Configuration files allow flexible definition of rules, such as static file service and load balancing. 4) Optimization suggestions include enabling Gzip compression, using cache and tuning the worker process.

NGINXUnit supports multiple programming languages and is implemented through modular design. 1. Loading language module: Load the corresponding module according to the configuration file. 2. Application startup: Execute application code when the calling language runs. 3. Request processing: forward the request to the application instance. 4. Response return: Return the processed response to the client.

NGINX and Apache have their own advantages and disadvantages and are suitable for different scenarios. 1.NGINX is suitable for high concurrency and low resource consumption scenarios. 2. Apache is suitable for scenarios where complex configurations and rich modules are required. By comparing their core features, performance differences, and best practices, you can help you choose the server software that best suits your needs.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

Atom editor mac version download
The most popular open source editor

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software