How to limit the number of concurrent connection requests in nginx

How to limit the number of concurrent connection requests in nginx

Introduction

The module that limits the number of concurrent connections is: http_limit_conn_module, address: http://nginx.org/en/docs/http/ngx_http_limit_conn_module.html

The module that limits the number of concurrent requests is: http_limit_req_module, address: http://nginx.org/en/docs/http/ngx_http_limit_req_module.html

Both modules are compiled into Nginx by default.

Limit the number of concurrent connections

Example configuration:

http {
	limit_conn_zone $binary_remote_addr zone=addr:10m;
  #limit_conn_zone $server_name zone=perserver:10m;
  
  server {
    limit_conn addr 1;
    limit_conn_log_level warn;
    limit_conn_status 503;
  }
}

limit_conn_zone key zone=name:size; defines the configuration of concurrent connections

  • The definable module is the http module.
  • The key keyword is based on what variable to limit the number of connections. In the example, there are binary_remote_addr and $server_name, depending on the actual business needs.
  • The zone defines the configuration name and maximum shared memory. If the occupied memory exceeds the maximum shared memory, the server returns an error

$binary_remote_addr in the example is the binary user address. Binary is used to save bytes and reduce the size of shared memory.

limit_conn zone number; concurrent connection limit

  • The modules that can be defined are http, server, and location modules
  • zone specifies which limit_conn_zone configuration to use
  • Number is the limit on the number of connections. In the example configuration, the limit is 1 connection.

limit_conn_log_level info | notice | warn | error ; Log level when limit occurs

  • The modules that can be defined are http, server, and location modules

limit_conn_status code; the error code returned when a limit occurs, the default is 503

  • The modules that can be defined are http, server, and location modules

Limit the number of concurrent requests

limit_req_zone key zone=name:size rate=rate; Defines the configuration for limiting concurrent requests.

  • If the occupied memory exceeds the maximum shared memory, the server returns an error response
  • rate defines the request rate, such as 10r/s for 10 requests per second, 10r/m for 10 requests per minute

limit_req zone=name [burst=number] [nodelay | delay=number];

  • zone defines which limit_req_zone configuration to use
  • burst=number sets the number of requests that the bucket can store, which is the buffer size of the request
  • Requests for the nodelay burst bucket are no longer buffered and are directly transmitted, and the rate request rate becomes invalid.
  • delay=number When receiving a request for the first time, number requests can be delivered in advance.

limit_req_log_level info | notice | warn | error; Log level when limit occurs

  • The modules that can be defined are http, server, and location modules

limit_req_status code ; error code when a limit occurs

  • The modules that can be defined are http, server, and location modules

Example Configuration 1

http {
  limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
  limit_req zone=one burst=5;
}

The request rate is 1 request delivered per second. The burst bucket size can store 5 requests. Requests exceeding the limit will return an error.

Example Configuration 2

http {
  limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
  limit_req zone=one burst=5 nodelay;
}

Example configuration 2 adds the nodelay option to example configuration 1. Then the rate request rate is useless. All requests in the burst bucket will be passed directly. Requests exceeding the limit will return an error.

Example Configuration 3

http {
  limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
  limit_req zone=one burst=5 delay=3;
}

Example configuration 3 adds delay=3 option to example configuration 1. Meaning the first 3 requests are delivered immediately, then the rest are delivered at the request rate. Requests exceeding the limit will return an error.

This is the end of this article about how to limit the number of concurrent connection requests in nginx. For more information about how to limit the number of concurrent connection requests in nginx, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • How to limit concurrent IP and download speed in Nginx
  • Nginx+Lua+Redis builds high-concurrency web applications
  • Nginx optimized configuration and kernel optimization to achieve over 100,000 concurrent connections
  • Tutorial on using google-perftools to optimize nginx performance at high concurrency (full version)
  • How to set up nginx and apache to limit concurrent IP access and limit IP connections
  • Nginx limits the number of visits and requests to a certain IP in the same time period. Example code

<<:  Summary of common Mysql DDL operations

>>:  Commonly used JavaScript array methods

Recommend

Details of watch monitoring properties in Vue

Table of contents 1.watch monitors changes in gen...

Solve the problem that Docker cannot ping the host machine under Mac

Solution Abandon the Linux virtual machine that c...

Detailed explanation of MySQL database paradigm

Preface: I have often heard about database paradi...

CSS example code for implementing sliding doors

The so-called sliding door technology means that ...

Detailed explanation of Nginx rewrite jump application scenarios

Application scenario 1: Domain name-based redirec...

CSS3 border effects

What is CSS# CSS (abbreviation of Cascading Style...

React Native environment installation process

react-native installation process 1.npx react-nat...

Common JavaScript memory errors and solutions

Table of contents 1. Timer monitoring 2. Event mo...

In-depth understanding of HTML form input monitoring

Today I saw a blog post about input events, and o...

How to add Vite support to old Vue projects

1. Introduction I have taken over a project of th...

Detailed explanation of linux crm deployment code

Linux basic configuration Compile and install pyt...

Vue calls the computer camera to realize the photo function

This article example shares the specific code of ...

Web front-end development course What are the web front-end development tools

With the development of Internet technology, user...

Analysis of slow insert cases caused by large transactions in MySQL

【question】 The INSERT statement is one of the mos...