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

Tomcat components illustrate the architectural evolution of a web server

1. Who is tomcat? 2. What can tomcat do? Tomcat i...

Summary of essential knowledge points for MySQL query optimization

Preface Query optimization is not something that ...

Docker win ping fails container avoidance guide

Using win docker-desktop, I want to connect to co...

Install and use Git and GitHub on Ubuntu Linux

Introduction to Git Git is an open source version...

Future-oriented all-round web design: progressive enhancement

<br />Original: Understanding Progressive En...

Solution to nacos not being able to connect to mysql

reason The mysql version that nacos's pom dep...

Docker starts Redis and sets the password

Redis uses the apline (Alps) image of Redis versi...

Vue+swiper realizes timeline effect

This article shares the specific code of vue+swip...

Correct modification steps for Docker's default network segment

background A colleague is working on his security...

How to separate static and dynamic state by combining Apache with Tomcat

Experimental environment Apache and Tomcat are bo...

The implementation process of Linux process network traffic statistics

Preface Linux has corresponding open source tools...

Detailed explanation of the mysql database LIKE operator in python

The LIKE operator is used in the WHERE clause to ...

Vue basic instructions example graphic explanation

Table of contents 1. v-on directive 1. Basic usag...