http-dynamic-limit-req #
The ngx_dynamic_limit_req_module module is used to dynamically lock IP and release it periodically.
Source: upstream source
Directives
dynamic_limit_req #
syntax: dynamic_limit_req zone=name [burst=number] [nodelay | delay=number]; · default: — · context: http, server, location, if
Assigns a shared-memory rate-limiting zone and configures burst and delay behavior for excess requests.
dynamic_limit_req_log_level #
syntax: dynamic_limit_req_log_level info | notice | warn | error; · default: dynamic_limit_req_log_level error; · context: http, server, location
By default, the maximum burst size is equal to zero. ## dynamic_limit_req_log_level Sets the desired logging level for cases when the server refuses to process requests due to rate exceeding, or delays request processing.
dynamic_limit_req_redis #
syntax: dynamic_limit_req_redis unix_socket | port=[number] requirepass=[password]; · default: port 6379 · context: http
Requests with an empty key value are not accounted. ## dynamic_limit_req_redis Sets optional parameters, unix_socket, port, requirepass.
dynamic_limit_req_status #
syntax: dynamic_limit_req_status code; · default: dynamic_limit_req_status 503; · context: http, server, location, if
Integer value.
dynamic_limit_req_zone #
syntax: dynamic_limit_req_zone key zone=name:size rate=rate [sync] redis=127.0.0.1 block_second=time; · default: — · context: http
Defines a shared memory zone with Redis-backed key rate tracking and a block_second timeout for offending keys.
Example
worker_processes 2;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
dynamic_limit_req_zone $binary_remote_addr zone=one:10m rate=100r/s redis=127.0.0.1 block_second=300;
dynamic_limit_req_zone $binary_remote_addr zone=two:10m rate=50r/s redis=127.0.0.1 block_second=600;
dynamic_limit_req_zone $binary_remote_addr zone=sms:5m rate=5r/m redis=127.0.0.1 block_second=1800;
server {
listen 80;
server_name localhost;
location / {
if ($http_x_forwarded_for) {
return 400;
}
root html;
index index.html index.htm;
dynamic_limit_req zone=one burst=100 nodelay;
dynamic_limit_req_status 403;
}
error_page 403 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name localhost2;
location / {
root html;
index index.html index.htm;
set $flag 0;
if ($document_uri ~* "regist"){
set $flag "${flag}1";
}
if ($request_method = POST ) {
set $flag "${flag}2";
}
if ($flag = "012"){
dynamic_limit_req zone=sms burst=3 nodelay;
dynamic_limit_req_status 403;
}
if ($document_uri ~* "getSmsVerifyCode.do"){
dynamic_limit_req zone=sms burst=5 nodelay;
dynamic_limit_req_status 444;
}
dynamic_limit_req zone=two burst=50 nodelay;
dynamic_limit_req_status 403;
}
error_page 403 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}