Home Operation and Maintenance Nginx How to implement nginx speed limit

How to implement nginx speed limit

May 16, 2023 am 11:16 AM
nginx

nginx speed limit can be implemented through the ngx_http_limit_conn_module and ngx_http_limit_req_module modules.

1. ngx_http_limit_conn_module:

This module mainly limits the download speed.

1. Concurrent connection limit:

http
{
    ...
    limit_conn_zone $binary_remote_addr zone=aming:10m;
    ...
    server
    {
        ...
        limit_conn aming 10;
        ...   
    }
}
说明:首先用limit_conn_zone定义了一个内存区块索引aming,大小为10m,它以$binary_remote_addr作为key。
该配置只能在http里面配置,不支持在server里配置。

limit_conn 定义针对aming这个zone,并发连接为10个。在这需要注意一下,这个10指的是单个IP的并发最多为10个。
Copy after login

2. Speed ​​limit:

location ~ /download/ {
    ...
    limit_rate_after 512k;
    limit_rate 150k;
    ...
}
说明:limit_rate_after定义当一个文件下载到指定大小(本例中为512k)之后开始限速;
limit_rate 定义下载速度为150k/s。

注意:这两个参数针对每个请求限速。
Copy after login

2. ngx_http_limit_req_module:

This module is mainly used to limit the number of requests.

1. limit_req_zone:

语法: limit_req_zone $variable zone=name:size rate=rate;
默认值: none
配置段: http

设置一块共享内存限制域用来保存键值的状态参数。 特别是保存了当前超出请求的数量。 
键的值就是指定的变量(空值不会被计算)。
如limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;

说明:区域名称为one,大小为10m,平均处理的请求频率不能超过每秒一次,键值是客户端IP。
使用$binary_remote_addr变量, 可以将每条状态记录的大小减少到64个字节,这样1M的内存可以保存大约1万6千个64字节的记录。
如果限制域的存储空间耗尽了,对于后续所有请求,服务器都会返回 503 (Service Temporarily Unavailable)错误。
速度可以设置为每秒处理请求数和每分钟处理请求数,其值必须是整数,
所以如果你需要指定每秒处理少于1个的请求,2秒处理一个请求,可以使用 “30r/m”。
Copy after login

2. limit_req

语法: limit_req zone=name [burst=number] [nodelay];
默认值: —
配置段: http, server, location

设置对应的共享内存限制域和允许被处理的最大请求数阈值。 
如果请求的频率超过了限制域配置的值,请求处理会被延迟,所以所有的请求都是以定义的频率被处理的。 
超过频率限制的请求会被延迟,直到被延迟的请求数超过了定义的阈值,
这时,这个请求会被终止,并返回503 (Service Temporarily Unavailable) 错误。

这个阈值的默认值为0。如:
limit_req_zone $binary_remote_addr zone=aming:10m rate=1r/s;
server {
    location /upload/ {
        limit_req zone=aming burst=5;
    }
}

限制平均每秒不超过一个请求,同时允许超过频率限制的请求数不多于5个。

如果不希望超过的请求被延迟,可以用nodelay参数,如:

limit_req zone=aming burst=5
Copy after login

Example:

http {
    limit_req_zone $binary_remote_addr zone=aming:10m rate=1r/s;

    server {
        location  ^~ /download/ {  
            limit_req zone=aming burst=5;
        }
    }
}
Copy after login

Set whitelist:

如果是针对公司内部IP或者lo(127.0.0.1)不进行限速,如何做呢?这就要用到geo模块了。

假如,预把127.0.0.1和192.168.100.0/24网段设置为白名单,需要这样做。
在http { }里面增加:
geo $limited {
    default 1;
    127.0.0.1/32 0;
    192.168.100.0/24 0;
}

map $limited $limit {
    1 $binary_remote_addr;
    0 "";
}

原来的 “limit_req_zone $binary_remote_addr ” 改为“limit_req_zone $limit”

完整示例:

http {
    geo $limited {
        default 1;
        127.0.0.1/32 0;
        192.168.100.0/24 0;
    }

    map $limited $limit {
        1 $binary_remote_addr;
        0 "";
    }
    
    limit_req_zone $limit zone=aming:10m rate=1r/s;

    server {
        location  ^~ /download/ {  
            limit_req zone=aming burst=5;
        }
    }
}
Copy after login

The above is the detailed content of How to implement nginx speed limit. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to configure cloud server domain name in nginx How to configure cloud server domain name in nginx Apr 14, 2025 pm 12:18 PM

How to configure an Nginx domain name on a cloud server: Create an A record pointing to the public IP address of the cloud server. Add virtual host blocks in the Nginx configuration file, specifying the listening port, domain name, and website root directory. Restart Nginx to apply the changes. Access the domain name test configuration. Other notes: Install the SSL certificate to enable HTTPS, ensure that the firewall allows port 80 traffic, and wait for DNS resolution to take effect.

How to check whether nginx is started How to check whether nginx is started Apr 14, 2025 pm 01:03 PM

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

How to check nginx version How to check nginx version Apr 14, 2025 am 11:57 AM

The methods that can query the Nginx version are: use the nginx -v command; view the version directive in the nginx.conf file; open the Nginx error page and view the page title.

How to create a mirror in docker How to create a mirror in docker Apr 15, 2025 am 11:27 AM

Steps to create a Docker image: Write a Dockerfile that contains the build instructions. Build the image in the terminal, using the docker build command. Tag the image and assign names and tags using the docker tag command.

How to check the name of the docker container How to check the name of the docker container Apr 15, 2025 pm 12:21 PM

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

How to start nginx server How to start nginx server Apr 14, 2025 pm 12:27 PM

Starting an Nginx server requires different steps according to different operating systems: Linux/Unix system: Install the Nginx package (for example, using apt-get or yum). Use systemctl to start an Nginx service (for example, sudo systemctl start nginx). Windows system: Download and install Windows binary files. Start Nginx using the nginx.exe executable (for example, nginx.exe -c conf\nginx.conf). No matter which operating system you use, you can access the server IP

How to run nginx apache How to run nginx apache Apr 14, 2025 pm 12:33 PM

To get Nginx to run Apache, you need to: 1. Install Nginx and Apache; 2. Configure the Nginx agent; 3. Start Nginx and Apache; 4. Test the configuration to ensure that you can see Apache content after accessing the domain name. In addition, you need to pay attention to other matters such as port number matching, virtual host configuration, and SSL/TLS settings.

How to check whether nginx is started? How to check whether nginx is started? Apr 14, 2025 pm 12:48 PM

In Linux, use the following command to check whether Nginx is started: systemctl status nginx judges based on the command output: If "Active: active (running)" is displayed, Nginx is started. If "Active: inactive (dead)" is displayed, Nginx is stopped.

See all articles