nginx limit配置参数解读

本文详细解析了Nginx中的ngx_http_core_module、ngx_http_limit_conn_module及ngx_http_limit_req_module模块中的限流相关配置参数。包括limit_rate、limit_except等用于控制带宽、请求速率的方法,以及limit_conn和limit_req如何实现连接数和请求频率的限制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文主要解析一下ngx_http_core_module、ngx_http_limit_conn_module以及ngx_http_limit_req_module中的limit相关配置参数。

limit_rate

名称默认配置作用域官方说明中文解读模块
limit_ratelimit_rate 0;http, server, location, if in locationLimits the rate of response transmission to a client. The rate is specified in bytes per second. The zero value disables rate limiting. The limit is set per a request, and so if a client simultaneously opens two connections, the overall rate will be twice as much as the specified limit.指定每秒该连接能下载的bytes,主要用来限制个别请求的带宽ngx_http_core_module
limit_rate_afterlimit_rate_after 0;http, server, location, if in locationSets the initial amount after which the further transmission of a response to a client will be rate limited.设置多少bytes过后将启动limit计数,如果小于此值则不限速ngx_http_core_module
limit_except没有默认值locationLimits allowed HTTP methods inside a location. The method parameter can be one of the following: GET, HEAD, POST, PUT, DELETE, MKCOL, COPY, MOVE, OPTIONS, PROPFIND, PROPPATCH, LOCK, UNLOCK, or PATCH. Allowing the GET method makes the HEAD method also allowed设置除了指定的http methods外其他method将被限制,允许GET就自动允许HEAD方法ngx_http_core_module
  • 实例
        location /downloads {
            limit_rate_after 1m;
            limit_rate 500k;
        }

        location / {
            proxy_pass http://localhost:3000;
            limit_except GET {
                deny all;
            }
        }
复制代码

limit_conn

名称默认配置作用域官方说明中文解读模块
limit_conn没有默认值,语法 limit_conn zone number;http, server, locationSets the shared memory zone and the maximum allowed number of connections for a given key value. When this limit is exceeded, the server will return the error in reply to a request.指定一个zone的每个key最大连接数ngx_http_limit_conn_module
limit_conn_zone没有默认值,语法 limit_conn_zone key zone=name:size;httpSets parameters for a shared memory zone that will keep states for various keys. In particular, the state includes the current number of connections. The key can contain text, variables, and their combination. Requests with an empty key value are not accounted.第一个参数是key,第二个参数是指定zone及其存放元数据(key,current num of conns per key,zone size)的共享内存大小ngx_http_limit_conn_module
limit_conn_log_levellimit_conn_log_level error;http, server, locationSets the desired logging level for cases when the server limits the number of connections. This directive appeared in version 0.8.18.指定当触发limit的时候日志打印级别ngx_http_limit_conn_module
  • 实例
http {
    limit_conn_zone $binary_remote_addr zone=ips:10m;
    limit_conn_zone $server_name zone=servers:10m;
    limit_conn_log_level notice;
    server {
        # these limits apply to the whole virtual server
        limit_conn ips 10;

        # only 1000 simultaneous connections to the same server_name
        limit_conn servers 1000;
    }
}
复制代码

limit_req

名称默认配置作用域官方说明中文解读模块
limit_req没有默认值,语法 limit_req zone=name [burst=number] [nodelay];http, server, locationSets the shared memory zone and the maximum burst size of requests. If the requests rate exceeds the rate configured for a zone, their processing is delayed such that requests are processed at a defined rate. Excessive requests are delayed until their number exceeds the maximum burst size in which case the request is terminated with an error.指定zone的burst大小ngx_http_limit_req_module
limit_req_zone没有默认值,语法 limit_req_zone key zone=name:size rate=rate;httpSets parameters for a shared memory zone that will keep states for various keys. In particular, the state stores the current number of excessive requests. The key can contain text, variables, and their combination. Requests with an empty key value are not accounted.第一个参数指定key,第二个参数指定zone名称和元数据的内存大小,第三个参数rate指定单位时间的请求数阈值ngx_http_limit_req_module
limit_req_log_levellimit_req_log_level error;http, server, locationSets the desired logging level for cases when the server refuses to process requests due to rate exceeding, or delays request processing. Logging level for delays is one point less than for refusals.指定触发req limit时打印的日志级别ngx_http_limit_req_module
  • 实例
http {
 limit_req_zone $binary_remote_addr zone=myreqzone:10m
 limit_req_log_level warn;

 server {
    ## 每个ip限定10个连接数
    ## 正常一个浏览器给每个host开两到三个连接
    ## 触发的话会返回503
    ## nodelay表示一上来就直接计算,不经过一些预热后再计算
    limit_req zone=myreqzone burst=10 nodelay;
 }
}
复制代码

doc

### Nginx限流配置解决方案 Nginx提供了多种模块来实现限流功能,包括限制连接数和请求速率。以下是一些常见的配置示例及其解释。 #### 1. 连接数限制 通过`limit_conn_module`模块可以限制每个IP地址的并发连接数。以下是配置示例: ```nginx http { # 定义一个共享内存区域用于存储连接状态 limit_conn_zone $binary_remote_addr zone=conn_limit:10m; server { location / { # 每个IP最多允许5个并发连接 limit_conn conn_limit 5; proxy_pass http://backend; } } } ``` 此配置中,`$binary_remote_addr`变量表示客户端的IP地址[^2],`zone=conn_limit:10m`定义了一个名为`conn_limit`的共享内存区域,大小为10MB。`limit_conn conn_limit 5`表示每个IP地址最多只能有5个并发连接。 #### 2. 请求速率限制 使用`ngx_http_limit_req_module`模块可以限制请求速率。以下是一个典型的配置示例: ```nginx http { # 定义一个共享内存区域用于存储请求状态 limit_req_zone $binary_remote_addr zone=req_limit:10m rate=1r/s; server { location / { # 每秒最多允许1个请求 limit_req zone=req_limit burst=5 nodelay; proxy_pass http://backend; } } } ``` 在此配置中,`rate=1r/s`表示每秒允许1个请求,`burst=5`表示可以额外处理5个突发请求,而`nodelay`参数确保这些突发请求不会被延迟处理[^1]。 #### 3. 组合防护方案 为了同时限制连接数和请求速率,可以将上述两种方法结合起来: ```nginx http { # 定义连接限制的共享内存区域 limit_conn_zone $binary_remote_addr zone=conn_limit:10m; # 定义请求速率限制的共享内存区域 limit_req_zone $binary_remote_addr zone=req_limit:10m rate=1r/s; server { location / { # 每个IP最多允许5个并发连接 limit_conn conn_limit 5; # 每秒最多允许1个请求,可容忍5个突发请求 limit_req zone=req_limit burst=5 nodelay; proxy_pass http://backend; } } } ``` 这种组合方案可以有效防止恶意用户通过过多的连接或请求来占用服务器资源[^1]。 #### 4. 实时监控与日志分析 为了更好地了解限流的效果,可以使用以下命令进行实时监控和日志分析: - **实时监控限流计数器**: ```bash watch -n1 "nginx -s limit_status" ``` 此命令会每隔一秒刷新一次限流状态信息。 - **分析访问日志**: 使用`awk`命令过滤包含限流信息的日志条目: ```bash awk '/limit_conn/ || /limit_req/' /var/log/nginx/access.log ``` 以上方法可以帮助管理员快速定位哪些请求触发了限流规则。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值