Detailed explanation of Nginx's connection limit configuration for IP addresses in a network segment

Detailed explanation of Nginx's connection limit configuration for IP addresses in a network segment

The so-called connection limit in Nginx is actually a TCP connection, that is, the connection status successfully established by the requester after a three-way handshake. Nginx generally provides us with the ngx_http_limit_conn_module module to provide connection limit function. This module can limit the number of connections for each key value based on the defined key, such as the number of connections from a single IP source.

ngx_http_limit_conn_module directive explained

Syntax: limit_conn zone number;
Default: —
Context: http, server, location

This directive describes the session state storage area. The key's state holds the current number of connections, and the key's value can be any non-null value for a particular variable (null values ​​will not be considered). $variable defines the key, and zone=name defines the zone name. Common usage examples:

limit_conn_zone $binary_remote_addr zone=addr:10m;

server {
  location /download/ {
    limit_conn addr 1;
  }

The above example shows that when a request is made to access /download/, only one request connection can successfully download the resource at the same time.

Network segment restriction

Common limit_conn_zone configurations generally set $binary_remote_addr or $server_name to limit the number of connections at a single IP or host level.

But sometimes we want to limit the number of connections to a certain IP segment, then we can do this:

http {

... ...

  limit_conn_zone $ip_segment zone=network_segment:10m;

  server {
      ... ...
      set $ip_segment $remote_addr;

      if ( $ip_segment ~ ^(\d+)\.(\d+)\.(\d+)\.(\d+) ) {
        set $ip_segment $1.$2.$3;
      }

      location /download/ {
        limit_conn network_segment 10; 
        #limit_rate 200k;
      }

  }
}

This basically satisfies Nginx's requirement to impose a unified connection limit on the number of connections for all IPs in the network segment.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Detailed explanation of Nginx current limiting configuration
  • Nginx rush purchase current limiting configuration implementation analysis
  • Practical Record of Nginx Current Limitation and Configuration Management

<<:  Vue implements the countdown component for second kills

>>:  How to view the storage location of MySQL data files

Recommend

Each time Docker starts a container, the IP and hosts specified operations

Preface Every time you use Docker to start a Hado...

JS realizes the case of eliminating stars

This article example shares the specific code of ...

Detailed process of building mysql5.7.29 on centos7 of linux

1. Download MySQL 1.1 Download address https://do...

How to use vue3 to build a material library

Table of contents Why do we need a material libra...

MySQL paging query optimization techniques

In applications with paging queries, queries that...

Detailed discussion on the issue of mysqldump data export

1. An error (1064) is reported when using mysqldu...

Understanding JavaScript prototype chain

Table of contents 1. Understanding the Equality R...

Detailed tutorial for installing MySQL 8.0.11 compressed version under win10

After reinstalling my computer recently, I downlo...

How to use css variables in JS

How to use css variables in JS Use the :export ke...

React non-parent-child component parameter passing example code

React is a JAVASCRIPT library for building user i...

How to set static IP for Ubuntu 18.04 Server

1. Background Netplan is a new command-line netwo...

Example of using JS to determine whether an element is an array

Here are the types of data that can be verified l...

Centos6.5 glibc upgrade process introduction

Table of contents Scenario Requirements glibc ver...