nginx Method for Limiting the Number of Concurrent Connection Requests

  • 2021-09-05 01:17:13
  • OfStack

Brief introduction

The modules that limit the number of concurrent connections are: http_limit_conn_module, address: http://nginx.org/en/docs/http/ngx_http_limit_conn_module. html

The modules that limit the number of concurrent requests are: 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

Sample 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_zonekey zone=name: size; Defining Configuration for Concurrent Connections

The modules that can be defined are http modules. The key keyword is based on what variable to limit the number of connections, such as binary_remote_addr, $server_name, according to actual business requirements. zone defines the configuration name and the maximum shared memory. If the occupied memory exceeds the maximum shared memory, the server returns an error

In the example $binary_remote_addr It is a binary user address, which is used to save bytes and reduce the size of shared memory.

limit_conn zone number; Concurrent connection limit

The modules can be defined as http, server and location modules zone specifies which limit_conn_zone configuration to use number is the limited number of connections, which is limited to 1 connection in the example configuration.

limit_conn_log_level info notice warn error; Limit the log level when it occurs

The modules can be defined as http, server and location modules

limit_conn_status code; Return error code when limit occurs, default 503

The modules can be defined as http, server and location modules

Limit the number of concurrent requests

limit_req_zone key zone=name: size rate=rate; Defines a configuration that limits concurrent requests.

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

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 the bucket can hold, which is the buffer size of the request The request of nodelay burst bucket is no longer buffered and passed directly, and the request rate of rate is invalid. delay = number When a request is received for the first time, number requests can be passed in advance.

limit_req_log_level info notice warn error; Limit the log level when it occurs

The definable modules are http, server and location modules

limit_req_status code; Error code when limit occurs

The definable modules are http, server and location modules

Sample 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 per second. The burst bucket size can hold 5 requests. Requests that exceed the limit will return an error.

Sample Configuration 2


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

Sample Configuration 2 is added to Sample Configuration 1 nodelay Option. Then the rate request rate doesn't work. All requests in the burst bucket are passed directly. Requests that exceed the limit will return an error.

Sample Configuration 3


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

Sample Configuration 3 is added to Sample Configuration 1 delay=3 Option. Indicates that the first three requests are delivered immediately, and then the other requests are delivered at the request rate. Requests that exceed the limit will return an error.


Related articles: