美文网首页
nginx limit配置参数解读 限速(带宽、连接数、请求数

nginx limit配置参数解读 限速(带宽、连接数、请求数

作者: ebayboy | 来源:发表于2019-05-30 16:10 被阅读0次

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_after1m;            limit_rate500k;        }        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_addrzone=ips:10m;    limit_conn_zone$server_namezone=servers:10m;    limit_conn_log_level notice;    server {# these limits apply to the whole virtual serverlimit_conn ips 10;# only 1000 simultaneous connections to the same server_namelimit_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_addrzone=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

limit_conn (限制客户端的并发连接数)测试方法:

配置如下:

server {

        listen 5657;

        server_name server_bb;

        limit_rate 100k;

        limit_conn server_bb 1;

        location / {

            root html;

        } 

    }

}

curl一个大文件:

curl http://192.168.137.101:5657/test

之后查看nginx上的连接数

[root@fanpf-resty conf]# netstat -anpt | grep 192.168.137.201 | grep  ESTABLISHED | wc -l

1

再次curl一个大文件:

curl http://192.168.137.101:5657/test

返回503:

<html>

<head><title>503 Service Temporarily Unavailable</title></head>

<body>

<center><h1>503 Service Temporarily Unavailable</h1></center>

<hr><center>openresty/1.15.8.1rc1</center>

</body>

</html>

证明每个客户端限制1个链接成功了。

相关文章

网友评论

      本文标题:nginx limit配置参数解读 限速(带宽、连接数、请求数

      本文链接:https://www.haomeiwen.com/subject/inyvtctx.html