Using the ngx_http_keyval_module in Nginx

nginx keyval module
20 July 2025

About the ngx_http_keyval_module in nginx


The ngx_http_keyval_module is a very useful tool in nginx that allows us to store and manage key-value information. This module is particularly useful when working with temporary data and can be applied in many different scenarios. By using this module, we can store data in specific areas called keyval_zone.



In fact, you can use keyval_zone to link requests with storing values related to each user, IP address, or even the URL path used. For example, if we want to keep track of views from a specific page over time, we can utilize this module. This topic helps us optimize our websites' performance and easily gather valuable information.



Using the ngx_http_keyval_module is very straightforward. You just need to define a keyval_zone and then use it in your nginx configuration. Next, I will show you how to accomplish this task and will provide sample related codes.



Considering all these points, the ngx_http_keyval_module has become one of the essential tools for developers in nginx. With the help of this module, not only can you gather valuable information but you can also utilize different strategies to benefit from them. Now let's take a look at the configuration and how to use this module.



# Define the zone for storing key-value data
keyval_zone my_zone 10m;

# Using keyval on the server side
server {
location / {
# Getting the key value
set $my_key_value $keyval;
# Setting and storing the key value
keyval_set my_key_value 1;
}
}


Code Description



Code: keyval_zone my_zone 10m;
Description: In this line, we define an area named my_zone with a capacity of 10 megabytes for storing key-value data.

Code: server {
Description: Here, we initiate a new server block that will specify the properties related to the website in it.

Code: location / {
Description: This configuration is for requests that originate from the website root (/). Here, we'll specify the settings specific to this location.

Code: set $my_key_value $keyval;
Description: We create a variable named my_key_value which will take its value from keyval.

Code: keyval_set my_key_value 1;
Description: We set the key my_key_value to 1, and this value will be stored in the area keyval_zone.

FAQ

?

What is the ngx_http_keyval_module?

?

How can I use keyval_zone?

?

Is this module useful for website optimization?