Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
nginx
GitHub Repository: nginx/nginx.org
Path: blob/main/xml/en/docs/http/ngx_http_keyval_module.xml
1 views
1
<?xml version="1.0"?>
2
3
<!--
4
Copyright (C) Nginx, Inc.
5
-->
6
7
<!DOCTYPE module SYSTEM "../../../../dtd/module.dtd">
8
9
<module name="Module ngx_http_keyval_module"
10
link="/en/docs/http/ngx_http_keyval_module.html"
11
lang="en"
12
rev="11">
13
14
<section id="summary">
15
16
<para>
17
The <literal>ngx_http_keyval_module</literal> module (1.13.3) creates variables
18
with values taken from key-value pairs managed by the
19
<link doc="ngx_http_api_module.xml" id="http_keyvals_">API</link>
20
or a variable (1.15.10) that can also be set with
21
<link url="https://github.com/nginx/njs-examples/#logging-the-number-of-requests-per-client-http-logging-num-requests">njs</link>.
22
</para>
23
24
<para>
25
<note>
26
This module is available as part of our
27
<commercial_version>commercial subscription</commercial_version>.
28
</note>
29
</para>
30
31
</section>
32
33
34
<section id="example" name="Example Configuration">
35
36
<para>
37
<example>
38
http {
39
40
keyval_zone zone=one:32k state=/var/lib/nginx/state/one.keyval;
41
keyval $arg_text $text zone=one;
42
...
43
server {
44
...
45
location / {
46
return 200 $text;
47
}
48
49
location /api {
50
api write=on;
51
}
52
}
53
}
54
</example>
55
</para>
56
57
</section>
58
59
60
<section id="directives" name="Directives">
61
62
<directive name="keyval">
63
<syntax>
64
<value>key</value>
65
<value>$variable</value>
66
<literal>zone</literal>=<value>name</value></syntax>
67
<default/>
68
<context>http</context>
69
70
<para>
71
Creates a new <value>$variable</value> whose value
72
is looked up by the <value>key</value> in the key-value database.
73
Matching rules are defined by the
74
<link id="keyval_type"><literal>type</literal></link> parameter of the
75
<link id="keyval_zone"><literal>keyval_zone</literal></link> directive.
76
The database is stored in a shared memory zone
77
specified by the <literal>zone</literal> parameter.
78
</para>
79
80
</directive>
81
82
83
<directive name="keyval_zone">
84
<syntax>
85
<literal>zone</literal>=<value>name</value>:<value>size</value>
86
[<literal>state</literal>=<value>file</value>]
87
[<literal>timeout</literal>=<value>time</value>]
88
[<literal>type</literal>=<literal>string</literal>|<literal>ip</literal>|<literal>prefix</literal>]
89
[<literal>sync</literal>]</syntax>
90
<default/>
91
<context>http</context>
92
93
<para>
94
Sets the <value>name</value> and <value>size</value> of the shared memory zone
95
that keeps the key-value database.
96
Key-value pairs are managed by the
97
<link doc="ngx_http_api_module.xml" id="http_keyvals_">API</link>.
98
</para>
99
100
<para id="keyval_state">
101
The optional <literal>state</literal> parameter specifies a <value>file</value>
102
that keeps the current state of the key-value database in the JSON format
103
and makes it persistent across nginx restarts.
104
Changing the file content directly should be avoided.
105
</para>
106
107
<para>
108
Examples:
109
<example>
110
keyval_zone zone=one:32k state=/var/lib/nginx/state/one.keyval; # path for Linux
111
keyval_zone zone=one:32k state=/var/db/nginx/state/one.keyval; # path for FreeBSD
112
</example>
113
</para>
114
115
<para id="keyval_timeout">
116
The optional <literal>timeout</literal> parameter (1.15.0) sets
117
the time after which key-value pairs are removed from the zone.
118
</para>
119
120
<para id="keyval_type">
121
The optional <literal>type</literal> parameter (1.17.1) activates
122
an extra index optimized for matching the key of a certain type
123
and defines matching rules when evaluating
124
a <link id="keyval">keyval</link> <literal>$variable</literal>.
125
<note>
126
The index is stored in the same shared memory zone
127
and thus requires additional storage.
128
</note>
129
130
<list type="tag">
131
132
<tag-name id="keyval_type_string"><literal>type=string</literal></tag-name>
133
<tag-desc>
134
default, no index is enabled;
135
variable lookup is performed using exact match
136
of the record key and a search key
137
</tag-desc>
138
139
<tag-name id="keyval_type_ip"><literal>type=ip</literal></tag-name>
140
<tag-desc>
141
the search key is the textual representation of IPv4 or IPv6 address
142
or CIDR range;
143
to match a record key, the search key must belong to a subnet
144
specified by a record key or exactly match an IP address
145
</tag-desc>
146
147
<tag-name id="keyval_type_prefix"><literal>type=prefix</literal></tag-name>
148
<tag-desc>
149
variable lookup is performed using prefix match
150
of a record key and a search key (1.17.5);
151
to match a record key, the record key must be a prefix of the search key
152
</tag-desc>
153
154
</list>
155
</para>
156
157
<para id="keyval_sync">
158
The optional <literal>sync</literal> parameter (1.15.0) enables
159
<link doc="../stream/ngx_stream_zone_sync_module.xml" id="zone_sync">synchronization</link>
160
of the shared memory zone.
161
The synchronization requires the
162
<literal>timeout</literal> parameter to be set.
163
<note>
164
If the synchronization is enabled, removal of key-value pairs (no matter
165
<link doc="ngx_http_api_module.xml" id="patchHttpKeyvalZoneKeyValue">one</link>
166
or
167
<link doc="ngx_http_api_module.xml" id="deleteHttpKeyvalZoneData">all</link>)
168
will be performed only on a target cluster node.
169
The same key-value pairs on other cluster nodes
170
will be removed upon <literal>timeout</literal>.
171
</note>
172
</para>
173
174
</directive>
175
176
</section>
177
178
</module>
179
180