keyval
The `keyval` directive defines a variable that retrieves values from a key-value store based on given key-value pairs.
Description
The keyval directive is utilized to create a named variable that is dynamically populated with data from predefined key-value pairs stored in either shared memory or a Redis backend. In its operation, the directive takes three arguments - the first argument represents the key, which can be a combination of static strings and NGINX variables, while the second argument specifies the variable name to which the value corresponding to the key will be assigned. The values stored with the keys could potentially have a time-to-live (TTL) expiration, making it suitable for use cases requiring ephemeral data storage. Furthermore, the third argument allows the specification of a shared memory zone where the key-value pairs are stored, aiding in scalability and performance by utilizing NGINX's inbuilt memory management capabilities.
Config Example
http {
keyval_zone zone=one:32k;
keyval $arg_text $text zone=one;
server {
listen 80;
location / {
return 200 $text;
}
}
}Ensure the memory zone specified with zone= is correctly defined; otherwise, values may not be retrievable.
When using dynamic variables in keys, validate that they do not evaluate to empty; this may lead to unintended behavior.
Key names must be unique within the specified zone to prevent data collisions.