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.

Installation#

To install the module, use one of the following packages:

  • Angie: angie-module-error-abuse

  • Angie PRO: angie-pro-module-error-abuse

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.

Directives#

The module provides the following directives:

  • 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 to

  • error_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 module

  • error_abuse_redis — sets the Redis server that stores the counters and blocks of the zones with the redis=on parameter (see Using Redis)

The module does not count requests whose key is empty. To exclude trusted addresses from blocking, set key= to a variable that a map block leaves empty for these addresses.

The module also provides variables:

  • $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 blocked

All directives with their defaults are described in the module documentation.

Loading the Module#

To use the module, load it in the 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;
        }
    }
}

Here, the 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#

Let's make six requests to nonexistent pages:

$ 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

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:

$ curl -s -D - -o /dev/null http://localhost/
HTTP/1.1 429 Too Many Requests
...
Cache-Control: private, no-store
Retry-After: 600

The 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.

The log shows how the counter grew:

$ 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

The response that reaches the threshold still goes to the client unchanged; the log already records BLOCKED for it.

Using Redis#

With the 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;
}

If Redis is unavailable, each server keeps counting errors and blocking clients on its own.

Additional Information#

Version: 12fa32c

Detailed documentation and source code are available at: myguard-labs/nginx-error-abuse-module