Nginx request limit configuration method

Nginx request limit configuration method

Nginx is a powerful, high-performance web and reverse proxy server with many excellent features:
In the case of high connection concurrency, Nginx is a good alternative to Apache service: Nginx is one of the software platforms often chosen by bosses in the virtual hosting business in the United States. Able to support responses of up to 50,000 concurrent connections, thanks to Nginx for choosing epoll and kqueue as the development model for us.

1. Nginx request limit

  • limit_conn_module Connection frequency limit
  • limit_req_module request frequency limit

2. Connection and request of HTTP protocol

1. The concept of HTTP protocol connection and request

  • HTTP request is based on a TCP connection. To complete an HTTP request, a three-way handshake of TCP is required.
  • An HTTP request is established based on the TCP connection. One TCP request generates at least one HTTP request.

2. Illustration of HTTP protocol connection and request

insert image description here

3. The relationship between HTTP protocol connection and request versions

HTTP protocol version Connection relationship
HTTP1.0 TCP cannot be reused
HTTP1.1 Sequential TCP multiplexing
HTTP2.0 Multiplexing TCP multiplexing

3. Configuration syntax of Nginx connection limit

1. Configuration syntax of limit_conn_zone

  • Syntax: limit_conn_zone key zone=name:size; limit_conn_zone indicates the requested connection storage space. key indicates which connection is keyed. For example, if the client's IP is used as the key, the restriction is on the client's IP. zone=name indicates the name of the storage space applied for. size indicates the size of the storage space requested
  • Default: —— indicates that there is no configuration by default
  • Context: http means that it needs to be configured in the http block

2. Configuration syntax of limit_conn

  • Syntax: limit_conn zone number; zone indicates the name of the storage space applied for in limit_conn_zone, and number indicates the limit of concurrent requests, that is, the number of connections limited at the same time.
  • Default: —— indicates that there is no configuration by default
  • Context: http, server, location means that the configuration needs to be done in the http block, server block, or location block

4. Configuration syntax of Nginx request restriction

1. Configuration syntax of limit_req_zone

  • Syntax: limit_req_zone key zone=name:size rate=rate; limit_req_zone indicates the requested connection storage space. key indicates which connection is keyed. For example, if the client's IP is used as the key, the restriction is on the client's IP. zone=name indicates the name of the storage space applied for. size indicates the size of the storage space requested. rate indicates the limit of the request, which refers to a limit unit (in seconds)
  • Default: —— indicates that there is no configuration by default
  • Context: http means that it needs to be configured in the http block

2. Configuration syntax of limit_req

  • Syntax: limit_req zone=name [burst = number] [nodelay]; zone=name indicates the name of the storage space requested in limit_req_zone; burst = number indicates that after exceeding the set rate, the remaining number of requests will be put into the next second for execution;
  • Default: —— indicates that there is no configuration by default
  • Context: http, server, location means that the configuration needs to be done in the http block, server block, or location block

5. Configuration demonstration of Nginx request restriction

1. First create a submodule.html page and upload it to the /opt/app/html directory, so that you can configure the sub_module module compilation parameters and then access the submodule.html page in the /opt/app/html directory to view the replaced content.

submodule.html page

<html>
	<head>
		<meta charset="utf-8">
		<title>submodule</title>
	</head>
	<body>
		<a>html</a></br>
		<a>xml</a></br>
		<a>json</a></br>
		<a>xml</a></br>
		<a>jsp</a></br>
		<a>html</a>
	</body>
</html>

Browser access is as follows

insert image description here

2. Edit the nginx.conf configuration file

[root@localhost /]# vim /etc/nginx/nginx.conf 

insert image description here

3. Add the following configuration in the http block

#$binary_remote_addr represents the client's address;
#zone=req_zone indicates the storage space name for requests initiated by the same client address #1r/s indicates once per second limit_req_zone $binary_remote_addr zone=req_zone:1m rate=1r/s; 

insert image description here

4. Add the following configuration to the location block of the http block

#Configuration of nginx connection request limit location / {
    root /opt/app/html;
    limit_req zone=req_zone;
} 

insert image description here

5. Check whether the modified configuration file is correct. If the message "successful" is returned, it means that the configuration file has been modified correctly. Otherwise check the configuration file syntax.

insert image description here

6. Reload the configuration file

[root@localhost /]# systemctl reload nginx 

insert image description here

7. Use the ab stress testing tool to make concurrent requests

For the installation of ab stress testing tool, please refer to this blog post link: https://wwwxz.blog.csdn.net/article/details/118584751

#-n indicates the number of requests initiated, -c indicates the number of concurrent requests [root@localhost ~]# ab -n 20 -c 10 http://localhost/submodule.html 

insert image description here

This is the end of this article about how to configure nginx request limit. For more information about nginx request limit, please search 123WORDPRESS.COM’s previous articles or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of Nginx http resource request limit (three methods)
  • Detailed explanation of nginx request limits (connection limits and request limits)
  • A brief discussion on the implementation of Nginx request restriction and access control
  • Global access restriction through nginx proxy interception request
  • Nginx limits the number of visits and requests to a certain IP in the same time period. Example code
  • Tutorial on how to configure the module to limit the number of connections and requests in Nginx server

<<:  Summary of MySQL foreign key constraints and table relationships

>>:  Implementing a table scrolling carousel effect through CSS animation

Recommend

Detailed steps to install Hadoop cluster under Linux

Table of contents 1. Create a Hadoop directory in...

How to choose transaction isolation level in MySQL project

introduction Let's start with our content. I ...

Using vue3 to implement counting function component encapsulation example

Table of contents Preface 1. The significance of ...

Linux system dual network card binding configuration implementation

System version [root@ ~]# cat /etc/redhat-release...

How to deploy your first application with Docker

In the previous article, you have installed Docke...

2 reasons why html-css tag style setting does not work

1 CSS style without semicolon ";" 2 Tags...

Vue implements 3 ways to switch tabs and switch to maintain data status

3 ways to implement tab switching in Vue 1. v-sho...

Docker-compose one-click deployment of gitlab Chinese version method steps

1. Introduction to gitlab Gitlab official address...

HTML user registration page settings source code

Design the web page shown above: <!DOCTYPE htm...

How to query and update the same table in MySQL database at the same time

In ordinary projects, I often encounter this prob...

Solution to ElementUI's this.$notify.close() call not working

Table of contents Requirement Description Problem...