rate_limit_prefix

Sets a prefix for rate limit keys used in Redis-backed rate limiting in NGINX.

Syntaxrate_limit_prefix prefix_string;
Defaultnone
Contexthttp, server, location
Arguments1

Description

The rate_limit_prefix directive adds a specified prefix to the keys used for rate limiting when Redis is the backend. This is useful for organizing and differentiating rate limit keys for different use cases or applications within the same Redis instance. When a client request is processed, the directive ensures that the full key used for rate limiting combines the specified prefix with the dynamically generated key based on the request context (such as IP address or API key).

When configured within a server or location block, the prefix is concatenated with the request-specific key, using an underscore as a separator. This concatenation ensures that the keys are unique for different rate limiting contexts, preventing collisions. If no prefix is defined, the rate limit key remains as just the generated context-specific key.

In practice, using prefixes can help implement multiple rate limit rules without conflicts and make it easier to monitor or manage rate limits from within Redis, as keys will be easily identifiable based on their prefixes. For instance, you might configure different prefixes for different API endpoints to differentiate their usage and rate limits easily.

Config Example

location = /api {
    rate_limit_prefix api;
    rate_limit $limit_key requests=10 period=1m burst=5;
    rate_limit_pass redis;
}

Ensure that the combined length of the prefix and key does not exceed Redis's key size limit (65535 bytes).

Be cautious with similar prefixes across different contexts, as this can lead to confusion in monitoring rate limit statistics.

If rate_limit_prefix is set in a location block, ensure it does not conflict with other locations or contexts that handle the same keys.

← Back to all directives