Path: blob/main/xml/en/docs/http/ngx_http_keyval_module.xml
1 views
<?xml version="1.0"?>12<!--3Copyright (C) Nginx, Inc.4-->56<!DOCTYPE module SYSTEM "../../../../dtd/module.dtd">78<module name="Module ngx_http_keyval_module"9link="/en/docs/http/ngx_http_keyval_module.html"10lang="en"11rev="11">1213<section id="summary">1415<para>16The <literal>ngx_http_keyval_module</literal> module (1.13.3) creates variables17with values taken from key-value pairs managed by the18<link doc="ngx_http_api_module.xml" id="http_keyvals_">API</link>19or a variable (1.15.10) that can also be set with20<link url="https://github.com/nginx/njs-examples/#logging-the-number-of-requests-per-client-http-logging-num-requests">njs</link>.21</para>2223<para>24<note>25This module is available as part of our26<commercial_version>commercial subscription</commercial_version>.27</note>28</para>2930</section>313233<section id="example" name="Example Configuration">3435<para>36<example>37http {3839keyval_zone zone=one:32k state=/var/lib/nginx/state/one.keyval;40keyval $arg_text $text zone=one;41...42server {43...44location / {45return 200 $text;46}4748location /api {49api write=on;50}51}52}53</example>54</para>5556</section>575859<section id="directives" name="Directives">6061<directive name="keyval">62<syntax>63<value>key</value>64<value>$variable</value>65<literal>zone</literal>=<value>name</value></syntax>66<default/>67<context>http</context>6869<para>70Creates a new <value>$variable</value> whose value71is looked up by the <value>key</value> in the key-value database.72Matching rules are defined by the73<link id="keyval_type"><literal>type</literal></link> parameter of the74<link id="keyval_zone"><literal>keyval_zone</literal></link> directive.75The database is stored in a shared memory zone76specified by the <literal>zone</literal> parameter.77</para>7879</directive>808182<directive name="keyval_zone">83<syntax>84<literal>zone</literal>=<value>name</value>:<value>size</value>85[<literal>state</literal>=<value>file</value>]86[<literal>timeout</literal>=<value>time</value>]87[<literal>type</literal>=<literal>string</literal>|<literal>ip</literal>|<literal>prefix</literal>]88[<literal>sync</literal>]</syntax>89<default/>90<context>http</context>9192<para>93Sets the <value>name</value> and <value>size</value> of the shared memory zone94that keeps the key-value database.95Key-value pairs are managed by the96<link doc="ngx_http_api_module.xml" id="http_keyvals_">API</link>.97</para>9899<para id="keyval_state">100The optional <literal>state</literal> parameter specifies a <value>file</value>101that keeps the current state of the key-value database in the JSON format102and makes it persistent across nginx restarts.103Changing the file content directly should be avoided.104</para>105106<para>107Examples:108<example>109keyval_zone zone=one:32k state=/var/lib/nginx/state/one.keyval; # path for Linux110keyval_zone zone=one:32k state=/var/db/nginx/state/one.keyval; # path for FreeBSD111</example>112</para>113114<para id="keyval_timeout">115The optional <literal>timeout</literal> parameter (1.15.0) sets116the time after which key-value pairs are removed from the zone.117</para>118119<para id="keyval_type">120The optional <literal>type</literal> parameter (1.17.1) activates121an extra index optimized for matching the key of a certain type122and defines matching rules when evaluating123a <link id="keyval">keyval</link> <literal>$variable</literal>.124<note>125The index is stored in the same shared memory zone126and thus requires additional storage.127</note>128129<list type="tag">130131<tag-name id="keyval_type_string"><literal>type=string</literal></tag-name>132<tag-desc>133default, no index is enabled;134variable lookup is performed using exact match135of the record key and a search key136</tag-desc>137138<tag-name id="keyval_type_ip"><literal>type=ip</literal></tag-name>139<tag-desc>140the search key is the textual representation of IPv4 or IPv6 address141or CIDR range;142to match a record key, the search key must belong to a subnet143specified by a record key or exactly match an IP address144</tag-desc>145146<tag-name id="keyval_type_prefix"><literal>type=prefix</literal></tag-name>147<tag-desc>148variable lookup is performed using prefix match149of a record key and a search key (1.17.5);150to match a record key, the record key must be a prefix of the search key151</tag-desc>152153</list>154</para>155156<para id="keyval_sync">157The optional <literal>sync</literal> parameter (1.15.0) enables158<link doc="../stream/ngx_stream_zone_sync_module.xml" id="zone_sync">synchronization</link>159of the shared memory zone.160The synchronization requires the161<literal>timeout</literal> parameter to be set.162<note>163If the synchronization is enabled, removal of key-value pairs (no matter164<link doc="ngx_http_api_module.xml" id="patchHttpKeyvalZoneKeyValue">one</link>165or166<link doc="ngx_http_api_module.xml" id="deleteHttpKeyvalZoneData">all</link>)167will be performed only on a target cluster node.168The same key-value pairs on other cluster nodes169will be removed upon <literal>timeout</literal>.170</note>171</para>172173</directive>174175</section>176177</module>178179180