negation in the map directive of nginx

Francis Daly francis at daoine.org
Thu Sep 8 22:52:13 UTC 2022


On Thu, Sep 08, 2022 at 01:25:20PM -0400, libresco_27 wrote:

Hi there,

> I'm working on rate limiting for specific group of client ids where if the
> client id is equal to XYZ don't map it, thus, the zone doesn't get
> incremented.

http://nginx.org/r/limit_req_zone: Requests with an empty key value are
not accounted.

It's probably easier to set the value to empty for those ones, and
not-empty for the rest.

> For ex - 
> limit_req_zone $default_rate_client_id zone=globalClientRateLimit_zone:50k
> rate=10r/m sync;
> map $client_id $default_rate_client_id {
>     "^(?!ZZZZZZ)$" "$1" 
> } 

    map $client_id $default_rate_client_id {
        ZZZZZ "";
        default $client_id;
    }

(or whatever value is wanted).

> But this doesn't seem to work. Is this the correct way to negate a
> particular string(ZZZZZ in this example)? Please let me know.

Negative regexes can be hard; it's simpler to avoid them entirely.

Cheers,

	f
-- 
Francis Daly        francis at daoine.org



More information about the nginx mailing list