资讯专栏INFORMATION COLUMN

nginx limit配置参数解读

Jonathan Shieber / 3327人阅读

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

limit_rate
名称 默认配置 作用域 官方说明 中文解读 模块
limit_rate limit_rate 0; http, server, location, if in location Limits 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_after limit_rate_after 0; http, server, location, if in location Sets 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 没有默认值 location Limits 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, location Sets 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; http Sets 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_level limit_conn_log_level error; http, server, location Sets 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, location Sets 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; http Sets 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_level limit_req_log_level error; http, server, location Sets 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

ngx_http_core_module

ngx_http_limit_conn_module

ngx_http_limit_req_module

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/39750.html

相关文章

  • nginx http模块配置参数解读

    摘要:名称默认配置作用域官方说明中文解读模块是否开启对后端的缓冲指定一个连接到代理服务器的超时时间,单位为秒,需要注意的是这个时间最好不要超过秒。 序 本文主要解析一下nginx http模块配置参数。主要分socket相关参数,对clinet请求的buffer参数以及对response的buffer参数。 socket 名称 默认配置 作用域 官方说明 中文解读 模块 sendf...

    pf_miles 评论0 收藏0
  • Nginx 对访问量的控制

    摘要:目的了解的和模块,对请求访问量进行控制。即对并发和并行的控制,这两个功能分别由和模块负责实现。模块说明该模块主要用于对请求并发量进行控制。 目的 了解 Nginx 的 ngx_http_limit_conn_module 和 ngx_http_limit_req_module 模块,对请求访问量进行控制。 Nginx 模块化 nginx 的内部结构是由核心模块和一系列的功能模块所组成。...

    AndroidTraveler 评论0 收藏0
  • Nginx http资源请求限制(三种方法)

    摘要:将延迟处理此类请求,直到存储区共享存储区已满。因此,如果目标是阻止下载速度大于指定值,则连接数也应该受到限制。以下示例显示了用于限制连接数和带宽的组合配置。 原文链接:何晓东 博客 前置条件:nginx 需要有 ngx_http_limit_conn_module 和 ngx_http_limit_req_module 模块,可以使用命令 2>&1 nginx -V | tr ...

    stormzhang 评论0 收藏0
  • Nginx http资源请求限制(三种方法)

    摘要:将延迟处理此类请求,直到存储区共享存储区已满。因此,如果目标是阻止下载速度大于指定值,则连接数也应该受到限制。以下示例显示了用于限制连接数和带宽的组合配置。 原文链接:何晓东 博客 前置条件:nginx 需要有 ngx_http_limit_conn_module 和 ngx_http_limit_req_module 模块,可以使用命令 2>&1 nginx -V | tr ...

    mayaohua 评论0 收藏0
  • Nginx-基础篇

    摘要:四优势多路复用轻量级功能模块少代码模块少亲和把核心和工作进程绑定,把每个进程固定在一个上执行,减少切换的,获得更好的性能。 Nginx-基础篇 一、环境: 系统硬件:CPU>=2Core,内存>=256M 操作系统:CentOS 7.2 x64 二、环境调试确认: 1、四个确认 确认系统网络 ping www.baidu.com 确认yum可用 yum list 确认...

    April 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<