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

Analysis of the operating principle and implementation process of Docker Hub

Similar to the code hosting service provided by G...

CSS controls the spacing between words through the letter-spacing property

letter-spacing property : Increase or decrease th...

MySQL 5.7.18 installation tutorial and problem summary

MySQL 5.7.18 installation and problem summary. I ...

How to view and execute historical commands in Linux

View historical commands and execute specified co...

MySQL 8.0.22 winx64 installation and configuration method graphic tutorial

The database installation tutorial of MySQL-8.0.2...

40+ Beautiful Web Form Design Examples

Web forms are the primary communication channel b...

How to find websites with SQL injection (must read)

Method 1: Use Google advanced search, for example...

JavaScript canvas implements graphics and text with shadows

Use canvas to create graphics and text with shado...

A brief discussion on MySql views, triggers and stored procedures

view What is a view? What is the role of a view? ...

Use pure JS to achieve the secondary menu effect

This article example shares the specific code of ...

MySQL Failover Notes: Application-Aware Design Detailed Explanation

1. Introduction As we all know, in the applicatio...

How to export and import .sql files under Linux command

This article describes how to export and import ....