keyval_zone
Defines a shared memory zone for storing key-value pairs in NGINX.
Description
The keyval_zone directive in NGINX configures a shared memory zone that can be used for storing key-value pairs accessible by other directives in the same context (http or stream). This allows the storage of dynamic data across multiple requests without needing to access a database or external service, improving performance by leveraging in-memory storage. The specified zone name is followed by the desired size allocation, which determines how much memory is reserved for this zone. The memory must be allocated sufficiently to handle the expected number of key-value pairs.
To use the keyval_zone directive, it should be defined with the syntax keyval_zone zone=name:size; where name is the identifier for the memory zone and size indicates the memory allocated (for example, 32k). This zone can then be used in conjunction with other directives such as keyval, which allows for the retrieval and setting of values associated with keys, leveraging the defined memory space. The dynamic nature of variable expansion also allows the creation of composite keys based on other variables, making this feature highly versatile for various application needs.
Config Example
http {
keyval_zone zone=one:32k;
keyval $arg_key $value zone=one;
server {
listen 80;
location / {
return 200 "$value";
}
}
}Ensure the memory allocated is sufficient for the expected key-value pairs; otherwise, data may not be stored properly.
Composite keys must be constructed with care to avoid collisions or unexpected behavior.
Using large sizes might lead to higher memory usage; monitor memory consumption.