For current-limiting examples of different URL is as follows:
limit_conn_zone $server_name zone=perserver:10m;
limit_req_zone $server_name zone=one:10m rate=1r/s;
server {
listen 80;
server_name localhost;
location /conn_1/ {
limit_conn perserver 1;
echo_sleep 0.1;
echo $uri;
}
location /conn_5/ {
limit_conn perserver 5;
echo_sleep 0.1;
echo $uri;
}
location /conn_10/ {
limit_conn perserver 10;
echo_sleep 0.1;
echo $uri;
}
location /rate_10B/ {
limit_rate 10;
echo $uri;
}
location /rate_50B/ {
limit_rate 50;
echo $uri;
}
location /rate_4kB/ {
limit_rate 4k;
echo $uri;
}
location /rate/ {
if ($uri ~ "^/rate/10B/") {
limit_rate 10;
}
if ($uri ~ "^/rate/50B/") {
limit_rate 50;
}
if ($uri ~ "^/rate/4kB/") {
limit_rate 4k;
}
echo $uri;
}
location /req_1_0/ {
limit_req zone=one;
echo $uri;
}
location /req_1_5/ {
limit_req zone=one burst=5;
echo $uri;
}
location /req_1_5_nodelay/ {
limit_req zone=one burst=5 nodelay;
echo $uri;
}
}
Note: the echo module used in the example.
Explanation
- server_name must be set. If not set, will "server_name" "use the default value of nginx;" where do the keywords defined by server_name configuration does not take effect.
- Simultaneously transmitting a plurality of requests to access HTTP: // localhost / conn_1 / , only 200 returns a status code, the remaining 503 returns an error. The following information appears in the error log:
Limiting Connections by Zone "PerServer", Client: 127.0.0.1, Server: localhost, Request: "GET / conn_1 / HTTP / 1.1", Host: "localhost"
Similarly, while sending more than five requests access to HTTP: // localhost / conn_5 / , only five return a 200 status code, and the rest returned a 503 error. - Access HTTP: // localhost / rate_10B / , can be seen by looking at the access log, $ bytes_sent (nginx returns the number of bytes to the client) of 199 bytes, while $ REQUEST_TIME (request processing time) was 19.015 seconds, i.e., the speed limit 10 bytes per second. Visit http: // localhost / rate / 10B / when the speed limit is also 10 bytes per second.
Access HTTP: // localhost / rate_50B / , can be seen by looking at the access log, $ bytes_sent is 199 bytes, and $ request_time is 3.003 seconds, that is, the speed limit is 50 bytes per second. - is the rate limiting limit_rate nginx transmits a response to the client, the client does not limit the rate of sending requests to nginx. nginx by connection speed limit, so if a client opens two connections at the same time, the overall rate of clients is twice the value of this instruction set.
- Simultaneously transmitting a plurality of requests to access HTTP: // localhost / req_1_0 / , only 200 returns a status code, the remaining 503 returns an error. The following information appears in the error log:
Limiting Requests, Excess: 1.000 by Zone "One", Client: 127.0.0.1, Server: localhost, Request: "GET / req_1_0 / HTTP / 1.1", Host: "localhost" - Simultaneous transmission 10 requests access to HTTP: // localhost / req_1_5 / , there is an immediate return to state 200 codes, there are four request immediately returns a status code 503, then the remaining 5 returns a response to a request 200 per second, the total takes 5 seconds, a total of six 200 returns a status code. The following information appears in the error log:
Limiting Requests, Excess: 5.997 by Zone "One", Client: 127.0.0.1, Server: localhost, Request: "GET / req_1_5 / HTTP / 1.1", Host: "localhost" - Simultaneous transmission 10 requests access to HTTP: // localhost / req_1_5_nodelay / , six request immediately returns a status code 200, the remaining 4 immediately returns a 503 error. : The following information appears in the error log
limiting requests, excess: 5.998 by zone "one", client: 127.0.0.1, server: localhost, request: "GET / req_1_5_nodelay / HTTP / 1.1", host: "localhost" - limit_conn and limit_req can not be set if the instruction, so if the current limit for different URL, can only be achieved through a different location. limit_rate if instructions can be used if the instruction matches the URL of the URL to achieve a different flow restrictor.
Author: boldcautious
link: https: //www.jianshu.com/p/ca6ac851f04d
Source: Jane books
are copyrighted by the author. Commercial reprint please contact the author authorized, non-commercial reprint please indicate the source.