Error Abuse#
The error_abuse module blocks clients that cause too many error
responses. For each key, by default the client address, it counts responses
with the selected status codes, such as 403 or 404, over a sliding time
window. When the count reaches a threshold, the module blocks the key for a
set time. Angie then rejects each further request that has this key with the
429 (Too Many Requests) status code; such requests do not reach the backend.
Unlike the Limit Req module, which limits the rate of all requests, this module reacts only to error responses. A scanner that probes nonexistent URLs collects 404 responses and gets blocked; requests that end with other status codes are not counted.
Counters and blocks are shared by all worker processes. The zone state can be
saved to a file so that blocks survive a restart, and several Angie servers
can count errors and share blocks through Redis. To install the module, use one of the following packages: Angie: Angie PRO: These packages are not built for ALT, Astra SE, OSNova, RED OS, and ROSA,
nor for Debian 11 and 12, Ubuntu 22.04, AlmaLinux 8, MSVSphere 8,
Oracle Linux 8, and Rocky Linux 8. The module provides the following directives: The module does not count requests whose key is empty. To exclude trusted
addresses from blocking, set The module also provides variables: All directives with their defaults are described in the
module documentation. To use the module, load it in the Here, the Let's make six requests to nonexistent pages: The fifth 404 response reaches the threshold, and the module blocks the
client, so the sixth request already gets 429. The block applies to all of the
client's requests in locations that use the zone, including requests for
existing pages: The The log shows how the counter grew: The response that reaches the threshold still goes to the client unchanged;
the log already records With the If Redis is unavailable, each server keeps counting errors and blocking
clients on its own. Version: 12fa32c Detailed documentation and source code are available at:
myguard-labs/nginx-error-abuse-moduleInstallation#
angie-module-error-abuseangie-pro-module-error-abuseDirectives#
error_abuse_zone — defines a shared memory zone with its key
(key=), counted status codes (statuses=), window
(interval=), threshold (threshold=), and block time
(block=); allowed only in the http context. The
persist= parameter sets a file to save the zone state toerror_abuse — enables the module with the zone set by
zone=; status= sets the status code of the block response,
dry_run=on only logs the blocks the module would apply, and
off disables the moduleerror_abuse_redis — sets the Redis server that stores the counters
and blocks of the zones with the redis=on parameter (see
Using Redis)key= to a variable that a map
block leaves empty for these addresses.$error_abuse_status — the module's decision for the request:
BYPASSED, PASSED, COUNTED, BLOCKED, or
DRY_RUN$error_abuse_count — the number of counted responses for the key
in the current window$error_abuse_blocked_until — the time the block ends, as a Unix
timestamp in seconds, or 0 if the key is not blockedLoading the Module#
main{} context:load_module modules/ngx_http_error_abuse_module.so;
Configuration Example#
http {
log_format abuse '$remote_addr "$request" $status '
'$error_abuse_status count=$error_abuse_count '
'until=$error_abuse_blocked_until';
error_abuse_zone zone=errors:10m statuses=403,404
interval=1m threshold=5 block=10m;
server {
listen 80;
access_log /var/log/angie/abuse.log abuse;
location / {
error_abuse zone=errors;
root /usr/share/angie/html;
}
}
}
errors zone counts 403 and 404 responses for each client
address. Five such responses within a minute block the client for
10 minutes. The abuse log format records the module's decision and
counters for each request in /var/log/angie/abuse.log.Demonstration#
$ for i in 1 2 3 4 5 6; do curl -s -o /dev/null -w '%{http_code} ' http://localhost/missing-$i.php; done; echo
404 404 404 404 404 429
$ curl -s -D - -o /dev/null http://localhost/
HTTP/1.1 429 Too Many Requests
...
Cache-Control: private, no-store
Retry-After: 600
Retry-After header gives the number of seconds until the block
ends. The Cache-Control header prevents shared caches from serving
this response to other clients.$ tail -n 7 /var/log/angie/abuse.log
127.0.0.1 "GET /missing-1.php HTTP/1.1" 404 COUNTED count=1 until=0
127.0.0.1 "GET /missing-2.php HTTP/1.1" 404 COUNTED count=2 until=0
127.0.0.1 "GET /missing-3.php HTTP/1.1" 404 COUNTED count=3 until=0
127.0.0.1 "GET /missing-4.php HTTP/1.1" 404 COUNTED count=4 until=0
127.0.0.1 "GET /missing-5.php HTTP/1.1" 404 BLOCKED count=5 until=1795512567
127.0.0.1 "GET /missing-6.php HTTP/1.1" 429 BLOCKED count=5 until=1795512567
127.0.0.1 "GET / HTTP/1.1" 429 BLOCKED count=5 until=1795512567
BLOCKED for it.Using Redis#
error_abuse_redis directive and the redis=on
parameter of a zone, Angie keeps the counters and blocks of the zone in Redis
as well. Angie servers that connect to the same Redis server and use the same
zone name then count errors together, and a client blocked by one server is
blocked by all of them:http {
error_abuse_redis host=127.0.0.1;
error_abuse_zone zone=errors:10m statuses=403,404
interval=1m threshold=5 block=10m redis=on;
}
Additional Information#