• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

src/H09-Mar-2016-975694

t/H09-Mar-2016-372293

util/H09-Mar-2016-2920

.gitattributesH A D09-Mar-201627 21

.gitignoreH A D09-Mar-2016327 4342

README.mdH A D09-Mar-201612.8 KiB389273

configH A D09-Mar-20161.5 KiB4538

valgrind.suppressH A D09-Mar-20162.9 KiB160159

README.md

1Name
2====
3
4array-var-nginx-module - Add support for array-typed variables to nginx config files
5
6*This module is not distributed with the Nginx source.* See the
7installation instructions.
8
9Table of Contents
10=================
11
12* [Name](#name)
13* [Status](#status)
14* [Synopsis](#synopsis)
15* [Description](#description)
16* [Directives](#directives)
17    * [array_split](#array_split)
18    * [array_join](#array_join)
19    * [array_map](#array_map)
20    * [array_map_op](#array_map_op)
21* [Installation](#installation)
22    * [Building as a dynamic module](#building-as-a-dynamic-module)
23* [Compatibility](#compatibility)
24* [Source Repository](#source-repository)
25* [Getting involved](#getting-involved)
26* [Author](#author)
27* [Copyright & License](#copyright--license)
28* [See Also](#see-also)
29
30Status
31======
32
33This module is production ready.
34
35Synopsis
36========
37
38```nginx
39location /foo {
40    array_split ',' $arg_files to=$array;
41
42    # use the set_quote_sql_str directive in the ngx_set_misc
43    # module to map to each element in the array $array:
44    array_map_op set_quote_sql_str $array;
45
46    array_map "name = $array_it" $array;
47
48    array_join ' or ' $array to=$sql_condition;
49
50    # well, we could feed it to ngx_drizzle to talk to MySQL, for example ;)
51    echo "select * from files where $sql_condition";
52}
53```
54
55Description
56===========
57
58This module provides array typed nginx variables to `nginx.conf`.
59
60Under the hood, this module just "abuses" the nginx string values to hold binary pointers
61to C data structures (NGINX core's `ngx_array_t` struct on the C land).
62
63The array type gives `nginx.onf` wonderful capabilities of handling value lists. Nowadays, however,
64you are highly recommended to use the [ngx_lua](https://github.com/openresty/lua-nginx-module) module
65so as to have the full scripting power provided by the Lua language in nginx.
66
67[Back to TOC](#table-of-contents)
68
69Directives
70==========
71
72[Back to TOC](#table-of-contents)
73
74array_split
75-----------
76**syntax:** *array_split <separator> <subject> to=$target_variable*
77
78**default:** *no*
79
80**context:** *http, server, server if, location, location if*
81
82Splits the string value in the `subject` argument with the separator string specified by the
83`separator` argument. The result is an array-typed value saved to the nginx variable specified by the `to=VAR` option.
84
85For example,
86
87```nginx
88array_split "," $arg_names to=$names;
89```
90
91will split the string values in the URI query argument `names` into an array-typed value saved to the custom nginx variable
92`$names`.
93
94This directive creates an array-typed variable. Array-typed variables cannot be used outside
95the directives offered by this module. If you want to use the values in an array-typed variable
96in other contexts,
97you must use the [array_join](#array_join) directive to produce a normal string value.
98
99[Back to TOC](#table-of-contents)
100
101array_join
102----------
103**syntax:** *array_split <separator> $array_var*
104
105**default:** *no*
106
107**context:** *http, server, server if, location, location if*
108
109Joins the elements in the array-typed nginx variable (`$array_var`) into a single string value
110with the separator specified by the first argument.
111
112For example,
113
114```nginx
115location /foo {
116    array_split ',' $arg_names to=$names;
117    array_join '+' $names;
118    echo $names;
119}
120```
121
122Then request `GET /foo?names=Bob,Marry,John` will yield the response body
123
124```
125Bob+Marry+John
126```
127
128In the example above, we use the [ngx_echo](https://github.com/openresty/echo-nginx-module) module's [echo](https://github.com/openresty/echo-nginx-module#echo) directive to output
129the final result.
130
131[Back to TOC](#table-of-contents)
132
133array_map
134---------
135**syntax:** *array_map <template> $array_var*
136
137**syntax:** *array_map <template> $array_var to=$new_array_var*
138
139**default:** *no*
140
141**context:** *http, server, server if, location, location if*
142
143Maps the string template to each element in the array-typed nginx variable specified. Within
144the string template, you can use the special iterator variable `$array_it` to reference the current
145array element in the array being mapped.
146
147For example,
148
149```nginx
150array_map "[$array_it]" $names;
151```
152
153will change each element in the array variable `$names` by putting the square brackets around
154each element's string value. The modification is in-place in this case.
155
156If you do not want in-place modifications, you can use the `to=$var` option to specify a new nginx variable to hold the results. For instance,
157
158```nginx
159array_map "[$array_it]" $names to=$new_names;
160```
161
162where the results are saved into another (array-typed) nginx variable named `$new_names` while
163the `$names` variable keeps intact.
164
165Below is a complete example for this:
166
167```nginx
168location /foo {
169    array_split ',' $arg_names to=$names;
170    array_map '[$array_it]' $names;
171    array_join '+' $names;
172    echo "$names";
173}
174```
175
176Then request `GET /foo?names=bob,marry,nomas` will yield the response body
177
178```
179[bob]+[marry]+[nomas]
180```
181
182[Back to TOC](#table-of-contents)
183
184array_map_op
185------------
186**syntax:** *array_map_op <directive> $array_var*
187
188**syntax:** *array_map_op <directive> $array_var to=$new_array_var*
189
190**default:** *no*
191
192**context:** *http, server, server if, location, location if*
193
194Similar to the [array_map](#array_map) directive but maps the specified nginx configuration directive instead of
195a string template to each element in the array-typed nginx variable specified. The result
196of applying the specified configuration directive becomes the result of the mapping.
197
198The nginx configuration directive being used as the iterator must be implemented by [Nginx Devel Kit](https://github.com/simpl/ngx_devel_kit) (NDK)'s set_var submodule's `ndk_set_var_value`.
199For example, the following [set-misc-nginx-module](http://github.com/openresty/set-misc-nginx-module) directives can be invoked this way:
200
201* [set_quote_sql_str](http://github.com/openresty/set-misc-nginx-module#set_quote_sql_str)
202* [set_quote_pgsql_str](http://github.com/openresty/set-misc-nginx-module#set_quote_pgsql_str)
203* [set_quote_json_str](http://github.com/openresty/set-misc-nginx-module#set_quote_json_str)
204* [set_unescape_uri](http://github.com/openresty/set-misc-nginx-module#set_unescape_uri)
205* [set_escape_uri](http://github.com/openresty/set-misc-nginx-module#set_escape_uri)
206* [set_encode_base32](http://github.com/openresty/set-misc-nginx-module#set_encode_base32)
207* [set_decode_base32](http://github.com/openresty/set-misc-nginx-module#set_decode_base32)
208* [set_encode_base64](http://github.com/openresty/set-misc-nginx-module#set_encode_base64)
209* [set_decode_base64](http://github.com/openresty/set-misc-nginx-module#set_decode_base64)
210* [set_encode_hex](http://github.com/openresty/set-misc-nginx-module#set_encode_base64)
211* [set_decode_hex](http://github.com/openresty/set-misc-nginx-module#set_decode_base64)
212* [set_sha1](http://github.com/openresty/set-misc-nginx-module#set_encode_base64)
213* [set_md5](http://github.com/openresty/set-misc-nginx-module#set_decode_base64)
214
215This is a higher-order operation where other nginx configuration directives can be used
216as arguments for this `map_array_op` directive.
217
218Consider the following example,
219
220```nginx
221array_map_op set_quote_sql_str $names;
222```
223
224This line changes each element in the array-typed nginx variable `$names` by applying the
225[set_quote_sql_str](https://github.com/openresty/set-misc-nginx-module#set_quote_sql_str)
226directive provided by the [ngx_set_misc](https://github.com/openresty/set-misc-nginx-module)
227module one by one. The result is that each element in the array `$names` has been escaped as SQL string literal values.
228
229You can also specify the `to=$var` option if you do not want in-place modifications of the input arrays. For instance,
230
231```nginx
232array_map_op set_quote_sql_str $names to=$quoted_names;
233```
234
235will save the escaped elements into a new (array-typed) nginx variable named `$quoted_names` with `$names` intact.
236
237The following is a relatively complete example:
238
239```nginx
240location /foo {
241    array_split ',' $arg_names to=$names;
242    array_map_op set_quote_sql_str $names;
243    array_join '+' $names to=$res;
244    echo $res;
245}
246```
247
248Then request `GET /foo?names=bob,marry,nomas` will yield the response body
249
250```
251'bob'+'marry'+'nomas'
252```
253
254Pretty cool, huh?
255
256[Back to TOC](#table-of-contents)
257
258Installation
259============
260
261You're recommended to install this module (as well as the Nginx core and many other goodies) via the [OpenResty bundle](http://openresty.org). See [the detailed instructions](http://openresty.org/#Installation) for downloading and installing OpenResty into your system. This is the easiest and most safe way to set things up.
262
263Alternatively, you can install this module manually with the Nginx source:
264
265Grab the nginx source code from [nginx.org](http://nginx.org/), for example,
266the version 1.9.7 (see [nginx compatibility](#compatibility)), and then build the source with this module:
267
268```bash
269
270 $ wget 'http://nginx.org/download/nginx-1.9.7.tar.gz'
271 $ tar -xzvf nginx-1.9.7.tar.gz
272 $ cd nginx-1.9.7/
273
274 # Here we assume you would install you nginx under /opt/nginx/.
275 $ ./configure --prefix=/opt/nginx \
276     --add-module=/path/to/array-var-nginx-module
277
278 $ make -j2
279 $ make install
280```
281
282Download the latest version of the release tarball of this module from [array-var-nginx-module file list](https://github.com/openresty/array-var-nginx-module/tags).
283
284Also, this module is included and enabled by default in the [OpenResty bundle](http://openresty.org).
285
286[Back to TOC](#table-of-contents)
287
288Building as a dynamic module
289----------------------------
290
291Starting from NGINX 1.9.11, you can also compile this module as a dynamic module, by using the `--add-dynamic-module=PATH` option instead of `--add-module=PATH` on the
292`./configure` command line above. And then you can explicitly load the module in your `nginx.conf` via the [load_module](http://nginx.org/en/docs/ngx_core_module.html#load_module)
293directive, for example,
294
295```nginx
296load_module /path/to/modules/ndk_http_module.so;  # assuming NDK is built as a dynamic module too
297load_module /path/to/modules/ngx_http_array_var_module.so;
298```
299
300[Back to TOC](#table-of-contents)
301
302Compatibility
303==============
304
305The following versions of Nginx should work with this module:
306
307* **1.9.x** (last tested: 1.9.7)
308* **1.8.x**
309* **1.7.x** (last tested: 1.7.10)
310* **1.6.x**
311* **1.5.x** (last tested: 1.5.12)
312* **1.4.x** (last tested: 1.4.2)
313* **1.2.x** (last tested: 1.2.9)
314* **1.1.x** (last tested: 1.1.5)
315* **1.0.x** (last tested: 1.0.8)
316* **0.9.x** (last tested: 0.9.4)
317* **0.8.x** (last tested: 0.8.54)
318* **0.7.x >= 0.7.44** (last tested: 0.7.68)
319
320Earlier versions of Nginx like 0.6.x and 0.5.x will *not* work.
321
322If you find that any particular version of Nginx above 0.7.44 does not
323work with this module, please consider reporting a bug.
324
325[Back to TOC](#table-of-contents)
326
327Source Repository
328=================
329
330Available on github at [openresty/array-var-nginx-module](https://github.com/openresty/array-var-nginx-module).
331
332[Back to TOC](#table-of-contents)
333
334Getting involved
335================
336
337You'll be very welcomed to submit patches to the author or just ask for
338a commit bit to the source repository on GitHub.
339
340[Back to TOC](#table-of-contents)
341
342Author
343======
344
345Yichun "agentzh" Zhang (章亦春) <agentzh@gmail.com>, CloudFlare Inc.
346
347[Back to TOC](#table-of-contents)
348
349Copyright & License
350===================
351
352Copyright (c) 2009-2016, Yichun Zhang (agentzh) <agentzh@gmail.com>, CloudFlare Inc.
353
354This module is licensed under the terms of the BSD license.
355
356Redistribution and use in source and binary forms, with or without
357modification, are permitted provided that the following conditions are
358met:
359
360*   Redistributions of source code must retain the above copyright
361notice, this list of conditions and the following disclaimer.
362*   Redistributions in binary form must reproduce the above copyright
363notice, this list of conditions and the following disclaimer in the
364documentation and/or other materials provided with the distribution.
365
366THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
367IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
368TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
369PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
370HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
371SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
372TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
373PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
374LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
375NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
376SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
377
378[Back to TOC](#table-of-contents)
379
380See Also
381========
382
383* [NDK](https://github.com/simpl/ngx_devel_kit)
384* [ngx_lua](https://github.com/openresty/lua-nginx-module)
385* [ngx_set_misc](https://github.com/openresty/set-misc-nginx-module)
386
387[Back to TOC](#table-of-contents)
388
389