Mercurial > hg > nginx-site
annotate xml/en/docs/dev/development_guide.xml @ 1982:28ee7ab54a90
Added the "Time" section to the development guide.
author | Vladimir Homutov <vl@nginx.com> |
---|---|
date | Tue, 02 May 2017 16:25:33 +0300 |
parents | 082724c57c38 |
children | 7660d6390a9d |
rev | line source |
---|---|
1899 | 1 <?xml version="1.0"?> |
2 | |
3 <!-- | |
4 Copyright (C) Nginx, Inc. | |
5 --> | |
6 | |
7 <!DOCTYPE article SYSTEM "../../../../dtd/article.dtd"> | |
8 | |
9 <article name="Development guide" | |
10 link="/en/docs/dev/development_guide.html" | |
11 lang="en" | |
1914
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
12 rev="2"> |
1899 | 13 |
14 <section name="Introduction" id="introduction"> | |
15 | |
16 | |
17 <section name="Code layout" id="code_layout"> | |
18 | |
19 <para> | |
20 <list type="bullet"> | |
21 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
22 <literal>auto</literal> — build scripts |
1899 | 23 </listitem> |
24 | |
25 <listitem> | |
26 <literal>src</literal> | |
27 | |
28 <list type="bullet"> | |
29 | |
30 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
31 <literal>core</literal> — basic types and functions — string, array, log, |
1899 | 32 pool etc |
33 </listitem> | |
34 | |
35 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
36 <literal>event</literal> — event core |
1899 | 37 |
38 <list type="bullet"> | |
39 | |
40 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
41 <literal>modules</literal> — event notification modules: epoll, kqueue, |
1899 | 42 select etc |
43 </listitem> | |
44 | |
45 </list> | |
46 | |
47 </listitem> | |
48 | |
49 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
50 <literal>http</literal> — core HTTP module and common code |
1899 | 51 |
52 <list type="bullet"> | |
53 | |
54 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
55 <literal>modules</literal> — other HTTP modules |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
56 </listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
57 |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
58 <listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
59 <literal>v2</literal> — HTTPv2 |
1899 | 60 </listitem> |
61 | |
62 </list> | |
63 | |
64 </listitem> | |
65 | |
66 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
67 <literal>mail</literal> — mail modules |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
68 </listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
69 |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
70 <listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
71 <literal>os</literal> — platform-specific code |
1899 | 72 |
73 <list type="bullet"> | |
74 | |
75 <listitem> | |
76 <literal>unix</literal> | |
77 </listitem> | |
78 | |
79 <listitem> | |
80 <literal>win32</literal> | |
81 </listitem> | |
82 | |
83 </list> | |
84 | |
85 </listitem> | |
86 | |
87 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
88 <literal>stream</literal> — stream modules |
1899 | 89 </listitem> |
90 | |
91 </list> | |
92 | |
93 </listitem> | |
94 | |
95 </list> | |
96 </para> | |
97 | |
98 </section> | |
99 | |
100 | |
101 <section name="Include files" id="include_files"> | |
102 | |
103 <para> | |
104 Each nginx file should start with including the following two files: | |
105 </para> | |
106 | |
107 | |
108 <programlisting> | |
109 #include <ngx_config.h> | |
110 #include <ngx_core.h> | |
111 </programlisting> | |
112 | |
113 <para> | |
114 In addition to that, HTTP code should include | |
115 </para> | |
116 | |
117 | |
118 <programlisting> | |
119 #include <ngx_http.h> | |
120 </programlisting> | |
121 | |
122 <para> | |
123 Mail code should include | |
124 </para> | |
125 | |
126 | |
127 <programlisting> | |
128 #include <ngx_mail.h> | |
129 </programlisting> | |
130 | |
131 <para> | |
132 Stream code should include | |
133 </para> | |
134 | |
135 | |
136 <programlisting> | |
137 #include <ngx_stream.h> | |
138 </programlisting> | |
139 | |
140 </section> | |
141 | |
142 | |
143 <section name="Integers" id="integers"> | |
144 | |
145 <para> | |
146 For general purpose, nginx code uses the following two integer types | |
147 <literal>ngx_int_t</literal> and <literal>ngx_uint_t</literal> which are | |
148 typedefs for <literal>intptr_t</literal> and <literal>uintptr_t</literal>. | |
149 </para> | |
150 | |
151 </section> | |
152 | |
153 | |
154 <section name="Common return codes" id="common_return_codes"> | |
155 | |
156 <para> | |
157 Most functions in nginx return the following codes: | |
158 </para> | |
159 | |
160 <para> | |
161 <list type="bullet"> | |
162 | |
163 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
164 <literal>NGX_OK</literal> — operation succeeded |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
165 </listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
166 |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
167 <listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
168 <literal>NGX_ERROR</literal> — operation failed |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
169 </listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
170 |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
171 <listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
172 <literal>NGX_AGAIN</literal> — operation incomplete, function should be called |
1899 | 173 again |
174 </listitem> | |
175 | |
176 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
177 <literal>NGX_DECLINED</literal> — operation rejected, for example, if disabled |
1899 | 178 in configuration. This is never an error |
179 </listitem> | |
180 | |
181 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
182 <literal>NGX_BUSY</literal> — resource is not available |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
183 </listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
184 |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
185 <listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
186 <literal>NGX_DONE</literal> — operation done or continued elsewhere. |
1899 | 187 Also used as an alternative success code |
188 </listitem> | |
189 | |
190 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
191 <literal>NGX_ABORT</literal> — function was aborted. |
1899 | 192 Also used as an alternative error code |
193 </listitem> | |
194 | |
195 </list> | |
196 </para> | |
197 | |
198 </section> | |
199 | |
200 | |
201 <section name="Error handling" id="error_handling"> | |
202 | |
203 <para> | |
204 For getting the last system error code, the <literal>ngx_errno</literal> macro | |
205 is available. | |
206 It's mapped to <literal>errno</literal> on POSIX platforms and to | |
207 <literal>GetLastError()</literal> call in Windows. | |
208 For getting the last socket error number, the | |
209 <literal>ngx_socket_errno</literal> macro is available. | |
210 It's mapped to <literal>errno</literal> on POSIX systems as well, | |
211 and to <literal>WSAGetLastError()</literal> call on Windows. | |
212 For performance reasons the values of <literal>ngx_errno</literal> or | |
213 <literal>ngx_socket_errno</literal> should not be accessed more than | |
214 once in a row. | |
215 The error value should be stored in a local variable of type | |
216 <literal>ngx_err_t</literal> for using multiple times, if required. | |
217 For setting errors, <literal>ngx_set_errno(errno)</literal> and | |
218 <literal>ngx_set_socket_errno(errno)</literal> macros are available. | |
219 </para> | |
220 | |
221 <para> | |
222 The values of <literal>ngx_errno</literal> or | |
223 <literal>ngx_socket_errno</literal> can be passed to logging functions | |
224 <literal>ngx_log_error()</literal> and <literal>ngx_log_debugX()</literal>, in | |
225 which case system error text is added to the log message. | |
226 </para> | |
227 | |
228 <para> | |
229 Example using <literal>ngx_errno</literal>: | |
230 </para> | |
231 | |
232 | |
233 <programlisting> | |
234 void | |
235 ngx_my_kill(ngx_pid_t pid, ngx_log_t *log, int signo) | |
236 { | |
237 ngx_err_t err; | |
238 | |
239 if (kill(pid, signo) == -1) { | |
240 err = ngx_errno; | |
241 | |
242 ngx_log_error(NGX_LOG_ALERT, log, err, "kill(%P, %d) failed", pid, signo); | |
243 | |
244 if (err == NGX_ESRCH) { | |
245 return 2; | |
246 } | |
247 | |
248 return 1; | |
249 } | |
250 | |
251 return 0; | |
252 } | |
253 </programlisting> | |
254 | |
255 </section> | |
256 | |
257 | |
258 </section> | |
259 | |
260 | |
261 <section name="Strings" id="strings"> | |
262 | |
263 | |
264 <section name="Overview" id="overview"> | |
265 | |
266 <para> | |
267 For C strings, nginx code uses unsigned character type pointer | |
268 <literal>u_char *</literal>. | |
269 </para> | |
270 | |
271 <para> | |
272 The nginx string type <literal>ngx_str_t</literal> is defined as follows: | |
273 </para> | |
274 | |
275 | |
276 <programlisting> | |
277 typedef struct { | |
278 size_t len; | |
279 u_char *data; | |
280 } ngx_str_t; | |
281 </programlisting> | |
282 | |
283 <para> | |
284 The <literal>len</literal> field holds the string length, | |
285 <literal>data</literal> holds the string data. | |
286 The string, held in <literal>ngx_str_t</literal>, may or may not be | |
287 null-terminated after the <literal>len</literal> bytes. | |
288 In most cases it’s not. | |
289 However, in certain parts of code (for example, when parsing configuration), | |
290 <literal>ngx_str_t</literal> objects are known to be null-terminated, and that | |
291 knowledge is used to simplify string comparison and makes it easier to pass | |
292 those strings to syscalls. | |
293 </para> | |
294 | |
295 <para> | |
296 A number of string operations are provided in nginx. | |
1915
8b7c3b0ef1a4
Semantically marked paths.
Vladimir Homutov <vl@nginx.com>
parents:
1914
diff
changeset
|
297 They are declared in <path>src/core/ngx_string.h</path>. |
1899 | 298 Some of them are wrappers around standard C functions: |
299 </para> | |
300 | |
301 <para> | |
302 <list type="bullet"> | |
303 | |
304 <listitem> | |
305 <literal>ngx_strcmp()</literal> | |
306 </listitem> | |
307 | |
308 <listitem> | |
309 <literal>ngx_strncmp()</literal> | |
310 </listitem> | |
311 | |
312 <listitem> | |
313 <literal>ngx_strstr()</literal> | |
314 </listitem> | |
315 | |
316 <listitem> | |
317 <literal>ngx_strlen()</literal> | |
318 </listitem> | |
319 | |
320 <listitem> | |
321 <literal>ngx_strchr()</literal> | |
322 </listitem> | |
323 | |
324 <listitem> | |
325 <literal>ngx_memcmp()</literal> | |
326 </listitem> | |
327 | |
328 <listitem> | |
329 <literal>ngx_memset()</literal> | |
330 </listitem> | |
331 | |
332 <listitem> | |
333 <literal>ngx_memcpy()</literal> | |
334 </listitem> | |
335 | |
336 <listitem> | |
337 <literal>ngx_memmove()</literal> | |
338 </listitem> | |
339 | |
340 </list> | |
341 | |
342 </para> | |
343 | |
344 <para> | |
345 Some nginx-specific string functions: | |
346 </para> | |
347 | |
348 <para> | |
349 <list type="bullet"> | |
350 | |
351 <listitem> | |
352 <literal>ngx_memzero()</literal> fills memory with zeroes | |
353 </listitem> | |
354 | |
355 <listitem> | |
356 <literal>ngx_cpymem()</literal> does the same as | |
357 <literal>ngx_memcpy()</literal>, but returns the final destination address | |
358 This one is handy for appending multiple strings in a row | |
359 </listitem> | |
360 | |
361 <listitem> | |
362 <literal>ngx_movemem()</literal> does the same as | |
363 <literal>ngx_memmove()</literal>, but returns the final destination address. | |
364 </listitem> | |
365 | |
366 <listitem> | |
367 <literal>ngx_strlchr()</literal> searches for a character in a string, | |
368 delimited by two pointers | |
369 </listitem> | |
370 </list> | |
371 </para> | |
372 | |
373 <para> | |
374 Some case conversion and comparison functions: | |
375 </para> | |
376 | |
377 <para> | |
378 <list type="bullet"> | |
379 | |
380 <listitem> | |
381 <literal>ngx_tolower()</literal> | |
382 </listitem> | |
383 | |
384 <listitem> | |
385 <literal>ngx_toupper()</literal> | |
386 </listitem> | |
387 | |
388 <listitem> | |
389 <literal>ngx_strlow()</literal> | |
390 </listitem> | |
391 | |
392 <listitem> | |
393 <literal>ngx_strcasecmp()</literal> | |
394 </listitem> | |
395 | |
396 <listitem> | |
397 <literal>ngx_strncasecmp()</literal> | |
398 </listitem> | |
399 | |
400 </list> | |
401 </para> | |
402 | |
403 </section> | |
404 | |
405 | |
406 <section name="Formatting" id="formatting"> | |
407 | |
408 <para> | |
409 A number of formatting functions are provided by nginx. These functions support nginx-specific types: | |
410 </para> | |
411 | |
412 | |
413 <para> | |
414 <list type="bullet"> | |
415 | |
416 <listitem> | |
417 <literal>ngx_sprintf(buf, fmt, ...)</literal> | |
418 </listitem> | |
419 | |
420 <listitem> | |
421 <literal>ngx_snprintf(buf, max, fmt, ...)</literal> | |
422 </listitem> | |
423 | |
424 <listitem> | |
1975 | 425 <literal>ngx_slprintf(buf, last, fmt, ...)</literal> |
1899 | 426 </listitem> |
427 | |
428 <listitem> | |
429 <literal>ngx_vslprint(buf, last, fmt, args)</literal> | |
430 </listitem> | |
431 | |
432 <listitem> | |
433 <literal>ngx_vsnprint(buf, max, fmt, args)</literal> | |
434 </listitem> | |
435 | |
436 </list> | |
437 </para> | |
438 | |
439 <para> | |
440 The full list of formatting options, supported by these functions, can be found | |
1915
8b7c3b0ef1a4
Semantically marked paths.
Vladimir Homutov <vl@nginx.com>
parents:
1914
diff
changeset
|
441 in <path>src/core/ngx_string.c</path>. Some of them are: |
1899 | 442 </para> |
443 | |
444 | |
445 <programlisting> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
446 %O — off_t |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
447 %T — time_t |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
448 %z — size_t |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
449 %i — ngx_int_t |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
450 %p — void * |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
451 %V — ngx_str_t * |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
452 %s — u_char * (null-terminated) |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
453 %*s — size_t + u_char * |
1899 | 454 </programlisting> |
455 | |
456 <para> | |
457 The ‘u’ modifier makes most types unsigned, ‘X’/‘x’ convert output to hex. | |
458 </para> | |
459 | |
460 <para> | |
461 Example: | |
462 | |
463 <programlisting> | |
464 u_char buf[NGX_INT_T_LEN]; | |
465 size_t len; | |
466 ngx_int_t n; | |
467 | |
468 /* set n here */ | |
469 | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
470 len = ngx_sprintf(buf, "%ui", n) — buf; |
1899 | 471 </programlisting> |
472 | |
473 </para> | |
474 | |
475 </section> | |
476 | |
477 | |
478 <section name="Numeric conversion" id="numeric_conversion"> | |
479 | |
480 <para> | |
481 Several functions for numeric conversion are implemented in nginx: | |
482 </para> | |
483 | |
484 <para> | |
485 <list type="bullet"> | |
486 | |
487 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
488 <literal>ngx_atoi(line, n)</literal> — converts a string of given length to a |
1899 | 489 positive integer of type <literal>ngx_int_t</literal>. |
490 Returns <literal>NGX_ERROR</literal> on error | |
491 </listitem> | |
492 | |
493 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
494 <literal>ngx_atosz(line, n)</literal> — same for <literal>ssize_t</literal> |
1899 | 495 type |
496 </listitem> | |
497 | |
498 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
499 <literal>ngx_atoof(line, n)</literal> — same for <literal>off_t</literal> |
1899 | 500 type |
501 </listitem> | |
502 | |
503 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
504 <literal>ngx_atotm(line, n)</literal> — same for <literal>time_t</literal> |
1899 | 505 type |
506 </listitem> | |
507 | |
508 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
509 <literal>ngx_atofp(line, n, point)</literal> — converts a fixed point floating |
1899 | 510 number of given length to a positive integer of type |
511 <literal>ngx_int_t</literal>. | |
512 The result is shifted left by <literal>points</literal> decimal | |
513 positions. The string representation of the number is expected to have no more | |
514 than <literal>points</literal> fractional digits. | |
515 Returns <literal>NGX_ERROR</literal> on error. For example, | |
516 <literal>ngx_atofp("10.5", 4, 2)</literal> returns <literal>1050</literal> | |
517 </listitem> | |
518 | |
519 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
520 <literal>ngx_hextoi(line, n)</literal> — converts hexadecimal representation of |
1899 | 521 a positive integer to <literal>ngx_int_t</literal>. Returns |
522 <literal>NGX_ERROR</literal> on error | |
523 </listitem> | |
524 | |
525 </list> | |
526 </para> | |
527 | |
528 </section> | |
529 | |
1919
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
530 <section name="Regular expressions" id="regex"> |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
531 |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
532 <para> |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
533 The regular expressions interface in nginx is a wrapper around |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
534 the <link url="http://www.pcre.org">PCRE</link> |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
535 library. |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
536 The corresponding header file is <path>src/core/ngx_regex.h</path>. |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
537 </para> |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
538 |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
539 <para> |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
540 To use a regular expression for string matching, first, it needs to be |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
541 compiled, this is usually done at configuration phase. |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
542 Note that since PCRE support is optional, all code using the interface must |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
543 be protected by the surrounding <literal>NGX_PCRE</literal> macro: |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
544 <programlisting> |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
545 #if (NGX_PCRE) |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
546 ngx_regex_t *re; |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
547 ngx_regex_compile_t rc; |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
548 |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
549 u_char errstr[NGX_MAX_CONF_ERRSTR]; |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
550 |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
551 ngx_str_t value = ngx_string("message (\\d\\d\\d).*Codeword is '(?<cw>\\w+)'"); |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
552 |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
553 ngx_memzero(&rc, sizeof(ngx_regex_compile_t)); |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
554 |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
555 rc.pattern = value; |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
556 rc.pool = cf->pool; |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
557 rc.err.len = NGX_MAX_CONF_ERRSTR; |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
558 rc.err.data = errstr; |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
559 /* rc.options are passed as is to pcre_compile() */ |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
560 |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
561 if (ngx_regex_compile(&rc) != NGX_OK) { |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
562 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "%V", &rc.err); |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
563 return NGX_CONF_ERROR; |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
564 } |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
565 |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
566 re = rc.regex; |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
567 #endif |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
568 </programlisting> |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
569 After successful compilation, <literal>ngx_regex_compile_t</literal> structure |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
570 fields <literal>captures</literal> and <literal>named_captures</literal> |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
571 are filled with count of all and named captures respectively found in the |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
572 regular expression. |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
573 </para> |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
574 |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
575 <para> |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
576 Later, the compiled regular expression may be used to match strings against it: |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
577 <programlisting> |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
578 ngx_int_t n; |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
579 int captures[(1 + rc.captures) * 3]; |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
580 |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
581 ngx_str_t input = ngx_string("This is message 123. Codeword is 'foobar'."); |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
582 |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
583 n = ngx_regex_exec(re, &input, captures, (1 + rc.captures) * 3); |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
584 if (n >= 0) { |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
585 /* string matches expression */ |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
586 |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
587 } else if (n == NGX_REGEX_NO_MATCHED) { |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
588 /* no match was found */ |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
589 |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
590 } else { |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
591 /* some error */ |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
592 ngx_log_error(NGX_LOG_ALERT, log, 0, ngx_regex_exec_n " failed: %i", n); |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
593 } |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
594 </programlisting> |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
595 The arguments of <literal>ngx_regex_exec()</literal> are: the compiled regular |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
596 expression <literal>re</literal>, string to match <literal>s</literal>, |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
597 optional array of integers to hold found <literal>captures</literal> |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
598 and its <literal>size</literal>. |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
599 The <literal>captures</literal> array size must be a multiple of three, |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
600 per requirements of the |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
601 <link url="http://www.pcre.org/original/doc/html/pcreapi.html">PCRE API</link>. |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
602 In the example, its size is calculated from a total number of captures plus |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
603 one for the matched string itself. |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
604 </para> |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
605 |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
606 <para> |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
607 Now, if there are matches, captures may be accessed: |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
608 <programlisting> |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
609 u_char *p; |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
610 size_t size; |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
611 ngx_str_t name, value; |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
612 |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
613 /* all captures */ |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
614 for (i = 0; i < n * 2; i += 2) { |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
615 value.data = input.data + captures[i]; |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
616 value.len = captures[i + 1] — captures[i]; |
1919
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
617 } |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
618 |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
619 /* accessing named captures */ |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
620 |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
621 size = rc.name_size; |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
622 p = rc.names; |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
623 |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
624 for (i = 0; i < rc.named_captures; i++, p += size) { |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
625 |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
626 /* capture name */ |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
627 name.data = &p[2]; |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
628 name.len = ngx_strlen(name.data); |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
629 |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
630 n = 2 * ((p[0] << 8) + p[1]); |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
631 |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
632 /* captured value */ |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
633 value.data = &input.data[captures[n]]; |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
634 value.len = captures[n + 1] — captures[n]; |
1919
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
635 } |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
636 </programlisting> |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
637 </para> |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
638 |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
639 <para> |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
640 The <literal>ngx_regex_exec_array()</literal> function accepts the array of |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
641 <literal>ngx_regex_elt_t</literal> elements (which are just compiled regular |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
642 expressions with associated names), a string to match and a log. |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
643 The function will apply expressions from the array to the string until |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
644 the match is found or no more expressions are left. |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
645 The return value is <literal>NGX_OK</literal> in case of match and |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
646 <literal>NGX_DECLINED</literal> otherwise, or <literal>NGX_ERROR</literal> |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
647 in case of error. |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
648 </para> |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
649 |
dcfb4f3ac8a7
Added the "Regular expressions" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1915
diff
changeset
|
650 </section> |
1899 | 651 |
652 </section> | |
653 | |
654 | |
1982
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
655 <section name="Time" id="time"> |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
656 |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
657 <para> |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
658 The <literal>ngx_time_t</literal> structure represents time split into seconds |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
659 and milliseconds with specification of GMT offset: |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
660 <programlisting> |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
661 typedef struct { |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
662 time_t sec; |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
663 ngx_uint_t msec; |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
664 ngx_int_t gmtoff; |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
665 } ngx_time_t; |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
666 </programlisting> |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
667 The <literal>ngx_tm_t</literal> is an alias for <literal>struct tm</literal> |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
668 on UNIX platforms and <literal>SYSTEMTIME</literal> on Windows. |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
669 </para> |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
670 |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
671 <para> |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
672 To obtain current time, usually it is enough to access one of available global |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
673 variables, representing the cached time value in desired format. |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
674 The <literal>ngx_current_msec</literal> variable holds milliseconds elapsed |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
675 since Epoch and truncated to <literal>ngx_msec_t</literal>. |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
676 </para> |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
677 |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
678 <para> |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
679 Available string representations are: |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
680 |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
681 <list type="bullet"> |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
682 |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
683 <listitem> |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
684 <literal>ngx_cached_err_log_time</literal> — used in error log: |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
685 "<literal>1970/09/28 12:00:00</literal>" |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
686 </listitem> |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
687 |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
688 <listitem> |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
689 <literal>ngx_cached_http_log_time</literal> — used in HTTP access log: |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
690 "<literal>28/Sep/1970:12:00:00 +0600</literal>" |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
691 </listitem> |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
692 |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
693 <listitem> |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
694 <literal>ngx_cached_syslog_time</literal> — used in syslog: |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
695 "<literal>Sep 28 12:00:00</literal>" |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
696 </listitem> |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
697 |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
698 <listitem> |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
699 <literal>ngx_cached_http_time</literal> — used in HTTP for headers: |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
700 "<literal>Mon, 28 Sep 1970 06:00:00 GMT</literal>" |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
701 </listitem> |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
702 |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
703 <listitem> |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
704 <literal>ngx_cached_http_log_iso8601</literal> — in the ISO 8601 |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
705 standard format: |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
706 "<literal>1970-09-28T12:00:00+06:00</literal>" |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
707 </listitem> |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
708 |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
709 </list> |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
710 </para> |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
711 |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
712 <para> |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
713 The <literal>ngx_time()</literal> and <literal>ngx_timeofday()</literal> macros |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
714 returning current value of seconds are a preferred way to access cached |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
715 time value. |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
716 </para> |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
717 |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
718 <para> |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
719 To obtain the time explicitly, <literal>ngx_gettimeofday()</literal> may |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
720 be used, which updates its argument (pointer to |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
721 <literal>struct timeval</literal>). |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
722 Time is always updated when nginx returns to event loop from system calls. |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
723 To update the time immediately, call <literal>ngx_time_update()</literal>, |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
724 or <literal>ngx_time_sigsafe_update()</literal> if you need it in the |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
725 signal handler context. |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
726 </para> |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
727 |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
728 <para> |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
729 The following functions convert <literal>time_t</literal> into broken-down time |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
730 representation, either <literal>ngx_tm_t</literal> or |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
731 <literal>struct tm</literal> for those with <literal>libc</literal> prefix: |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
732 |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
733 <list type="bullet"> |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
734 |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
735 <listitem> |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
736 <literal>ngx_gmtime(), ngx_libc_gmtime()</literal> — result time is UTC |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
737 </listitem> |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
738 |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
739 <listitem> |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
740 <literal>ngx_localtime(), ngx_libc_localtime()</literal> — result time is |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
741 relative to the timezone |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
742 </listitem> |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
743 |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
744 </list> |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
745 |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
746 The <literal>ngx_http_time(buf, time)</literal> returns string |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
747 representation suitable for use with HTTP headers (for example, |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
748 "<literal>Mon, 28 Sep 1970 06:00:00 GMT</literal>"). |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
749 Another possible conversion is provided by |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
750 <literal>ngx_http_cookie_time(buf, time)</literal> that produces format suitable |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
751 for HTTP cookies ("<literal>Thu, 31-Dec-37 23:55:55 GMT</literal>"). |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
752 </para> |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
753 |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
754 </section> |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
755 |
28ee7ab54a90
Added the "Time" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1981
diff
changeset
|
756 |
1899 | 757 <section name="Containers" id="containers"> |
758 | |
759 | |
760 <section name="Array" id="array"> | |
761 | |
762 <para> | |
763 The nginx array type <literal>ngx_array_t</literal> is defined as follows | |
764 </para> | |
765 | |
766 | |
767 <programlisting> | |
768 typedef struct { | |
769 void *elts; | |
770 ngx_uint_t nelts; | |
771 size_t size; | |
772 ngx_uint_t nalloc; | |
773 ngx_pool_t *pool; | |
774 } ngx_array_t; | |
775 </programlisting> | |
776 | |
777 <para> | |
778 The elements of array are available through the <literal>elts</literal> field. | |
779 The number of elements is held in the <literal>nelts</literal> field. | |
780 The <literal>size</literal> field holds the size of a single element and is set | |
781 when initializing the array. | |
782 </para> | |
783 | |
784 <para> | |
785 An array can be created in a pool with the | |
786 <literal>ngx_array_create(pool, n, size)</literal> call. | |
787 An already allocated array object can be initialized with the | |
788 <literal>ngx_array_init(array, pool, n, size)</literal> call. | |
789 </para> | |
790 | |
791 | |
792 <programlisting> | |
793 ngx_array_t *a, b; | |
794 | |
795 /* create an array of strings with preallocated memory for 10 elements */ | |
796 a = ngx_array_create(pool, 10, sizeof(ngx_str_t)); | |
797 | |
798 /* initialize string array for 10 elements */ | |
799 ngx_array_init(&b, pool, 10, sizeof(ngx_str_t)); | |
800 </programlisting> | |
801 | |
802 <para> | |
803 Adding elements to array are done with the following functions: | |
804 </para> | |
805 | |
806 <para> | |
807 <list type="bullet"> | |
808 | |
809 <listitem> | |
810 <literal>ngx_array_push(a)</literal> adds one tail element and returns pointer | |
811 to it | |
812 </listitem> | |
813 | |
814 <listitem> | |
815 <literal>ngx_array_push_n(a, n)</literal> adds <literal>n</literal> tail elements | |
816 and returns pointer to the first one | |
817 </listitem> | |
818 | |
819 </list> | |
820 </para> | |
821 | |
822 <para> | |
823 If currently allocated memory is not enough for new elements, a new memory for | |
824 elements is allocated and existing elements are copied to that memory. | |
825 The new memory block is normally twice as large, as the existing one. | |
826 </para> | |
827 | |
828 | |
829 <programlisting> | |
830 s = ngx_array_push(a); | |
831 ss = ngx_array_push_n(&b, 3); | |
832 </programlisting> | |
833 | |
834 </section> | |
835 | |
836 | |
837 <section name="List" id="list"> | |
838 | |
839 <para> | |
840 List in nginx is a sequence of arrays, optimized for inserting a potentially | |
841 large number of items. The list type is defined as follows: | |
842 </para> | |
843 | |
844 | |
845 <programlisting> | |
846 typedef struct { | |
847 ngx_list_part_t *last; | |
848 ngx_list_part_t part; | |
849 size_t size; | |
850 ngx_uint_t nalloc; | |
851 ngx_pool_t *pool; | |
852 } ngx_list_t; | |
853 </programlisting> | |
854 | |
855 <para> | |
1958 | 856 The actual items are stored in list parts, defined as follows: |
1899 | 857 </para> |
858 | |
859 | |
860 <programlisting> | |
861 typedef struct ngx_list_part_s ngx_list_part_t; | |
862 | |
863 struct ngx_list_part_s { | |
864 void *elts; | |
865 ngx_uint_t nelts; | |
866 ngx_list_part_t *next; | |
867 }; | |
868 </programlisting> | |
869 | |
870 <para> | |
871 Initially, a list must be initialized by calling | |
872 <literal>ngx_list_init(list, pool, n, size)</literal> or created by calling | |
873 <literal>ngx_list_create(pool, n, size)</literal>. | |
874 Both functions receive the size of a single item and a number of items per list | |
875 part. | |
876 The <literal>ngx_list_push(list)</literal> function is used to add an item to the | |
877 list. Iterating over the items is done by direct accessing the list fields, as | |
878 seen in the example: | |
879 </para> | |
880 | |
881 | |
882 <programlisting> | |
883 ngx_str_t *v; | |
884 ngx_uint_t i; | |
885 ngx_list_t *list; | |
886 ngx_list_part_t *part; | |
887 | |
888 list = ngx_list_create(pool, 100, sizeof(ngx_str_t)); | |
889 if (list == NULL) { /* error */ } | |
890 | |
891 /* add items to the list */ | |
892 | |
893 v = ngx_list_push(list); | |
894 if (v == NULL) { /* error */ } | |
1936
8f996938fe23
Style: proper quotes usage.
Vladimir Homutov <vl@nginx.com>
parents:
1935
diff
changeset
|
895 ngx_str_set(v, "foo"); |
1899 | 896 |
897 v = ngx_list_push(list); | |
898 if (v == NULL) { /* error */ } | |
1936
8f996938fe23
Style: proper quotes usage.
Vladimir Homutov <vl@nginx.com>
parents:
1935
diff
changeset
|
899 ngx_str_set(v, "bar"); |
1899 | 900 |
901 /* iterate over the list */ | |
902 | |
903 part = &list->part; | |
904 v = part->elts; | |
905 | |
906 for (i = 0; /* void */; i++) { | |
907 | |
908 if (i >= part->nelts) { | |
909 if (part->next == NULL) { | |
910 break; | |
911 } | |
912 | |
913 part = part->next; | |
914 v = part->elts; | |
915 i = 0; | |
916 } | |
917 | |
918 ngx_do_smth(&v[i]); | |
919 } | |
920 </programlisting> | |
921 | |
922 <para> | |
923 The primary use for the list in nginx is HTTP input and output headers. | |
924 </para> | |
925 | |
926 <para> | |
927 The list does not support item removal. | |
928 However, when needed, items can internally be marked as missing without actual | |
929 removing from the list. | |
930 For example, HTTP output headers which are stored as | |
931 <literal>ngx_table_elt_t</literal> objects, are marked as missing by setting | |
932 the <literal>hash</literal> field of <literal>ngx_table_elt_t</literal> to | |
933 zero. Such items are explicitly skipped, when iterating over the headers. | |
934 </para> | |
935 | |
936 </section> | |
937 | |
938 | |
939 <section name="Queue" id="queue"> | |
940 | |
941 <para> | |
942 Queue in nginx is an intrusive doubly linked list, with each node defined as | |
943 follows: | |
944 </para> | |
945 | |
946 | |
947 <programlisting> | |
948 typedef struct ngx_queue_s ngx_queue_t; | |
949 | |
950 struct ngx_queue_s { | |
951 ngx_queue_t *prev; | |
952 ngx_queue_t *next; | |
953 }; | |
954 </programlisting> | |
955 | |
956 <para> | |
957 The head queue node is not linked with any data. Before using, the list head | |
958 should be initialized with <literal>ngx_queue_init(q)</literal> call. | |
959 Queues support the following operations: | |
960 </para> | |
961 | |
962 <para> | |
963 <list type="bullet"> | |
964 | |
965 <listitem> | |
966 <literal>ngx_queue_insert_head(h, x)</literal>, | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
967 <literal>ngx_queue_insert_tail(h, x)</literal> — insert a new node |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
968 </listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
969 |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
970 <listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
971 <literal>ngx_queue_remove(x)</literal> — remove a queue node |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
972 </listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
973 |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
974 <listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
975 <literal>ngx_queue_split(h, q, n)</literal> — split a queue at a node, |
1899 | 976 queue tail is returned in a separate queue |
977 </listitem> | |
978 | |
979 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
980 <literal>ngx_queue_add(h, n)</literal> — add second queue to the first queue |
1899 | 981 </listitem> |
982 | |
983 <listitem> | |
984 <literal>ngx_queue_head(h)</literal>, | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
985 <literal>ngx_queue_last(h)</literal> — get first or last queue node |
1899 | 986 </listitem> |
987 | |
988 <listitem> | |
989 <literal>ngx_queue_sentinel(h)</literal> | |
990 - get a queue sentinel object to end iteration at | |
991 </listitem> | |
992 | |
993 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
994 <literal>ngx_queue_data(q, type, link)</literal> — get reference to the beginning of a |
1899 | 995 queue node data structure, considering the queue field offset in it |
996 </listitem> | |
997 | |
998 </list> | |
999 </para> | |
1000 | |
1001 <para> | |
1002 Example: | |
1003 </para> | |
1004 | |
1005 | |
1006 <programlisting> | |
1007 typedef struct { | |
1008 ngx_str_t value; | |
1009 ngx_queue_t queue; | |
1010 } ngx_foo_t; | |
1011 | |
1012 ngx_foo_t *f; | |
1013 ngx_queue_t values; | |
1014 | |
1015 ngx_queue_init(&values); | |
1016 | |
1017 f = ngx_palloc(pool, sizeof(ngx_foo_t)); | |
1018 if (f == NULL) { /* error */ } | |
1936
8f996938fe23
Style: proper quotes usage.
Vladimir Homutov <vl@nginx.com>
parents:
1935
diff
changeset
|
1019 ngx_str_set(&f->value, "foo"); |
1899 | 1020 |
1021 ngx_queue_insert_tail(&values, f); | |
1022 | |
1023 /* insert more nodes here */ | |
1024 | |
1025 for (q = ngx_queue_head(&values); | |
1026 q != ngx_queue_sentinel(&values); | |
1027 q = ngx_queue_next(q)) | |
1028 { | |
1029 f = ngx_queue_data(q, ngx_foo_t, queue); | |
1030 | |
1031 ngx_do_smth(&f->value); | |
1032 } | |
1033 </programlisting> | |
1034 | |
1035 </section> | |
1036 | |
1037 | |
1038 <section name="Red-Black tree" id="red_black_tree"> | |
1039 | |
1040 <para> | |
1915
8b7c3b0ef1a4
Semantically marked paths.
Vladimir Homutov <vl@nginx.com>
parents:
1914
diff
changeset
|
1041 The <path>src/core/ngx_rbtree.h</path> header file provides access to the |
1899 | 1042 effective implementation of red-black trees. |
1043 </para> | |
1044 | |
1045 | |
1046 <programlisting> | |
1047 typedef struct { | |
1048 ngx_rbtree_t rbtree; | |
1049 ngx_rbtree_node_t sentinel; | |
1050 | |
1051 /* custom per-tree data here */ | |
1052 } my_tree_t; | |
1053 | |
1054 typedef struct { | |
1055 ngx_rbtree_node_t rbnode; | |
1056 | |
1057 /* custom per-node data */ | |
1058 foo_t val; | |
1059 } my_node_t; | |
1060 </programlisting> | |
1061 | |
1062 <para> | |
1063 To deal with a tree as a whole, you need two nodes: root and sentinel. | |
1064 Typically, they are added to some custom structure, thus allowing to | |
1065 organize your data into a tree which leaves contain a link to or embed | |
1066 your data. | |
1067 </para> | |
1068 | |
1069 <para> | |
1070 To initialize a tree: | |
1071 </para> | |
1072 | |
1073 | |
1074 <programlisting> | |
1075 my_tree_t root; | |
1076 | |
1077 ngx_rbtree_init(&root.rbtree, &root.sentinel, insert_value_function); | |
1078 </programlisting> | |
1079 | |
1080 <para> | |
1081 The <literal>insert_value_function</literal> is a function that is | |
1082 responsible for traversing the tree and inserting new values into correct | |
1083 place. | |
1084 For example, the <literal>ngx_str_rbtree_insert_value</literal> functions is | |
1085 designed to deal with <literal>ngx_str_t</literal> type. | |
1086 </para> | |
1087 | |
1088 | |
1089 <programlisting> | |
1090 void ngx_str_rbtree_insert_value(ngx_rbtree_node_t *temp, | |
1091 ngx_rbtree_node_t *node, | |
1092 ngx_rbtree_node_t *sentinel) | |
1093 </programlisting> | |
1094 | |
1095 <para> | |
1096 Its arguments are pointers to a root node of an insertion, newly created node | |
1097 to be added, and a tree sentinel. | |
1098 </para> | |
1099 | |
1100 <para> | |
1101 The traversal is pretty straightforward and can be demonstrated with the | |
1102 following lookup function pattern: | |
1103 </para> | |
1104 | |
1105 | |
1106 <programlisting> | |
1107 my_node_t * | |
1108 my_rbtree_lookup(ngx_rbtree_t *rbtree, foo_t *val, uint32_t hash) | |
1109 { | |
1110 ngx_int_t rc; | |
1111 my_node_t *n; | |
1112 ngx_rbtree_node_t *node, *sentinel; | |
1113 | |
1114 node = rbtree->root; | |
1115 sentinel = rbtree->sentinel; | |
1116 | |
1117 while (node != sentinel) { | |
1118 | |
1119 n = (my_node_t *) node; | |
1120 | |
1121 if (hash != node->key) { | |
1122 node = (hash < node->key) ? node->left : node->right; | |
1123 continue; | |
1124 } | |
1125 | |
1126 rc = compare(val, node->val); | |
1127 | |
1128 if (rc < 0) { | |
1129 node = node->left; | |
1130 continue; | |
1131 } | |
1132 | |
1133 if (rc > 0) { | |
1134 node = node->right; | |
1135 continue; | |
1136 } | |
1137 | |
1138 return n; | |
1139 } | |
1140 | |
1141 return NULL; | |
1142 } | |
1143 </programlisting> | |
1144 | |
1145 <para> | |
1146 The <literal>compare()</literal> is a classic comparator function returning | |
1147 value less, equal or greater than zero. To speed up lookups and avoid comparing | |
1148 user objects that can be big, integer hash field is used. | |
1149 </para> | |
1150 | |
1151 <para> | |
1152 To add a node to a tree, allocate a new node, initialize it and call | |
1153 <literal>ngx_rbtree_insert()</literal>: | |
1154 </para> | |
1155 | |
1156 | |
1157 <programlisting> | |
1158 my_node_t *my_node; | |
1159 ngx_rbtree_node_t *node; | |
1160 | |
1161 my_node = ngx_palloc(...); | |
1162 init_custom_data(&my_node->val); | |
1163 | |
1164 node = &my_node->rbnode; | |
1165 node->key = create_key(my_node->val); | |
1166 | |
1167 ngx_rbtree_insert(&root->rbtree, node); | |
1168 </programlisting> | |
1169 | |
1170 <para> | |
1171 to remove a node: | |
1172 </para> | |
1173 | |
1174 | |
1175 <programlisting> | |
1176 ngx_rbtree_delete(&root->rbtree, node); | |
1177 </programlisting> | |
1178 | |
1179 </section> | |
1180 | |
1914
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1181 <section name="Hash" id="hash"> |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1182 |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1183 <para> |
1920 | 1184 Hash table functions are declared in <path>src/core/ngx_hash.h</path>. |
1914
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1185 Exact and wildcard matching is supported. |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1186 The latter requires extra setup and is described in a separate section below. |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1187 </para> |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1188 |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1189 <para> |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1190 To initialize a hash, one needs to know the number of elements in advance, |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1191 so that nginx can build the hash optimally. |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1192 Two parameters that need to be configured are <literal>max_size</literal> |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1193 and <literal>bucket_size</literal>. |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1194 The details of setting up these are provided in a separate |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1195 <link doc="../hash.xml">document</link>. |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1196 Usually, these two parameters are configurable by user. |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1197 Hash initialization settings are stored as the |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1198 <literal>ngx_hash_init_t</literal> type, |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1199 and the hash itself is <literal>ngx_hash_t</literal>: |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1200 <programlisting> |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1201 ngx_hash_t foo_hash; |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1202 ngx_hash_init_t hash; |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1203 |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1204 hash.hash = &foo_hash; |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1205 hash.key = ngx_hash_key; |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1206 hash.max_size = 512; |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1207 hash.bucket_size = ngx_align(64, ngx_cacheline_size); |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1208 hash.name = "foo_hash"; |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1209 hash.pool = cf->pool; |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1210 hash.temp_pool = cf->temp_pool; |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1211 </programlisting> |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1212 The <literal>key</literal> is a pointer to a function that creates hash integer |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1213 key from a string. |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1214 Two generic functions are provided: |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1215 <literal>ngx_hash_key(data, len)</literal> and |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1216 <literal>ngx_hash_key_lc(data, len)</literal>. |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1217 The latter converts a string to lowercase and thus requires the passed string to |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1218 be writable. |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1219 If this is not true, <literal>NGX_HASH_READONLY_KEY</literal> flag |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1220 may be passed to the function, initializing array keys (see below). |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1221 </para> |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1222 |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1223 <para> |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1224 The hash keys are stored in <literal>ngx_hash_keys_arrays_t</literal> and |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1225 are initialized with <literal>ngx_hash_keys_array_init(arr, type)</literal>: |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1226 <programlisting> |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1227 ngx_hash_keys_arrays_t foo_keys; |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1228 |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1229 foo_keys.pool = cf->pool; |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1230 foo_keys.temp_pool = cf->temp_pool; |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1231 |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1232 ngx_hash_keys_array_init(&foo_keys, NGX_HASH_SMALL); |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1233 </programlisting> |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1234 The second parameter can be either <literal>NGX_HASH_SMALL</literal> or |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1235 <literal>NGX_HASH_LARGE</literal> and controls the amount of preallocated |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1236 resources for the hash. |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1237 If you expect the hash to contain thousands elements, |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1238 use <literal>NGX_HASH_LARGE</literal>. |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1239 </para> |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1240 |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1241 <para> |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1242 The <literal>ngx_hash_add_key(keys_array, key, value, flags)</literal> |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1243 function is used to insert keys into hash keys array; |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1244 <programlisting> |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1245 ngx_str_t k1 = ngx_string("key1"); |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1246 ngx_str_t k2 = ngx_string("key2"); |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1247 |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1248 ngx_hash_add_key(&foo_keys, &k1, &my_data_ptr_1, NGX_HASH_READONLY_KEY); |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1249 ngx_hash_add_key(&foo_keys, &k2, &my_data_ptr_2, NGX_HASH_READONLY_KEY); |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1250 </programlisting> |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1251 </para> |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1252 |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1253 <para> |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1254 Now, the hash table may be built using the call to |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1255 <literal>ngx_hash_init(hinit, key_names, nelts)</literal>: |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1256 |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1257 <programlisting> |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1258 ngx_hash_init(&hash, foo_keys.keys.elts, foo_keys.keys.nelts); |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1259 </programlisting> |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1260 |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1261 This may fail, if <literal>max_size</literal> or <literal>bucket_size</literal> |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1262 parameters are not big enough. |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1263 When the hash is built, <literal>ngx_hash_find(hash, key, name, len)</literal> |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1264 function may be used to look up elements: |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1265 <programlisting> |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1266 my_data_t *data; |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1267 ngx_uint_t key; |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1268 |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1269 key = ngx_hash_key(k1.data, k1.len); |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1270 |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1271 data = ngx_hash_find(&foo_hash, key, k1.data, k1.len); |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1272 if (data == NULL) { |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1273 /* key not found */ |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1274 } |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1275 </programlisting> |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1276 |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1277 </para> |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1278 |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1279 <section name="Wildcard matching" id="wildcard_matching"> |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1280 |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1281 <para> |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1282 To create a hash that works with wildcards, |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1283 <literal>ngx_hash_combined_t</literal> type is used. |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1284 It includes the hash type described above and has two additional keys arrays: |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1285 <literal>dns_wc_head</literal> and <literal>dns_wc_tail</literal>. |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1286 The initialization of basic properties is done similarly to a usual hash: |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1287 <programlisting> |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1288 ngx_hash_init_t hash |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1289 ngx_hash_combined_t foo_hash; |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1290 |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1291 hash.hash = &foo_hash.hash; |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1292 hash.key = ...; |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1293 </programlisting> |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1294 </para> |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1295 |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1296 <para> |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1297 It is possible to add wildcard keys using the |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1298 <literal>NGX_HASH_WILDCARD_KEY</literal> flag: |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1299 <programlisting> |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1300 /* k1 = ".example.org"; */ |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1301 /* k2 = "foo.*"; */ |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1302 ngx_hash_add_key(&foo_keys, &k1, &data1, NGX_HASH_WILDCARD_KEY); |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1303 ngx_hash_add_key(&foo_keys, &k2, &data2, NGX_HASH_WILDCARD_KEY); |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1304 </programlisting> |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1305 The function recognizes wildcards and adds keys into corresponding arrays. |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1306 Please refer to the |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1307 <link doc="../http/ngx_http_map_module.xml" id="map"/> module |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1308 documentation for the description of the wildcard syntax and |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1309 matching algorithm. |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1310 </para> |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1311 |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1312 <para> |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1313 Depending on the contents of added keys, you may need to initialize up to three |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1314 keys arrays: one for exact matching (described above), and two for matching |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1315 starting from head or tail of a string: |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1316 <programlisting> |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1317 if (foo_keys.dns_wc_head.nelts) { |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1318 |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1319 ngx_qsort(foo_keys.dns_wc_head.elts, |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1320 (size_t) foo_keys.dns_wc_head.nelts, |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1321 sizeof(ngx_hash_key_t), |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1322 cmp_dns_wildcards); |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1323 |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1324 hash.hash = NULL; |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1325 hash.temp_pool = pool; |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1326 |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1327 if (ngx_hash_wildcard_init(&hash, foo_keys.dns_wc_head.elts, |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1328 foo_keys.dns_wc_head.nelts) |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1329 != NGX_OK) |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1330 { |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1331 return NGX_ERROR; |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1332 } |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1333 |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1334 foo_hash.wc_head = (ngx_hash_wildcard_t *) hash.hash; |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1335 } |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1336 </programlisting> |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1337 The keys array needs to be sorted, and initialization results must be added |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1338 to the combined hash. |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1339 The initialization of <literal>dns_wc_tail</literal> array is done similarly. |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1340 </para> |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1341 |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1342 <para> |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1343 The lookup in a combined hash is handled by the |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1344 <literal>ngx_hash_find_combined(chash, key, name, len)</literal>: |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1345 <programlisting> |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
1346 /* key = "bar.example.org"; — will match ".example.org" */ |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
1347 /* key = "foo.example.com"; — will match "foo.*" */ |
1914
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1348 |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1349 hkey = ngx_hash_key(key.data, key.len); |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1350 res = ngx_hash_find_combined(&foo_hash, hkey, key.data, key.len); |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1351 </programlisting> |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1352 </para> |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1353 |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1354 </section> |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1355 |
f8659301a260
Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents:
1907
diff
changeset
|
1356 </section> |
1899 | 1357 |
1358 </section> | |
1359 | |
1360 | |
1361 <section name="Memory management" id="memory_management"> | |
1362 | |
1363 | |
1364 <section name="Heap" id="heap"> | |
1365 | |
1366 <para> | |
1367 To allocate memory from system heap, the following functions are provided by | |
1368 nginx: | |
1369 </para> | |
1370 | |
1371 <para> | |
1372 <list type="bullet"> | |
1373 | |
1374 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
1375 <literal>ngx_alloc(size, log)</literal> — allocate memory from system heap. |
1899 | 1376 This is a wrapper around <literal>malloc()</literal> with logging support. |
1377 Allocation error and debugging information is logged to <literal>log</literal> | |
1378 </listitem> | |
1379 | |
1380 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
1381 <literal>ngx_calloc(size, log)</literal> — same as |
1899 | 1382 <literal>ngx_alloc()</literal>, but memory is filled with zeroes after |
1383 allocation | |
1384 </listitem> | |
1385 | |
1386 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
1387 <literal>ngx_memalign(alignment, size, log)</literal> — allocate aligned memory |
1899 | 1388 from system heap. This is a wrapper around <literal>posix_memalign()</literal> |
1389 on those platforms which provide it. | |
1390 Otherwise implementation falls back to <literal>ngx_alloc()</literal> which | |
1391 provides maximum alignment | |
1392 </listitem> | |
1393 | |
1394 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
1395 <literal>ngx_free(p)</literal> — free allocated memory. |
1899 | 1396 This is a wrapper around <literal>free()</literal> |
1397 </listitem> | |
1398 | |
1399 </list> | |
1400 </para> | |
1401 | |
1402 </section> | |
1403 | |
1404 | |
1405 <section name="Pool" id="pool"> | |
1406 | |
1407 <para> | |
1408 Most nginx allocations are done in pools. Memory allocated in an nginx pool is | |
1409 freed automatically when the pool in destroyed. This provides good | |
1410 allocation performance and makes memory control easy. | |
1411 </para> | |
1412 | |
1413 <para> | |
1414 A pool internally allocates objects in continuous blocks of memory. Once a | |
1415 block is full, a new one is allocated and added to the pool memory | |
1416 block list. When a large allocation is requested which does not fit into | |
1417 a block, such allocation is forwarded to the system allocator and the | |
1418 returned pointer is stored in the pool for further deallocation. | |
1419 </para> | |
1420 | |
1421 <para> | |
1422 Nginx pool has the type <literal>ngx_pool_t</literal>. | |
1423 The following operations are supported: | |
1424 </para> | |
1425 | |
1426 <para> | |
1427 <list type="bullet"> | |
1428 | |
1429 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
1430 <literal>ngx_create_pool(size, log)</literal> — create a pool with given |
1899 | 1431 block size. The pool object returned is allocated in the pool as well. |
1432 </listitem> | |
1433 | |
1434 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
1435 <literal>ngx_destroy_pool(pool)</literal> — free all pool memory, including |
1899 | 1436 the pool object itself. |
1437 </listitem> | |
1438 | |
1439 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
1440 <literal>ngx_palloc(pool, size)</literal> — allocate aligned memory from pool |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
1441 </listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
1442 |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
1443 <listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
1444 <literal>ngx_pcalloc(pool, size)</literal> — allocated aligned memory |
1899 | 1445 from pool and fill it with zeroes |
1446 </listitem> | |
1447 | |
1448 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
1449 <literal>ngx_pnalloc(pool, size)</literal> — allocate unaligned memory from pool. |
1899 | 1450 Mostly used for allocating strings |
1451 </listitem> | |
1452 | |
1453 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
1454 <literal>ngx_pfree(pool, p)</literal> — free memory, previously allocated |
1899 | 1455 in the pool. |
1456 Only allocations, forwarded to the system allocator, can be freed. | |
1457 </listitem> | |
1458 | |
1459 </list> | |
1460 </para> | |
1461 | |
1462 <programlisting> | |
1463 u_char *p; | |
1464 ngx_str_t *s; | |
1465 ngx_pool_t *pool; | |
1466 | |
1467 pool = ngx_create_pool(1024, log); | |
1468 if (pool == NULL) { /* error */ } | |
1469 | |
1470 s = ngx_palloc(pool, sizeof(ngx_str_t)); | |
1471 if (s == NULL) { /* error */ } | |
1936
8f996938fe23
Style: proper quotes usage.
Vladimir Homutov <vl@nginx.com>
parents:
1935
diff
changeset
|
1472 ngx_str_set(s, "foo"); |
1899 | 1473 |
1474 p = ngx_pnalloc(pool, 3); | |
1475 if (p == NULL) { /* error */ } | |
1936
8f996938fe23
Style: proper quotes usage.
Vladimir Homutov <vl@nginx.com>
parents:
1935
diff
changeset
|
1476 ngx_memcpy(p, "foo", 3); |
1899 | 1477 </programlisting> |
1478 | |
1479 <para> | |
1480 Since chain links <literal>ngx_chain_t</literal> are actively used in nginx, | |
1481 nginx pool provides a way to reuse them. | |
1482 The <literal>chain</literal> field of <literal>ngx_pool_t</literal> keeps a | |
1483 list of previously allocated links ready for reuse. For efficient allocation of | |
1484 a chain link in a pool, the function | |
1485 <literal>ngx_alloc_chain_link(pool)</literal> should be used. | |
1486 This function looks up a free chain link in the pool list and only if it's | |
1487 empty allocates a new one. To free a link <literal>ngx_free_chain(pool, cl)</literal> | |
1488 should be called. | |
1489 </para> | |
1490 | |
1491 <para> | |
1492 Cleanup handlers can be registered in a pool. | |
1493 Cleanup handler is a callback with an argument which is called when pool is | |
1494 destroyed. | |
1495 Pool is usually tied with a specific nginx object (like HTTP request) and | |
1496 destroyed in the end of that object’s lifetime, releasing the object itself. | |
1902 | 1497 Registering a pool cleanup is a convenient way to release resources, close file |
1899 | 1498 descriptors or make final adjustments to shared data, associated with the main |
1499 object. | |
1500 </para> | |
1501 | |
1502 <para> | |
1503 A pool cleanup is registered by calling <literal>ngx_pool_cleanup_add(pool, | |
1504 size)</literal> which returns <literal>ngx_pool_cleanup_t</literal> pointer to | |
1505 be filled by the caller. The <literal>size</literal> argument allows allocating | |
1506 context for the cleanup handler. | |
1507 </para> | |
1508 | |
1509 | |
1510 <programlisting> | |
1511 ngx_pool_cleanup_t *cln; | |
1512 | |
1513 cln = ngx_pool_cleanup_add(pool, 0); | |
1514 if (cln == NULL) { /* error */ } | |
1515 | |
1516 cln->handler = ngx_my_cleanup; | |
1936
8f996938fe23
Style: proper quotes usage.
Vladimir Homutov <vl@nginx.com>
parents:
1935
diff
changeset
|
1517 cln->data = "foo"; |
1899 | 1518 |
1519 ... | |
1520 | |
1521 static void | |
1522 ngx_my_cleanup(void *data) | |
1523 { | |
1524 u_char *msg = data; | |
1525 | |
1526 ngx_do_smth(msg); | |
1527 } | |
1528 </programlisting> | |
1529 | |
1530 </section> | |
1531 | |
1532 | |
1533 <section name="Shared memory" id="shared_memory"> | |
1534 | |
1535 <para> | |
1536 Shared memory is used by nginx to share common data between processes. | |
1537 Function <literal>ngx_shared_memory_add(cf, name, size, tag)</literal> adds a | |
1538 new shared memory entry <literal>ngx_shm_zone_t</literal> to the cycle. The | |
1539 function receives <literal>name</literal> and <literal>size</literal> of the | |
1540 zone. | |
1541 Each shared zone must have a unique name. | |
1542 If a shared zone entry with the provided name exists, the old zone entry is | |
1543 reused, if its tag value matches too. | |
1544 Mismatched tag is considered an error. | |
1545 Usually, the address of the module structure is passed as tag, making it | |
1546 possible to reuse shared zones by name within one nginx module. | |
1547 </para> | |
1548 | |
1549 <para> | |
1550 The shared memory entry structure <literal>ngx_shm_zone_t</literal> has the | |
1551 following fields: | |
1552 </para> | |
1553 | |
1554 <para> | |
1555 <list type="bullet"> | |
1556 | |
1557 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
1558 <literal>init</literal> — initialization callback, called after shared zone is |
1899 | 1559 mapped to actual memory |
1560 </listitem> | |
1561 | |
1562 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
1563 <literal>data</literal> — data context, used to pass arbitrary data to the |
1899 | 1564 <literal>init</literal> callback |
1565 </listitem> | |
1566 | |
1567 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
1568 <literal>noreuse</literal> — flag, disabling shared zone reuse from the |
1899 | 1569 old cycle |
1570 </listitem> | |
1571 | |
1572 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
1573 <literal>tag</literal> — shared zone tag |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
1574 </listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
1575 |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
1576 <listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
1577 <literal>shm</literal> — platform-specific object of type |
1899 | 1578 <literal>ngx_shm_t</literal>, having at least the following fields: |
1579 <list type="bullet"> | |
1580 | |
1581 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
1582 <literal>addr</literal> — mapped shared memory address, initially NULL |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
1583 </listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
1584 |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
1585 <listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
1586 <literal>size</literal> — shared memory size |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
1587 </listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
1588 |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
1589 <listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
1590 <literal>name</literal> — shared memory name |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
1591 </listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
1592 |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
1593 <listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
1594 <literal>log</literal> — shared memory log |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
1595 </listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
1596 |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
1597 <listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
1598 <literal>exists</literal> — flag, showing that shared memory was inherited |
1899 | 1599 from the master process (Windows-specific) |
1600 </listitem> | |
1601 | |
1602 </list> | |
1603 </listitem> | |
1604 | |
1605 </list> | |
1606 </para> | |
1607 | |
1608 <para> | |
1609 Shared zone entries are mapped to actual memory in | |
1610 <literal>ngx_init_cycle()</literal> after configuration is parsed. | |
1611 On POSIX systems, <literal>mmap()</literal> syscall is used to create shared | |
1612 anonymous mapping. | |
1613 On Windows, <literal>CreateFileMapping()/MapViewOfFileEx()</literal> pair is | |
1614 used. | |
1615 </para> | |
1616 | |
1617 <para> | |
1618 For allocating in shared memory, nginx provides slab pool | |
1619 <literal>ngx_slab_pool_t</literal>. | |
1620 In each nginx shared zone, a slab pool is automatically created for allocating | |
1621 memory in that zone. | |
1622 The pool is located in the beginning of the shared zone and can be accessed by | |
1623 the expression <literal>(ngx_slab_pool_t *) shm_zone->shm.addr</literal>. | |
1624 Allocation in shared zone is done by calling one of the functions | |
1625 <literal>ngx_slab_alloc(pool, size)/ngx_slab_calloc(pool, size)</literal>. | |
1626 Memory is freed by calling <literal>ngx_slab_free(pool, p)</literal>. | |
1627 </para> | |
1628 | |
1629 <para> | |
1630 Slab pool divides all shared zone into pages. | |
1631 Each page is used for allocating objects of the same size. | |
1632 Only the sizes which are powers of 2, and not less than 8, are considered. | |
1633 Other sizes are rounded up to one of these values. | |
1634 For each page, a bitmask is kept, showing which blocks within that page are in | |
1635 use and which are free for allocation. | |
1636 For sizes greater than half-page (usually, 2048 bytes), allocation is done by | |
1637 entire pages. | |
1638 </para> | |
1639 | |
1640 <para> | |
1641 To protect data in shared memory from concurrent access, mutex is available in | |
1642 the <literal>mutex</literal> field of <literal>ngx_slab_pool_t</literal>. | |
1643 The mutex is used by the slab pool while allocating and freeing memory. | |
1644 However, it can be used to protect any other user data structures, | |
1645 allocated in the shared zone. | |
1646 Locking is done by calling | |
1647 <literal>ngx_shmtx_lock(&shpool->mutex)</literal>, unlocking is done by | |
1648 calling <literal>ngx_shmtx_unlock(&shpool->mutex)</literal>. | |
1649 </para> | |
1650 | |
1651 | |
1652 <programlisting> | |
1653 ngx_str_t name; | |
1654 ngx_foo_ctx_t *ctx; | |
1655 ngx_shm_zone_t *shm_zone; | |
1656 | |
1657 ngx_str_set(&name, "foo"); | |
1658 | |
1659 /* allocate shared zone context */ | |
1660 ctx = ngx_pcalloc(cf->pool, sizeof(ngx_foo_ctx_t)); | |
1661 if (ctx == NULL) { | |
1662 /* error */ | |
1663 } | |
1664 | |
1665 /* add an entry for 65k shared zone */ | |
1666 shm_zone = ngx_shared_memory_add(cf, &name, 65536, &ngx_foo_module); | |
1667 if (shm_zone == NULL) { | |
1668 /* error */ | |
1669 } | |
1670 | |
1671 /* register init callback and context */ | |
1672 shm_zone->init = ngx_foo_init_zone; | |
1673 shm_zone->data = ctx; | |
1674 | |
1675 | |
1676 ... | |
1677 | |
1678 | |
1679 static ngx_int_t | |
1680 ngx_foo_init_zone(ngx_shm_zone_t *shm_zone, void *data) | |
1681 { | |
1682 ngx_foo_ctx_t *octx = data; | |
1683 | |
1684 size_t len; | |
1685 ngx_foo_ctx_t *ctx; | |
1686 ngx_slab_pool_t *shpool; | |
1687 | |
1688 value = shm_zone->data; | |
1689 | |
1690 if (octx) { | |
1691 /* reusing a shared zone from old cycle */ | |
1692 ctx->value = octx->value; | |
1693 return NGX_OK; | |
1694 } | |
1695 | |
1696 shpool = (ngx_slab_pool_t *) shm_zone->shm.addr; | |
1697 | |
1698 if (shm_zone->shm.exists) { | |
1699 /* initialize shared zone context in Windows nginx worker */ | |
1700 ctx->value = shpool->data; | |
1701 return NGX_OK; | |
1702 } | |
1703 | |
1704 /* initialize shared zone */ | |
1705 | |
1706 ctx->value = ngx_slab_alloc(shpool, sizeof(ngx_uint_t)); | |
1707 if (ctx->value == NULL) { | |
1708 return NGX_ERROR; | |
1709 } | |
1710 | |
1711 shpool->data = ctx->value; | |
1712 | |
1713 return NGX_OK; | |
1714 } | |
1715 </programlisting> | |
1716 | |
1717 </section> | |
1718 | |
1719 | |
1720 </section> | |
1721 | |
1722 | |
1723 <section name="Logging" id="logging"> | |
1724 | |
1725 <para> | |
1726 For logging nginx code uses <literal>ngx_log_t</literal> objects. | |
1727 Nginx logger provides support for several types of output: | |
1728 | |
1729 <list type="bullet"> | |
1730 | |
1731 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
1732 stderr — logging to standard error output |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
1733 </listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
1734 |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
1735 <listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
1736 file — logging to file |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
1737 </listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
1738 |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
1739 <listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
1740 syslog — logging to syslog |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
1741 </listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
1742 |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
1743 <listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
1744 memory — logging to internal memory storage for development purposes. |
1899 | 1745 The memory could be accessed later with debugger |
1746 </listitem> | |
1747 | |
1748 </list> | |
1749 </para> | |
1750 | |
1751 <para> | |
1752 A logger instance may actually be a chain of loggers, linked to each other with | |
1753 the <literal>next</literal> field. | |
1754 Each message is written to all loggers in chain. | |
1755 </para> | |
1756 | |
1757 <para> | |
1758 Each logger has an error level which limits the messages written to that log. | |
1759 The following error levels are supported by nginx: | |
1760 </para> | |
1761 | |
1762 <para> | |
1763 <list type="bullet"> | |
1764 | |
1765 <listitem> | |
1766 <literal>NGX_LOG_EMERG</literal> | |
1767 </listitem> | |
1768 | |
1769 <listitem> | |
1770 <literal>NGX_LOG_ALERT</literal> | |
1771 </listitem> | |
1772 | |
1773 <listitem> | |
1774 <literal>NGX_LOG_CRIT</literal> | |
1775 </listitem> | |
1776 | |
1777 <listitem> | |
1778 <literal>NGX_LOG_ERR</literal> | |
1779 </listitem> | |
1780 | |
1781 <listitem> | |
1782 <literal>NGX_LOG_WARN</literal> | |
1783 </listitem> | |
1784 | |
1785 <listitem> | |
1786 <literal>NGX_LOG_NOTICE</literal> | |
1787 </listitem> | |
1788 | |
1789 <listitem> | |
1790 <literal>NGX_LOG_INFO</literal> | |
1791 </listitem> | |
1792 | |
1793 <listitem> | |
1794 <literal>NGX_LOG_DEBUG</literal> | |
1795 </listitem> | |
1796 | |
1797 </list> | |
1798 </para> | |
1799 | |
1800 <para> | |
1801 For debug logging, debug mask is checked as well. The following debug masks | |
1802 exist: | |
1803 </para> | |
1804 | |
1805 <para> | |
1806 <list type="bullet"> | |
1807 | |
1808 <listitem> | |
1809 <literal>NGX_LOG_DEBUG_CORE</literal> | |
1810 </listitem> | |
1811 | |
1812 <listitem> | |
1813 <literal>NGX_LOG_DEBUG_ALLOC</literal> | |
1814 </listitem> | |
1815 | |
1816 <listitem> | |
1817 <literal>NGX_LOG_DEBUG_MUTEX</literal> | |
1818 </listitem> | |
1819 | |
1820 <listitem> | |
1821 <literal>NGX_LOG_DEBUG_EVENT</literal> | |
1822 </listitem> | |
1823 | |
1824 <listitem> | |
1825 <literal>NGX_LOG_DEBUG_HTTP</literal> | |
1826 </listitem> | |
1827 | |
1828 <listitem> | |
1829 <literal>NGX_LOG_DEBUG_MAIL</literal> | |
1830 </listitem> | |
1831 | |
1832 <listitem> | |
1833 <literal>NGX_LOG_DEBUG_STREAM</literal> | |
1834 </listitem> | |
1835 | |
1836 </list> | |
1837 </para> | |
1838 | |
1839 <para> | |
1840 Normally, loggers are created by existing nginx code from | |
1841 <literal>error_log</literal> directives and are available at nearly every stage | |
1842 of processing in cycle, configuration, client connection and other objects. | |
1843 </para> | |
1844 | |
1845 <para> | |
1846 Nginx provides the following logging macros: | |
1847 </para> | |
1848 | |
1849 <para> | |
1850 <list type="bullet"> | |
1851 | |
1852 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
1853 <literal>ngx_log_error(level, log, err, fmt, ...)</literal> — error logging |
1899 | 1854 </listitem> |
1855 | |
1856 <listitem> | |
1857 <literal>ngx_log_debug0(level, log, err, fmt)</literal>, | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
1858 <literal>ngx_log_debug1(level, log, err, fmt, arg1)</literal> etc — debug |
1899 | 1859 logging, up to 8 formatting arguments are supported |
1860 </listitem> | |
1861 | |
1862 </list> | |
1863 </para> | |
1864 | |
1865 <para> | |
1866 A log message is formatted in a buffer of size | |
1867 <literal>NGX_MAX_ERROR_STR</literal> (currently, 2048 bytes) on stack. | |
1868 The message is prepended with error level, process PID, connection id (stored | |
1869 in <literal>log->connection</literal>) and system error text. | |
1870 For non-debug messages <literal>log->handler</literal> is called as well to | |
1871 prepend more specific information to the log message. | |
1872 HTTP module sets <literal>ngx_http_log_error()</literal> function as log | |
1873 handler to log client and server addresses, current action (stored in | |
1874 <literal>log->action</literal>), client request line, server name etc. | |
1875 </para> | |
1876 | |
1877 <para> | |
1878 Example: | |
1879 </para> | |
1880 | |
1881 | |
1882 <programlisting> | |
1883 /* specify what is currently done */ | |
1884 log->action = "sending mp4 to client”; | |
1885 | |
1886 /* error and debug log */ | |
1887 ngx_log_error(NGX_LOG_INFO, c->log, 0, "client prematurely | |
1888 closed connection”); | |
1889 | |
1890 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0, | |
1891 "mp4 start:%ui, length:%ui”, mp4->start, mp4->length); | |
1892 </programlisting> | |
1893 | |
1894 <para> | |
1895 Logging result: | |
1896 </para> | |
1897 | |
1898 | |
1899 <programlisting> | |
1900 2016/09/16 22:08:52 [info] 17445#0: *1 client prematurely closed connection while | |
1901 sending mp4 to client, client: 127.0.0.1, server: , request: "GET /file.mp4 HTTP/1.1” | |
1902 2016/09/16 23:28:33 [debug] 22140#0: *1 mp4 start:0, length:10000 | |
1903 </programlisting> | |
1904 | |
1905 </section> | |
1906 | |
1907 | |
1908 <section name="Cycle" id="cycle"> | |
1909 | |
1910 <para> | |
1911 Cycle object keeps nginx runtime context, created from a specific | |
1912 configuration. | |
1913 The type of the cycle is <literal>ngx_cycle_t</literal>. | |
1914 Upon configuration reload a new cycle is created from the new version of nginx | |
1915 configuration. | |
1916 The old cycle is usually deleted after a new one is successfully created. | |
1917 Currently active cycle is held in the <literal>ngx_cycle</literal> global | |
1918 variable and is inherited by newly started nginx workers. | |
1919 </para> | |
1920 | |
1921 <para> | |
1922 A cycle is created by the function <literal>ngx_init_cycle()</literal>. | |
1923 The function receives the old cycle as the argument. | |
1924 It's used to locate the configuration file and inherit as much resources as | |
1925 possible from the old cycle to keep nginx running smoothly. | |
1936
8f996938fe23
Style: proper quotes usage.
Vladimir Homutov <vl@nginx.com>
parents:
1935
diff
changeset
|
1926 When nginx starts, a fake cycle called “init cycle” is created and is then |
1899 | 1927 replaced by a normal cycle, built from configuration. |
1928 </para> | |
1929 | |
1930 <para> | |
1931 Some members of the cycle: | |
1932 </para> | |
1933 | |
1934 <para> | |
1935 <list type="bullet"> | |
1936 | |
1937 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
1938 <literal>pool</literal> — cycle pool. Created for each new cycle |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
1939 </listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
1940 |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
1941 <listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
1942 <literal>log</literal> — cycle log. Initially, this log is inherited from the |
1899 | 1943 old cycle. |
1944 After reading configuration, this member is set to point to | |
1945 <literal>new_log</literal> | |
1946 </listitem> | |
1947 | |
1948 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
1949 <literal>new_log</literal> — cycle log, created by the configuration. |
1899 | 1950 It's affected by the root scope <literal>error_log</literal> directive |
1951 </listitem> | |
1952 | |
1953 <listitem> | |
1981
082724c57c38
Fixes in cycle section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1980
diff
changeset
|
1954 <literal>connections</literal>, <literal>connection_n</literal> — |
1899 | 1955 array of connections of type <literal>ngx_connection_t</literal>, created by |
1956 the event module while initializing each nginx worker. | |
1981
082724c57c38
Fixes in cycle section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1980
diff
changeset
|
1957 The number of connections <literal>connection_n</literal> is set by the |
082724c57c38
Fixes in cycle section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1980
diff
changeset
|
1958 <literal>worker_connections</literal> directive |
1899 | 1959 </listitem> |
1960 | |
1961 <listitem> | |
1962 <literal>free_connections</literal>, | |
1981
082724c57c38
Fixes in cycle section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1980
diff
changeset
|
1963 <literal>free_connection_n</literal> — list and number of currently available |
1899 | 1964 connections. |
1981
082724c57c38
Fixes in cycle section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1980
diff
changeset
|
1965 If no connections are available, nginx worker refuses to accept new clients or |
082724c57c38
Fixes in cycle section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1980
diff
changeset
|
1966 connect to upstream servers |
1899 | 1967 </listitem> |
1968 | |
1969 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
1970 <literal>files</literal>, <literal>files_n</literal> — array for mapping file |
1899 | 1971 descriptors to nginx connections. |
1972 This mapping is used by the event modules, having the | |
1973 <literal>NGX_USE_FD_EVENT</literal> flag (currently, it's poll and devpoll) | |
1974 </listitem> | |
1975 | |
1976 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
1977 <literal>conf_ctx</literal> — array of core module configurations. |
1899 | 1978 The configurations are created and filled while reading nginx configuration |
1979 files | |
1980 </listitem> | |
1981 | |
1982 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
1983 <literal>modules</literal>, <literal>modules_n</literal> — array of modules |
1899 | 1984 <literal>ngx_module_t</literal>, both static and dynamic, loaded by current |
1985 configuration | |
1986 </listitem> | |
1987 | |
1988 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
1989 <literal>listening</literal> — array of listening objects |
1899 | 1990 <literal>ngx_listening_t</literal>. |
1991 Listening objects are normally added by the the <literal>listen</literal> | |
1992 directive of different modules which call the | |
1993 <literal>ngx_create_listening()</literal> function. | |
1994 Based on listening objects, listen sockets are created by nginx | |
1995 </listitem> | |
1996 | |
1997 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
1998 <literal>paths</literal> — array of paths <literal>ngx_path_t</literal>. |
1899 | 1999 Paths are added by calling the function <literal>ngx_add_path()</literal> from |
2000 modules which are going to operate on certain directories. | |
2001 These directories are created by nginx after reading configuration, if missing. | |
2002 Moreover, two handlers can be added for each path: | |
2003 | |
2004 <list type="bullet"> | |
2005 | |
2006 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2007 path loader — executed only once in 60 seconds after starting or reloading |
1899 | 2008 nginx. Normally, reads the directory and stores data in nginx shared |
1936
8f996938fe23
Style: proper quotes usage.
Vladimir Homutov <vl@nginx.com>
parents:
1935
diff
changeset
|
2009 memory. The handler is called from a dedicated nginx process “nginx |
8f996938fe23
Style: proper quotes usage.
Vladimir Homutov <vl@nginx.com>
parents:
1935
diff
changeset
|
2010 cache loader” |
1899 | 2011 </listitem> |
2012 | |
2013 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2014 path manager — executed periodically. Normally, removes old files from the |
1899 | 2015 directory and reflects these changes in nginx memory. The handler is |
1936
8f996938fe23
Style: proper quotes usage.
Vladimir Homutov <vl@nginx.com>
parents:
1935
diff
changeset
|
2016 called from a dedicated nginx process “nginx cache manager” |
1899 | 2017 </listitem> |
2018 | |
2019 </list> | |
2020 </listitem> | |
2021 | |
2022 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2023 <literal>open_files</literal> — list of <literal>ngx_open_file_t</literal> |
1899 | 2024 objects. |
2025 An open file object is created by calling the function | |
2026 <literal>ngx_conf_open_file()</literal>. | |
2027 After reading configuration nginx opens all files from the | |
2028 <literal>open_files</literal> list and stores file descriptors in the | |
2029 <literal>fd</literal> field of each open file object. | |
2030 The files are opened in append mode and created if missing. | |
2031 The files from this list are reopened by nginx workers upon receiving the | |
2032 reopen signal (usually it's <literal>USR1</literal>). | |
2033 In this case the <literal>fd</literal> fields are changed to new descriptors. | |
2034 The open files are currently used for logging | |
2035 </listitem> | |
2036 | |
2037 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2038 <literal>shared_memory</literal> — list of shared memory zones, each added by |
1899 | 2039 calling the <literal>ngx_shared_memory_add()</literal> function. |
2040 Shared zones are mapped to the same address range in all nginx processes and | |
2041 are used to share common data, for example HTTP cache in-memory tree | |
2042 </listitem> | |
2043 | |
2044 </list> | |
2045 </para> | |
2046 | |
2047 </section> | |
2048 | |
2049 <section name="Buffer" id="buffer"> | |
2050 | |
2051 <para> | |
2052 For input/output operations, nginx provides the buffer type | |
2053 <literal>ngx_buf_t</literal>. | |
2054 Normally, it's used to hold data to be written to a destination or read from a | |
2055 source. | |
2056 Buffer can reference data in memory and in file. | |
2057 Technically it's possible that a buffer references both at the same time. | |
2058 Memory for the buffer is allocated separately and is not related to the buffer | |
2059 structure <literal>ngx_buf_t</literal>. | |
2060 </para> | |
2061 | |
2062 <para> | |
2063 The structure <literal>ngx_buf_t</literal> has the following fields: | |
2064 </para> | |
2065 | |
2066 <para> | |
2067 <list type="bullet"> | |
2068 | |
2069 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2070 <literal>start</literal>, <literal>end</literal> — the boundaries of memory |
1899 | 2071 block, allocated for the buffer |
2072 </listitem> | |
2073 | |
2074 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2075 <literal>pos</literal>, <literal>last</literal> — memory buffer boundaries, |
1899 | 2076 normally a subrange of <literal>start</literal> .. <literal>end</literal> |
2077 </listitem> | |
2078 | |
2079 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2080 <literal>file_pos</literal>, <literal>file_last</literal> — file buffer |
1899 | 2081 boundaries, these are offsets from the beginning of the file |
2082 </listitem> | |
2083 | |
2084 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2085 <literal>tag</literal> — unique value, used to distinguish buffers, created by |
1899 | 2086 different nginx module, usually, for the purpose of buffer reuse |
2087 </listitem> | |
2088 | |
2089 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2090 <literal>file</literal> — file object |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2091 </listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2092 |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2093 <listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2094 <literal>temporary</literal> — flag, meaning that the buffer references |
1899 | 2095 writable memory |
2096 </listitem> | |
2097 | |
2098 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2099 <literal>memory</literal> — flag, meaning that the buffer references read-only |
1899 | 2100 memory |
2101 </listitem> | |
2102 | |
2103 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2104 <literal>in_file</literal> — flag, meaning that current buffer references data |
1899 | 2105 in a file |
2106 </listitem> | |
2107 | |
2108 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2109 <literal>flush</literal> — flag, meaning that all data prior to this buffer |
1899 | 2110 should be flushed |
2111 </listitem> | |
2112 | |
2113 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2114 <literal>recycled</literal> — flag, meaning that the buffer can be reused and |
1899 | 2115 should be consumed as soon as possible |
2116 </listitem> | |
2117 | |
2118 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2119 <literal>sync</literal> — flag, meaning that the buffer carries no data or |
1899 | 2120 special signal like <literal>flush</literal> or <literal>last_buf</literal>. |
2121 Normally, such buffers are considered an error by nginx. This flags allows | |
2122 skipping the error checks | |
2123 </listitem> | |
2124 | |
2125 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2126 <literal>last_buf</literal> — flag, meaning that current buffer is the last in |
1899 | 2127 output |
2128 </listitem> | |
2129 | |
2130 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2131 <literal>last_in_chain</literal> — flag, meaning that there's no more data |
1899 | 2132 buffers in a (sub)request |
2133 </listitem> | |
2134 | |
2135 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2136 <literal>shadow</literal> — reference to another buffer, related to the current |
1899 | 2137 buffer. Usually current buffer uses data from the shadow buffer. Once current |
2138 buffer is consumed, the shadow buffer should normally also be marked as | |
2139 consumed | |
2140 </listitem> | |
2141 | |
2142 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2143 <literal>last_shadow</literal> — flag, meaning that current buffer is the last |
1899 | 2144 buffer, referencing a particular shadow buffer |
2145 </listitem> | |
2146 | |
2147 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2148 <literal>temp_file</literal> — flag, meaning that the buffer is in a temporary |
1899 | 2149 file |
2150 </listitem> | |
2151 | |
2152 </list> | |
2153 </para> | |
2154 | |
2155 <para> | |
2156 For input and output buffers are linked in chains. | |
2157 Chain is a sequence of chain links <literal>ngx_chain_t</literal>, defined as | |
2158 follows: | |
2159 </para> | |
2160 | |
2161 | |
2162 <programlisting> | |
2163 typedef struct ngx_chain_s ngx_chain_t; | |
2164 | |
2165 struct ngx_chain_s { | |
2166 ngx_buf_t *buf; | |
2167 ngx_chain_t *next; | |
2168 }; | |
2169 </programlisting> | |
2170 | |
2171 <para> | |
2172 Each chain link keeps a reference to its buffer and a reference to the next | |
2173 chain link. | |
2174 </para> | |
2175 | |
2176 <para> | |
2177 Example of using buffers and chains: | |
2178 </para> | |
2179 | |
2180 | |
2181 <programlisting> | |
2182 ngx_chain_t * | |
2183 ngx_get_my_chain(ngx_pool_t *pool) | |
2184 { | |
2185 ngx_buf_t *b; | |
2186 ngx_chain_t *out, *cl, **ll; | |
2187 | |
2188 /* first buf */ | |
2189 cl = ngx_alloc_chain_link(pool); | |
2190 if (cl == NULL) { /* error */ } | |
2191 | |
2192 b = ngx_calloc_buf(pool); | |
2193 if (b == NULL) { /* error */ } | |
2194 | |
2195 b->start = (u_char *) "foo"; | |
2196 b->pos = b->start; | |
2197 b->end = b->start + 3; | |
2198 b->last = b->end; | |
2199 b->memory = 1; /* read-only memory */ | |
2200 | |
2201 cl->buf = b; | |
2202 out = cl; | |
2203 ll = &cl->next; | |
2204 | |
2205 /* second buf */ | |
2206 cl = ngx_alloc_chain_link(pool); | |
2207 if (cl == NULL) { /* error */ } | |
2208 | |
2209 b = ngx_create_temp_buf(pool, 3); | |
2210 if (b == NULL) { /* error */ } | |
2211 | |
2212 b->last = ngx_cpymem(b->last, "foo", 3); | |
2213 | |
2214 cl->buf = b; | |
2215 cl->next = NULL; | |
2216 *ll = cl; | |
2217 | |
2218 return out; | |
2219 } | |
2220 </programlisting> | |
2221 | |
2222 </section> | |
2223 | |
2224 | |
2225 <section name="Networking" id="networking"> | |
2226 | |
2227 | |
2228 <!-- | |
2229 <section name="Network data types" id="network_data_types"> | |
2230 | |
2231 <para> | |
2232 TBD: ngx_addr_t, ngx_url_t, ngx_socket_t, ngx_sockaddr_t, parse url, parse | |
2233 address... | |
2234 </para> | |
2235 | |
2236 </section> | |
2237 --> | |
2238 | |
2239 <section name="Connection" id="connection"> | |
2240 | |
2241 <para> | |
2242 Connection type <literal>ngx_connection_t</literal> is a wrapper around a | |
2243 socket descriptor. Some of the structure fields are: | |
2244 </para> | |
2245 | |
2246 <para> | |
2247 <list type="bullet"> | |
2248 | |
2249 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2250 <literal>fd</literal> — socket descriptor |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2251 </listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2252 |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2253 <listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2254 <literal>data</literal> — arbitrary connection context. |
1899 | 2255 Normally, a pointer to a higher level object, built on top of the connection, |
2256 like HTTP request or Stream session | |
2257 </listitem> | |
2258 | |
2259 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2260 <literal>read</literal>, <literal>write</literal> — read and write events for |
1899 | 2261 the connection |
2262 </listitem> | |
2263 | |
2264 <listitem> | |
2265 <literal>recv</literal>, <literal>send</literal>, | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2266 <literal>recv_chain</literal>, <literal>send_chain</literal> — I/O operations |
1899 | 2267 for the connection |
2268 </listitem> | |
2269 | |
2270 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2271 <literal>pool</literal> — connection pool |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2272 </listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2273 |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2274 <listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2275 <literal>log</literal> — connection log |
1899 | 2276 </listitem> |
2277 | |
2278 <listitem> | |
2279 <literal>sockaddr</literal>, <literal>socklen</literal>, | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2280 <literal>addr_text</literal> — remote socket address in binary and text forms |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2281 </listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2282 |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2283 <listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2284 <literal>local_sockaddr</literal>, <literal>local_socklen</literal> — local |
1899 | 2285 socket address in binary form. |
2286 Initially, these fields are empty. | |
2287 Function <literal>ngx_connection_local_sockaddr()</literal> should be used to | |
2288 get socket local address | |
2289 </listitem> | |
2290 | |
2291 <listitem> | |
2292 <literal>proxy_protocol_addr</literal>, <literal>proxy_protocol_port</literal> | |
2293 - PROXY protocol client address and port, if PROXY protocol is enabled for the | |
2294 connection | |
2295 </listitem> | |
2296 | |
2297 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2298 <literal>ssl</literal> — nginx connection SSL context |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2299 </listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2300 |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2301 <listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2302 <literal>reusable</literal> — flag, meaning, that the connection is at the |
1899 | 2303 state, when it can be reused |
2304 </listitem> | |
2305 | |
2306 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2307 <literal>close</literal> — flag, meaning, that the connection is being reused |
1899 | 2308 and should be closed |
2309 </listitem> | |
2310 | |
2311 </list> | |
2312 </para> | |
2313 | |
2314 <para> | |
2315 An nginx connection can transparently encapsulate SSL layer. | |
2316 In this case the connection <literal>ssl</literal> field holds a pointer to an | |
2317 <literal>ngx_ssl_connection_t</literal> structure, keeping all SSL-related data | |
2318 for the connection, including <literal>SSL_CTX</literal> and | |
2319 <literal>SSL</literal>. | |
2320 The handlers <literal>recv</literal>, <literal>send</literal>, | |
2321 <literal>recv_chain</literal>, <literal>send_chain</literal> are set as well to | |
2322 SSL functions. | |
2323 </para> | |
2324 | |
2325 <para> | |
2326 The number of connections per nginx worker is limited by the | |
2327 <literal>worker_connections</literal> value. | |
2328 All connection structures are pre-created when a worker starts and stored in | |
2329 the <literal>connections</literal> field of the cycle object. | |
2330 To reach out for a connection structure, <literal>ngx_get_connection(s, | |
2331 log)</literal> function is used. | |
2332 The function receives a socket descriptor <literal>s</literal> which needs to | |
2333 be wrapped in a connection structure. | |
2334 </para> | |
2335 | |
2336 <para> | |
2337 Since the number of connections per worker is limited, nginx provides a | |
2338 way to grab connections which are currently in use. | |
2339 To enable or disable reuse of a connection, function | |
2340 <literal>ngx_reusable_connection(c, reusable)</literal> is called. | |
2341 Calling <literal>ngx_reusable_connection(c, 1)</literal> sets the | |
2342 <literal>reuse</literal> flag of the connection structure and inserts the | |
2343 connection in the <literal>reusable_connections_queue</literal> of the cycle. | |
2344 Whenever <literal>ngx_get_connection()</literal> finds out there are no | |
2345 available connections in the <literal>free_connections</literal> list of the | |
2346 cycle, it calls <literal>ngx_drain_connections()</literal> to release a | |
2347 specific number of reusable connections. | |
2348 For each such connection, the <literal>close</literal> flag is set and its read | |
2349 handler is called which is supposed to free the connection by calling | |
2350 <literal>ngx_close_connection(c)</literal> and make it available for reuse. | |
2351 To exit the state when a connection can be reused | |
2352 <literal>ngx_reusable_connection(c, 0)</literal> is called. | |
2353 An example of reusable connections in nginx is HTTP client connections which | |
2354 are marked as reusable until some data is received from the client. | |
2355 </para> | |
2356 | |
2357 </section> | |
2358 | |
2359 | |
2360 </section> | |
2361 | |
2362 | |
2363 <section name="Events" id="events"> | |
2364 | |
2365 | |
2366 <section name="Event" id="event"> | |
2367 | |
2368 <para> | |
2369 Event object <literal>ngx_event_t</literal> in nginx provides a way to be | |
2370 notified of a specific event happening. | |
2371 </para> | |
2372 | |
2373 <para> | |
2374 Some of the fields of the <literal>ngx_event_t</literal> are: | |
2375 </para> | |
2376 | |
2377 <para> | |
2378 <list type="bullet"> | |
2379 | |
2380 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2381 <literal>data</literal> — arbitrary event context, used in event handler, |
1899 | 2382 usually, a pointer to a connection, tied with the event |
2383 </listitem> | |
2384 | |
2385 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2386 <literal>handler</literal> — callback function to be invoked when the event |
1899 | 2387 happens |
2388 </listitem> | |
2389 | |
2390 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2391 <literal>write</literal> — flag, meaning that this is the write event. |
1899 | 2392 Used to distinguish between read and write events |
2393 </listitem> | |
2394 | |
2395 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2396 <literal>active</literal> — flag, meaning that the event is registered for |
1899 | 2397 receiving I/O notifications, normally from notification mechanisms like epoll, |
2398 kqueue, poll | |
2399 </listitem> | |
2400 | |
2401 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2402 <literal>ready</literal> — flag, meaning that the event has received an |
1899 | 2403 I/O notification |
2404 </listitem> | |
2405 | |
2406 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2407 <literal>delayed</literal> — flag, meaning that I/O is delayed due to rate |
1899 | 2408 limiting |
2409 </listitem> | |
2410 | |
2411 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2412 <literal>timer</literal> — Red-Black tree node for inserting the event into |
1899 | 2413 the timer tree |
2414 </listitem> | |
2415 | |
2416 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2417 <literal>timer_set</literal> — flag, meaning that the event timer is set, |
1899 | 2418 but not yet expired |
2419 </listitem> | |
2420 | |
2421 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2422 <literal>timedout</literal> — flag, meaning that the event timer has expired |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2423 </listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2424 |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2425 <listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2426 <literal>eof</literal> — read event flag, meaning that the eof has happened |
1899 | 2427 while reading data |
2428 </listitem> | |
2429 | |
2430 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2431 <literal>pending_eof</literal> — flag, meaning that the eof is pending on the |
1899 | 2432 socket, even though there may be some data available before it. |
2433 The flag is delivered via <literal>EPOLLRDHUP</literal> epoll event or | |
2434 <literal>EV_EOF</literal> kqueue flag | |
2435 </listitem> | |
2436 | |
2437 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2438 <literal>error</literal> — flag, meaning that an error has happened while |
1899 | 2439 reading (for read event) or writing (for write event) |
2440 </listitem> | |
2441 | |
2442 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2443 <literal>cancelable</literal> — timer event flag, meaning that the event |
1899 | 2444 handler should be called while performing nginx worker graceful shutdown, event |
2445 though event timeout has not yet expired. The flag provides a way to finalize | |
2446 certain activities, for example, flush log files | |
2447 </listitem> | |
2448 | |
2449 <listitem> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2450 <literal>posted</literal> — flag, meaning that the event is posted to queue |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2451 </listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2452 |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2453 <listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2454 <literal>queue</literal> — queue node for posting the event to a queue |
1899 | 2455 </listitem> |
2456 | |
2457 </list> | |
2458 </para> | |
2459 | |
2460 </section> | |
2461 | |
2462 | |
2463 <section name="I/O events" id="i_o_events"> | |
2464 | |
2465 <para> | |
2466 Each connection, received with the | |
2467 <literal>ngx_get_connection()</literal> call, has two events attached to it: | |
2468 <literal>c->read</literal> and <literal>c->write</literal>. | |
2469 These events are used to receive notifications about the socket being ready for | |
2470 reading or writing. | |
2471 All such events operate in Edge-Triggered mode, meaning that they only trigger | |
2472 notifications when the state of the socket changes. | |
2473 For example, doing a partial read on a socket will not make nginx deliver a | |
2474 repeated read notification until more data arrive in the socket. | |
2475 Even when the underlying I/O notification mechanism is essentially | |
2476 Level-Triggered (poll, select etc), nginx will turn the notifications into | |
2477 Edge-Triggered. | |
2478 To make nginx event notifications consistent across all notifications systems | |
2479 on different platforms, it's required, that the functions | |
2480 <literal>ngx_handle_read_event(rev, flags)</literal> and | |
1907
42ed974b83a5
Fixed the duplicated reference in development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1902
diff
changeset
|
2481 <literal>ngx_handle_write_event(wev, lowat)</literal> are called after handling |
1899 | 2482 an I/O socket notification or calling any I/O functions on that socket. |
2483 Normally, these functions are called once in the end of each read or write | |
2484 event handler. | |
2485 </para> | |
2486 | |
2487 </section> | |
2488 | |
2489 | |
2490 <section name="Timer events" id="timer_events"> | |
2491 | |
2492 <para> | |
2493 An event can be set to notify a timeout expiration. | |
2494 The function <literal>ngx_add_timer(ev, timer)</literal> sets a timeout for an | |
2495 event, <literal>ngx_del_timer(ev)</literal> deletes a previously set timeout. | |
2496 Timeouts currently set for all existing events, are kept in a global timeout | |
2497 Red-Black tree <literal>ngx_event_timer_rbtree</literal>. | |
2498 The key in that tree has the type <literal>ngx_msec_t</literal> and is the time | |
2499 in milliseconds since the beginning of January 1, 1970 (modulus | |
2500 <literal>ngx_msec_t</literal> max value) at which the event should expire. | |
2501 The tree structure provides fast inserting and deleting operations, as well as | |
2502 accessing the nearest timeouts. | |
2503 The latter is used by nginx to find out for how long to wait for I/O events | |
2504 and for expiring timeout events afterwards. | |
2505 </para> | |
2506 | |
2507 </section> | |
2508 | |
2509 | |
2510 <section name="Posted events" id="posted_events"> | |
2511 | |
2512 <para> | |
2513 An event can be posted which means that its handler will be called at some | |
2514 point later within the current event loop iteration. | |
2515 Posting events is a good practice for simplifying code and escaping stack | |
2516 overflows. | |
2517 Posted events are held in a post queue. | |
2518 The macro <literal>ngx_post_event(ev, q)</literal> posts the event | |
2519 <literal>ev</literal> to the post queue <literal>q</literal>. | |
2520 Macro <literal>ngx_delete_posted_event(ev)</literal> deletes the event | |
2521 <literal>ev</literal> from whatever queue it's currently posted. | |
2522 Normally, events are posted to the <literal>ngx_posted_events</literal> queue. | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2523 This queue is processed late in the event loop — after all I/O and timer |
1899 | 2524 events are already handled. |
2525 The function <literal>ngx_event_process_posted()</literal> is called to process | |
2526 an event queue. | |
2527 This function calls event handlers until the queue is not empty. This means | |
2528 that a posted event handler can post more events to be processed within the | |
2529 current event loop iteration. | |
2530 </para> | |
2531 | |
2532 <para> | |
2533 Example: | |
2534 </para> | |
2535 | |
2536 | |
2537 <programlisting> | |
2538 void | |
2539 ngx_my_connection_read(ngx_connection_t *c) | |
2540 { | |
2541 ngx_event_t *rev; | |
2542 | |
2543 rev = c->read; | |
2544 | |
2545 ngx_add_timer(rev, 1000); | |
2546 | |
2547 rev->handler = ngx_my_read_handler; | |
2548 | |
2549 ngx_my_read(rev); | |
2550 } | |
2551 | |
2552 | |
2553 void | |
2554 ngx_my_read_handler(ngx_event_t *rev) | |
2555 { | |
2556 ssize_t n; | |
2557 ngx_connection_t *c; | |
2558 u_char buf[256]; | |
2559 | |
2560 if (rev->timedout) { /* timeout expired */ } | |
2561 | |
2562 c = rev->data; | |
2563 | |
2564 while (rev->ready) { | |
2565 n = c->recv(c, buf, sizeof(buf)); | |
2566 | |
2567 if (n == NGX_AGAIN) { | |
2568 break; | |
2569 } | |
2570 | |
2571 if (n == NGX_ERROR) { /* error */ } | |
2572 | |
2573 /* process buf */ | |
2574 } | |
2575 | |
2576 if (ngx_handle_read_event(rev, 0) != NGX_OK) { /* error */ } | |
2577 } | |
2578 </programlisting> | |
2579 | |
2580 </section> | |
2581 | |
2582 | |
2583 <section name="Event loop" id="event_loop"> | |
2584 | |
2585 <para> | |
2586 All nginx processes which do I/O, have an event loop. | |
2587 The only type of process which does not have I/O, is nginx master process which | |
2588 spends most of its time in <literal>sigsuspend()</literal> call waiting for | |
2589 signals to arrive. | |
2590 Event loop is implemented in <literal>ngx_process_events_and_timers()</literal> | |
2591 function. | |
2592 This function is called repeatedly until the process exits. | |
2593 It has the following stages: | |
2594 </para> | |
2595 | |
2596 <para> | |
2597 <list type="bullet"> | |
2598 | |
2599 <listitem> | |
2600 find nearest timeout by calling <literal>ngx_event_find_timer()</literal>. | |
2601 This function finds the leftmost timer tree node and returns the number of | |
2602 milliseconds until that node expires | |
2603 </listitem> | |
2604 | |
2605 <listitem> | |
2606 process I/O events by calling a handler, specific to event notification | |
2607 mechanism, chosen by nginx configuration. | |
2608 This handler waits for at least one I/O event to happen, but no longer, than | |
2609 the nearest timeout. | |
2610 For each read or write event which has happened, the <literal>ready</literal> | |
2611 flag is set and its handler is called. | |
2612 For Linux, normally, the <literal>ngx_epoll_process_events()</literal> handler | |
2613 is used which calls <literal>epoll_wait()</literal> to wait for I/O events | |
2614 </listitem> | |
2615 | |
2616 <listitem> | |
2617 expire timers by calling <literal>ngx_event_expire_timers()</literal>. | |
2618 The timer tree is iterated from the leftmost element to the right until a not | |
2619 yet expired timeout is found. | |
2620 For each expired node the <literal>timedout</literal> event flag is set, | |
2621 <literal>timer_set</literal> flag is reset, and the event handler is called | |
2622 </listitem> | |
2623 | |
2624 <listitem> | |
2625 process posted events by calling <literal>ngx_event_process_posted()</literal>. | |
2626 The function repeatedly removes the first element from the posted events | |
2627 queue and calls its handler until the queue gets empty | |
2628 </listitem> | |
2629 | |
2630 </list> | |
2631 </para> | |
2632 | |
2633 <para> | |
2634 All nginx processes handle signals as well. | |
2635 Signal handlers only set global variables which are checked after the | |
2636 <literal>ngx_process_events_and_timers()</literal> call. | |
2637 </para> | |
2638 | |
2639 </section> | |
2640 | |
2641 | |
2642 </section> | |
2643 | |
2644 | |
2645 <section name="Processes" id="processes"> | |
2646 | |
2647 <para> | |
2648 There are several types of processes in nginx. | |
2649 The type of current process is kept in the <literal>ngx_process</literal> | |
2650 global variable: | |
2651 </para> | |
2652 | |
2653 <list type="bullet"> | |
2654 | |
2655 <listitem> | |
2656 | |
2657 <para> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2658 <literal>NGX_PROCESS_MASTER</literal> — the master process runs the |
1899 | 2659 <literal>ngx_master_process_cycle()</literal> function. |
2660 Master process does not have any I/O and responds only to signals. | |
2661 It reads configuration, creates cycles, starts and controls child processes | |
2662 </para> | |
2663 | |
2664 | |
2665 </listitem> | |
2666 | |
2667 <listitem> | |
2668 | |
2669 <para> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2670 <literal>NGX_PROCESS_WORKER</literal> — the worker process runs the |
1899 | 2671 <literal>ngx_worker_process_cycle()</literal> function. |
2672 Worker processes are started by master and handle client connections. | |
2673 They also respond to signals and channel commands, sent from master | |
2674 </para> | |
2675 | |
2676 | |
2677 </listitem> | |
2678 | |
2679 <listitem> | |
2680 | |
2681 <para> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2682 <literal>NGX_PROCESS_SINGLE</literal> — single process is the only type |
1899 | 2683 of processes which exist in the <literal>master_process off</literal> mode. |
2684 The cycle function for this process is | |
2685 <literal>ngx_single_process_cycle()</literal>. | |
2686 This process creates cycles and handles client connections | |
2687 </para> | |
2688 | |
2689 | |
2690 </listitem> | |
2691 | |
2692 <listitem> | |
2693 | |
2694 <para> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2695 <literal>NGX_PROCESS_HELPER</literal> — currently, there are two types of |
1899 | 2696 helper processes: cache manager and cache loader. |
2697 Both of them share the same cycle function | |
2698 <literal>ngx_cache_manager_process_cycle()</literal>. | |
2699 </para> | |
2700 | |
2701 | |
2702 </listitem> | |
2703 | |
2704 </list> | |
2705 | |
2706 <para> | |
2707 All nginx processes handle the following signals: | |
2708 </para> | |
2709 | |
2710 <list type="bullet"> | |
2711 | |
2712 <listitem> | |
2713 | |
2714 <para> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2715 <literal>NGX_SHUTDOWN_SIGNAL</literal> (<literal>SIGQUIT</literal>) — graceful |
1899 | 2716 shutdown. |
2717 Upon receiving this signal master process sends shutdown signal to all child | |
2718 processes. | |
2719 When no child processes are left, master destroys cycle pool and exits. | |
2720 A worker process which received this signal, closes all listening sockets and | |
2721 waits until timeout tree becomes empty, then destroys cycle pool and exits. | |
2722 A cache manager process exits right after receiving this signal. | |
2723 The variable <literal>ngx_quit</literal> is set to one after receiving this | |
2724 signal and immediately reset after being processed. | |
2725 The variable <literal>ngx_exiting</literal> is set to one when worker process | |
2726 is in shutdown state | |
2727 </para> | |
2728 | |
2729 | |
2730 </listitem> | |
2731 | |
2732 <listitem> | |
2733 | |
2734 <para> | |
2735 <literal>NGX_TERMINATE_SIGNAL</literal> (<literal>SIGTERM</literal>) - | |
2736 terminate. | |
2737 Upon receiving this signal master process sends terminate signal to all child | |
2738 processes. | |
2739 If child processes do not exit in 1 second, they are killed with the | |
2740 <literal>SIGKILL</literal> signal. | |
2741 When no child processes are left, master process destroys cycle pool and exits. | |
2742 A worker or cache manager process which received this signal destroys cycle | |
2743 pool and exits. | |
2744 The variable <literal>ngx_terminate</literal> is set to one after receiving | |
2745 this signal | |
2746 </para> | |
2747 | |
2748 | |
2749 </listitem> | |
2750 | |
2751 <listitem> | |
2752 | |
2753 <para> | |
2754 <literal>NGX_NOACCEPT_SIGNAL</literal> (<literal>SIGWINCH</literal>) | |
2755 - gracefully shut down worker processes | |
2756 </para> | |
2757 | |
2758 | |
2759 </listitem> | |
2760 | |
2761 <listitem> | |
2762 | |
2763 <para> | |
2764 <literal>NGX_RECONFIGURE_SIGNAL</literal> (<literal>SIGHUP</literal>) - | |
2765 reconfigure. | |
2766 Upon receiving this signal master process creates a new cycle from | |
2767 configuration file. | |
2768 If the new cycle was created successfully, the old cycle is deleted and new | |
2769 child processes are started. | |
2770 Meanwhile, the old processes receive the shutdown signal. | |
2771 In single-process mode, nginx creates a new cycle as well, but keeps the old | |
2772 one until all clients, tied to the old cycle, are gone. | |
2773 Worker and helper processes ignore this signal | |
2774 </para> | |
2775 | |
2776 | |
2777 </listitem> | |
2778 | |
2779 <listitem> | |
2780 | |
2781 <para> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2782 <literal>NGX_REOPEN_SIGNAL</literal> (<literal>SIGUSR1</literal>) — reopen |
1899 | 2783 files. |
2784 Master process passes this signal to workers. | |
2785 Worker processes reopen all <literal>open_files</literal> from the cycle | |
2786 </para> | |
2787 | |
2788 | |
2789 </listitem> | |
2790 | |
2791 <listitem> | |
2792 | |
2793 <para> | |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2794 <literal>NGX_CHANGEBIN_SIGNAL</literal> (<literal>SIGUSR2</literal>) — change |
1899 | 2795 nginx binary. |
2796 Master process starts a new nginx binary and passes there a list of all listen | |
2797 sockets. | |
1936
8f996938fe23
Style: proper quotes usage.
Vladimir Homutov <vl@nginx.com>
parents:
1935
diff
changeset
|
2798 The list is passed in the environment variable <literal>“NGINX”</literal> in |
1899 | 2799 text format, where descriptor numbers separated with semicolons. |
2800 A new nginx instance reads that variable and adds the sockets to its init | |
2801 cycle. | |
2802 Other processes ignore this signal | |
2803 </para> | |
2804 | |
2805 | |
2806 </listitem> | |
2807 | |
2808 </list> | |
2809 | |
2810 <para> | |
2811 While all nginx worker processes are able to receive and properly handle POSIX | |
2812 signals, master process normally does not pass any signals to workers and | |
2813 helpers with the standard <literal>kill()</literal> syscall. | |
2814 Instead, nginx uses inter-process channels which allow sending messages between | |
2815 all nginx processes. | |
2816 Currently, however, messages are only sent from master to its children. | |
2817 Those messages carry the same signals. | |
2818 The channels are socketpairs with their ends in different processes. | |
2819 </para> | |
2820 | |
2821 <para> | |
2822 When running nginx binary, several values can be specified next to | |
2823 <literal>-s</literal> parameter. | |
2824 Those values are <literal>stop</literal>, <literal>quit</literal>, | |
2825 <literal>reopen</literal>, <literal>reload</literal>. | |
2826 They are converted to signals <literal>NGX_TERMINATE_SIGNAL</literal>, | |
2827 <literal>NGX_SHUTDOWN_SIGNAL</literal>, <literal>NGX_REOPEN_SIGNAL</literal> | |
2828 and <literal>NGX_RECONFIGURE_SIGNAL</literal> and sent to the nginx master | |
2829 process, whose pid is read from nginx pid file. | |
2830 </para> | |
2831 | |
2832 </section> | |
2833 | |
1928
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2834 <section name="Modules" id="Modules"> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2835 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2836 <section name="Adding new modules" id="adding_new_modules"> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2837 <para> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2838 The standalone nginx module resides in a separate directory that contains |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2839 at least two files: |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2840 <literal>config</literal> and a file with the module source. |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2841 The first file contains all information needed for nginx to integrate |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2842 the module, for example: |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2843 <programlisting> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2844 ngx_module_type=CORE |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2845 ngx_module_name=ngx_foo_module |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2846 ngx_module_srcs="$ngx_addon_dir/ngx_foo_module.c" |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2847 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2848 . auto/module |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2849 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2850 ngx_addon_name=$ngx_module_name |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2851 </programlisting> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2852 The file is a POSIX shell script and it can set (or access) the |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2853 following variables: |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2854 <list type="bullet"> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2855 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2856 <listitem> |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2857 <literal>ngx_module_type</literal> — the type of module to build. |
1928
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2858 Possible options are <literal>CORE</literal>, <literal>HTTP</literal>, |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2859 <literal>HTTP_FILTER</literal>, <literal>HTTP_INIT_FILTER</literal>, |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2860 <literal>HTTP_AUX_FILTER</literal>, <literal>MAIL</literal>, |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2861 <literal>STREAM</literal>, or <literal>MISC</literal> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2862 </listitem> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2863 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2864 <listitem> |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2865 <literal>ngx_module_name</literal> — the name of the module. |
1928
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2866 A whitespace separated values list is accepted and may be used to build |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2867 multiple modules from a single set of source files. |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2868 The first name indicates the name of the output binary for a dynamic module. |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2869 The names in this list should match the names used in the module. |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2870 </listitem> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2871 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2872 <listitem> |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2873 <literal>ngx_addon_name</literal> — supplies the name of the module in the |
1928
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2874 console output text of the configure script. |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2875 </listitem> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2876 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2877 <listitem> |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2878 <literal>ngx_module_srcs</literal> — a whitespace separated list of source |
1928
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2879 files used to compile the module. |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2880 The $ngx_addon_dir variable can be used as a placeholder for the path of the |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2881 module source. |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2882 </listitem> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2883 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2884 <listitem> |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2885 <literal>ngx_module_incs</literal> — include paths required to build the module |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2886 </listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2887 |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2888 <listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2889 <literal>ngx_module_deps</literal> — a list of module's header files. |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2890 </listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2891 |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2892 <listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2893 <literal>ngx_module_libs</literal> — a list of libraries to link with the |
1928
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2894 module. |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2895 For example, libpthread would be linked using |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2896 <literal>ngx_module_libs=-lpthread</literal>. |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2897 The following macros can be used to link against the same libraries as |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2898 nginx: |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2899 <literal>LIBXSLT</literal>, <literal>LIBGD</literal>, <literal>GEOIP</literal>, |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2900 <literal>PCRE</literal>, <literal>OPENSSL</literal>, <literal>MD5</literal>, |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2901 <literal>SHA1</literal>, <literal>ZLIB</literal>, and <literal>PERL</literal> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2902 </listitem> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2903 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2904 <listitem> |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2905 <literal>ngx_module_link</literal> — set by the build system to |
1928
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2906 <literal>DYNAMIC</literal> for a dynamic module or <literal>ADDON</literal> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2907 for a static module and used to perform different actions depending on |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2908 linking type. |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2909 </listitem> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2910 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2911 <listitem> |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
2912 <literal>ngx_module_order</literal> — sets the load order for the module which |
1928
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2913 is useful for <literal>HTTP_FILTER</literal> and |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2914 <literal>HTTP_AUX_FILTER</literal> module types. |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2915 The order is stored in a reverse list. |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2916 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2917 <para> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2918 The <literal>ngx_http_copy_filter_module</literal> is near the bottom of the |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2919 list so is one of the first to be executed. |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2920 This reads the data for other filters. |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2921 Near the top of the list is <literal>ngx_http_write_filter_module</literal> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2922 which writes the data out and is one of the last to be executed. |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2923 </para> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2924 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2925 <para> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2926 The format for this option is typically the current module’s name followed by |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2927 a whitespace separated list of modules to insert before, and therefore execute |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2928 after. |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2929 The module will be inserted before the last module in the list that is found |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2930 to be currently loaded. |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2931 </para> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2932 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2933 <para> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2934 By default for filter modules this is set to |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2935 “<literal>ngx_http_copy_filter</literal>” which will insert the module before |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2936 the copy filter in the list and therefore will execute after the copy filter. |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2937 For other module types the default is empty. |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2938 </para> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2939 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2940 </listitem> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2941 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2942 </list> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2943 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2944 A module can be added to nginx by means of the configure script using |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2945 <literal>--add-module=/path/to/module</literal> for static compilation and |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2946 <literal>--add-dynamic-module=/path/to/module</literal> for dynamic compilation. |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2947 </para> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2948 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2949 </section> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2950 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2951 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2952 <section name="Core modules" id="core_modules"> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2953 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2954 <para> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2955 Modules are building blocks of nginx, and most of its functionality is |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2956 implemented as modules. |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2957 The module source file must contain a global variable of |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2958 <literal>ngx_module_t</literal> type which is defined as follows: |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2959 <programlisting> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2960 struct ngx_module_s { |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2961 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2962 /* private part is omitted */ |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2963 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2964 void *ctx; |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2965 ngx_command_t *commands; |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2966 ngx_uint_t type; |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2967 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2968 ngx_int_t (*init_master)(ngx_log_t *log); |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2969 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2970 ngx_int_t (*init_module)(ngx_cycle_t *cycle); |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2971 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2972 ngx_int_t (*init_process)(ngx_cycle_t *cycle); |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2973 ngx_int_t (*init_thread)(ngx_cycle_t *cycle); |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2974 void (*exit_thread)(ngx_cycle_t *cycle); |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2975 void (*exit_process)(ngx_cycle_t *cycle); |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2976 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2977 void (*exit_master)(ngx_cycle_t *cycle); |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2978 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2979 /* stubs for future extensions are omitted */ |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2980 }; |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2981 </programlisting> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2982 The omitted private part includes module version, signature and is filled |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2983 using the predefined macro <literal>NGX_MODULE_V1</literal>. |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2984 </para> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2985 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2986 <para> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2987 Each module keeps its private data in the <literal>ctx</literal> field, |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2988 recognizes specific configuration directives, specified in the |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2989 <literal>commands</literal> array, and may be invoked at certain stages of |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2990 nginx lifecycle. |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2991 The module lifecycle consists of the following events: |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2992 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2993 <list type="bullet"> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2994 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2995 <listitem> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2996 Configuration directive handlers are called as they appear |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2997 in configuration files in the context of the master process |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2998 </listitem> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
2999 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3000 <listitem> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3001 The <literal>init_module</literal> handler is called in the context of |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3002 the master process after the configuration is parsed successfully |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3003 </listitem> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3004 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3005 <listitem> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3006 The master process creates worker process(es) and |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3007 <literal>init_process</literal> handler is called in each of them |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3008 </listitem> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3009 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3010 <listitem> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3011 When a worker process receives the shutdown command from master, it invokes |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3012 the <literal>exit_process</literal> handler |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3013 </listitem> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3014 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3015 <listitem> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3016 The master process calls the <literal>exit_master</literal> handler before |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3017 exiting. |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3018 </listitem> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3019 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3020 </list> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3021 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3022 <note> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3023 <literal>init_module</literal> handler may be called multiple times |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3024 in the master process if the configuration reload is requested. |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3025 </note> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3026 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3027 The <literal>init_master</literal>, <literal>init_thread</literal> and |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3028 <literal>exit_thread</literal> handlers are not implemented at the moment; |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3029 Threads in nginx are only used as supplementary I/O facility with its own |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3030 API and <literal>init_master</literal> handler looks unnecessary. |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3031 </para> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3032 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3033 <para> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3034 The module <literal>type</literal> defines what exactly is stored in the |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3035 <literal>ctx</literal> field. |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3036 There are several types of modules: |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3037 <list type="bullet"> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3038 <listitem><literal>NGX_CORE_MODULE</literal></listitem> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3039 <listitem><literal>NGX_EVENT_MODULE</literal></listitem> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3040 <listitem><literal>NGX_HTTP_MODULE</literal></listitem> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3041 <listitem><literal>NGX_MAIL_MODULE</literal></listitem> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3042 <listitem><literal>NGX_STREAM_MODULE</literal></listitem> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3043 </list> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3044 The <literal>NGX_CORE_MODULE</literal> is the most basic and thus the most |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3045 generic and most low-level type of module. Other module types are implemented |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3046 on top of it and provide more convenient way to deal with corresponding |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3047 problem domains, like handling events or http requests. |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3048 </para> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3049 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3050 <para> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3051 The examples of core modules are <literal>ngx_core_module</literal>, |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3052 <literal>ngx_errlog_module</literal>, <literal>ngx_regex_module</literal>, |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3053 <literal>ngx_thread_pool_module</literal>, |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3054 <literal>ngx_openssl_module</literal> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3055 modules and, of course, http, stream, mail and event modules itself. |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3056 The context of a core module is defined as: |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3057 <programlisting> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3058 typedef struct { |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3059 ngx_str_t name; |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3060 void *(*create_conf)(ngx_cycle_t *cycle); |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3061 char *(*init_conf)(ngx_cycle_t *cycle, void *conf); |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3062 } ngx_core_module_t; |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3063 </programlisting> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3064 where the <literal>name</literal> is a string with a module name for |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3065 convenience, <literal>create_conf</literal> and <literal>init_conf</literal> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3066 are pointers to functions that create and initialize module configuration |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3067 correspondingly. |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3068 For core modules, nginx will call <literal>create_conf</literal> before parsing |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3069 a new configuration and <literal>init_conf</literal> after all configuration |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3070 was parsed successfully. |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3071 The typical <literal>create_conf</literal> function allocates memory for the |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3072 configuration and sets default values. |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3073 The <literal>init_conf</literal> deals with known configuration and thus may |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3074 perform sanity checks and complete initialization. |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3075 </para> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3076 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3077 <para> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3078 For example, the simplistic <literal>ngx_foo_module</literal> can look like |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3079 this: |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3080 <programlisting> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3081 /* |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3082 * Copyright (C) Author. |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3083 */ |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3084 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3085 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3086 #include <ngx_config.h> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3087 #include <ngx_core.h> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3088 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3089 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3090 typedef struct { |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3091 ngx_flag_t enable; |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3092 } ngx_foo_conf_t; |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3093 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3094 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3095 static void *ngx_foo_create_conf(ngx_cycle_t *cycle); |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3096 static char *ngx_foo_init_conf(ngx_cycle_t *cycle, void *conf); |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3097 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3098 static char *ngx_foo_enable(ngx_conf_t *cf, void *post, void *data); |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3099 static ngx_conf_post_t ngx_foo_enable_post = { ngx_foo_enable }; |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3100 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3101 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3102 static ngx_command_t ngx_foo_commands[] = { |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3103 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3104 { ngx_string("foo_enabled"), |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3105 NGX_MAIN_CONF|NGX_DIRECT_CONF|NGX_CONF_FLAG, |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3106 ngx_conf_set_flag_slot, |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3107 0, |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3108 offsetof(ngx_foo_conf_t, enable), |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3109 &ngx_foo_enable_post }, |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3110 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3111 ngx_null_command |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3112 }; |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3113 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3114 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3115 static ngx_core_module_t ngx_foo_module_ctx = { |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3116 ngx_string("foo"), |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3117 ngx_foo_create_conf, |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3118 ngx_foo_init_conf |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3119 }; |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3120 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3121 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3122 ngx_module_t ngx_foo_module = { |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3123 NGX_MODULE_V1, |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3124 &ngx_foo_module_ctx, /* module context */ |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3125 ngx_foo_commands, /* module directives */ |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3126 NGX_CORE_MODULE, /* module type */ |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3127 NULL, /* init master */ |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3128 NULL, /* init module */ |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3129 NULL, /* init process */ |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3130 NULL, /* init thread */ |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3131 NULL, /* exit thread */ |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3132 NULL, /* exit process */ |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3133 NULL, /* exit master */ |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3134 NGX_MODULE_V1_PADDING |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3135 }; |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3136 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3137 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3138 static void * |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3139 ngx_foo_create_conf(ngx_cycle_t *cycle) |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3140 { |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3141 ngx_foo_conf_t *fcf; |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3142 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3143 fcf = ngx_pcalloc(cycle->pool, sizeof(ngx_foo_conf_t)); |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3144 if (fcf == NULL) { |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3145 return NULL; |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3146 } |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3147 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3148 fcf->enable = NGX_CONF_UNSET; |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3149 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3150 return fcf; |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3151 } |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3152 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3153 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3154 static char * |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3155 ngx_foo_init_conf(ngx_cycle_t *cycle, void *conf) |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3156 { |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3157 ngx_foo_conf_t *fcf = conf; |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3158 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3159 ngx_conf_init_value(fcf->enable, 0); |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3160 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3161 return NGX_CONF_OK; |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3162 } |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3163 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3164 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3165 static char * |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3166 ngx_foo_enable(ngx_conf_t *cf, void *post, void *data) |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3167 { |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3168 ngx_flag_t *fp = data; |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3169 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3170 if (*fp == 0) { |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3171 return NGX_CONF_OK; |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3172 } |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3173 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3174 ngx_log_error(NGX_LOG_NOTICE, cf->log, 0, "Foo Module is enabled"); |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3175 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3176 return NGX_CONF_OK; |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3177 } |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3178 </programlisting> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3179 </para> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3180 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3181 </section> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3182 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3183 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3184 <section name="Configuration directives" id="config_directives"> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3185 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3186 <para> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3187 The <literal>ngx_command_t</literal> describes single configuration directive. |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3188 Each module, supporting configuration, provides an array of such specifications |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3189 that describe how to process arguments and what handlers to call: |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3190 <programlisting> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3191 struct ngx_command_s { |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3192 ngx_str_t name; |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3193 ngx_uint_t type; |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3194 char *(*set)(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3195 ngx_uint_t conf; |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3196 ngx_uint_t offset; |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3197 void *post; |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3198 }; |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3199 </programlisting> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3200 The array should be terminated by a special value |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3201 “<literal>ngx_null_command</literal>”. |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3202 The <literal>name</literal> is the literal name of a directive, as it appears |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3203 in configuration file, for example “<literal>worker_processes</literal>” or |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3204 “<literal>listen</literal>”. |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3205 The <literal>type</literal> is a bitfield that controls number of arguments, |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3206 command type and other properties using corresponding flags. |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3207 Arguments flags: |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3208 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3209 <list type="bullet"> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3210 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3211 <listitem> |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3212 <literal>NGX_CONF_NOARGS</literal> — directive without arguments |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3213 </listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3214 |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3215 <listitem><literal>NGX_CONF_1MORE</literal> — one required argument</listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3216 |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3217 <listitem><literal>NGX_CONF_2MORE</literal> — two required arguments</listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3218 |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3219 <listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3220 <literal>NGX_CONF_TAKE1..7</literal> — exactly 1..7 arguments |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3221 </listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3222 |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3223 <listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3224 <literal>NGX_CONF_TAKE12, 13, 23, 123, 1234</literal> — one or two arguments, |
1928
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3225 or other combinations |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3226 </listitem> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3227 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3228 </list> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3229 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3230 Directive types: |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3231 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3232 <list type="bullet"> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3233 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3234 <listitem> |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3235 <literal>NGX_CONF_BLOCK</literal> — the directive is a block, i.e. it may |
1928
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3236 contain other directives in curly braces, or even implement its own parser |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3237 to handle contents inside. |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3238 </listitem> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3239 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3240 <listitem> |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3241 <literal>NGX_CONF_FLAG</literal> — the directive value is a flag, a boolean |
1928
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3242 value represented by “<literal>on</literal>” or “<literal>off</literal>” |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3243 strings. |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3244 </listitem> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3245 </list> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3246 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3247 Context of a directive defines where in the configuration it may appear |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3248 and how to access module context to store corresponding values: |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3249 <list type="bullet"> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3250 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3251 <listitem> |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3252 <literal>NGX_MAIN_CONF</literal> — top level configuration |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3253 </listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3254 |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3255 <listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3256 <literal>NGX_HTTP_MAIN_CONF</literal> — in the http block |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3257 </listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3258 |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3259 <listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3260 <literal>NGX_HTTP_SRV_CONF</literal> — in the http server block |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3261 </listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3262 |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3263 <listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3264 <literal>NGX_HTTP_LOC_CONF</literal> — in the http location |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3265 </listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3266 |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3267 <listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3268 <literal>NGX_HTTP_UPS_CONF</literal> — in the http upstream block |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3269 </listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3270 |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3271 <listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3272 <literal>NGX_HTTP_SIF_CONF</literal> — in the http server “if” |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3273 </listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3274 |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3275 <listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3276 <literal>NGX_HTTP_LIF_CONF</literal> — in the http location “if” |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3277 </listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3278 |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3279 <listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3280 <literal>NGX_HTTP_LMT_CONF</literal> — in the http “limit_except” |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3281 </listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3282 |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3283 <listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3284 <literal>NGX_STREAM_MAIN_CONF</literal> — in the stream block |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3285 </listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3286 |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3287 <listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3288 <literal>NGX_STREAM_SRV_CONF</literal> — in the stream server block |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3289 </listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3290 |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3291 <listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3292 <literal>NGX_STREAM_UPS_CONF</literal> — in the stream upstream block |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3293 </listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3294 |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3295 <listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3296 <literal>NGX_MAIL_MAIN_CONF</literal> — in the the mail block |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3297 </listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3298 |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3299 <listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3300 <literal>NGX_MAIL_SRV_CONF</literal> — in the mail server block |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3301 </listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3302 |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3303 <listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3304 <literal>NGX_EVENT_CONF</literal> — in the event block |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3305 </listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3306 |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3307 <listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3308 <literal>NGX_DIRECT_CONF</literal> — used by modules that don't |
1928
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3309 create a hierarchy of contexts and store module configuration directly in ctx |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3310 </listitem> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3311 </list> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3312 The configuration parser uses this flags to throw an error in case of |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3313 a misplaced directive and calls directive handlers supplied with a proper |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3314 configuration pointer, so that same directives in different locations could |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3315 store their values in distinct places. |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3316 </para> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3317 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3318 <para> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3319 The <literal>set</literal> field defines a handler that processes a directive |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3320 and stores parsed values into corresponding configuration. |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3321 Nginx offers a convenient set of functions that perform common conversions: |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3322 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3323 <list type="bullet"> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3324 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3325 <listitem> |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3326 <literal>ngx_conf_set_flag_slot</literal> — converts literal |
1928
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3327 “<literal>on</literal>” or “<literal>off</literal>” strings into |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3328 <literal>ngx_flag_t</literal> type with values 1 or 0 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3329 </listitem> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3330 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3331 <listitem> |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3332 <literal>ngx_conf_set_str_slot</literal> — stores string as a value of the |
1928
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3333 <literal>ngx_str_t</literal> type |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3334 </listitem> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3335 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3336 <listitem> |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3337 <literal>ngx_conf_set_str_array_slot</literal> — appends |
1928
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3338 <literal>ngx_array_t</literal> of <literal>ngx_str_t</literal> with a new value. |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3339 The array is created if not yet exists |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3340 </listitem> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3341 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3342 <listitem> |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3343 <literal>ngx_conf_set_keyval_slot</literal> — appends |
1928
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3344 <literal>ngx_array_t</literal> of <literal>ngx_keyval_t</literal> with |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3345 a new value, where key is the first string and value is second. |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3346 The array is created if not yet exists |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3347 </listitem> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3348 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3349 <listitem> |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3350 <literal>ngx_conf_set_num_slot</literal> — converts directive argument |
1928
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3351 to a <literal>ngx_int_t</literal> value |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3352 </listitem> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3353 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3354 <listitem> |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3355 <literal>ngx_conf_set_size_slot</literal> — converts |
1928
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3356 <link doc="../syntax.xml">size</link> to <literal>size_t</literal> value |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3357 in bytes |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3358 </listitem> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3359 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3360 <listitem> |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3361 <literal>ngx_conf_set_off_slot</literal> — converts |
1928
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3362 <link doc="../syntax.xml">offset</link> to <literal>off_t</literal> value |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3363 in bytes |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3364 </listitem> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3365 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3366 <listitem> |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3367 <literal>ngx_conf_set_msec_slot</literal> — converts |
1928
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3368 <link doc="../syntax.xml">time</link> to <literal>ngx_msec_t</literal> value |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3369 in milliseconds |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3370 </listitem> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3371 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3372 <listitem> |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3373 <literal>ngx_conf_set_sec_slot</literal> — converts |
1928
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3374 <link doc="../syntax.xml">time</link> to <literal>time_t</literal> value |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3375 in seconds |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3376 </listitem> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3377 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3378 <listitem> |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3379 <literal>ngx_conf_set_bufs_slot</literal> — converts two arguments |
1928
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3380 into <literal>ngx_bufs_t</literal> that holds <literal>ngx_int_t</literal> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3381 number and <link doc="../syntax.xml">size</link> of buffers |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3382 </listitem> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3383 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3384 <listitem> |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3385 <literal>ngx_conf_set_enum_slot</literal> — converts argument |
1928
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3386 into <literal>ngx_uint_t</literal> value. |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3387 The null-terminated array of <literal>ngx_conf_enum_t</literal> passed in the |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3388 <literal>post</literal> field defines acceptable strings and corresponding |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3389 integer values |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3390 </listitem> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3391 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3392 <listitem> |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3393 <literal>ngx_conf_set_bitmask_slot</literal> — arguments are converted to |
1928
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3394 <literal>ngx_uint_t</literal> value and OR'ed with the resulting value, |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3395 forming a bitmask. |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3396 The null-terminated array of <literal>ngx_conf_bitmask_t</literal> passed in |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3397 the <literal>post</literal> field defines acceptable strings and corresponding |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3398 mask values |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3399 </listitem> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3400 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3401 <listitem> |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3402 <literal>set_path_slot</literal> — converts arguments to |
1928
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3403 <literal>ngx_path_t</literal> type and performs all required initializations. |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3404 See the |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3405 <link doc="../http/ngx_http_proxy_module.xml" id="proxy_temp_path">proxy_temp_path</link> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3406 directive description for details |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3407 </listitem> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3408 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3409 <listitem> |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3410 <literal>set_access_slot</literal> — converts arguments to file permissions |
1928
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3411 mask. |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3412 See the |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3413 <link doc="../http/ngx_http_proxy_module.xml" id="proxy_store_access">proxy_store_access</link> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3414 directive description for details |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3415 </listitem> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3416 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3417 </list> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3418 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3419 </para> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3420 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3421 <para> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3422 The <literal>conf</literal> field defines which context is used to store |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3423 the value of the directive, or zero if contexts are not used. |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3424 Only simple core modules use configuration without context and set |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3425 <literal>NGX_DIRECT_CONF</literal> flag. |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3426 In real life, such modules like http or stream require more sophisticated |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3427 configuration that can be applied per-server or per-location, or even more |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3428 precisely, in the context of the “<literal>if</literal>” directive or |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3429 some limit. |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3430 In this modules, configuration structure is more complex. |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3431 Please refer to corresponding modules description to understand how |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3432 they manage their configuration. |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3433 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3434 <list type="bullet"> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3435 <listitem> |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3436 <literal>NGX_HTTP_MAIN_CONF_OFFSET</literal> — http block configuration |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3437 </listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3438 |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3439 <listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3440 <literal>NGX_HTTP_SRV_CONF_OFFSET</literal> — http server configuration |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3441 </listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3442 |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3443 <listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3444 <literal>NGX_HTTP_LOC_CONF_OFFSET</literal> — http location configuration |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3445 </listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3446 |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3447 <listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3448 <literal>NGX_STREAM_MAIN_CONF_OFFSET</literal> — stream block configuration |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3449 </listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3450 |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3451 <listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3452 <literal>NGX_STREAM_SRV_CONF_OFFSET</literal> — stream server configuration |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3453 </listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3454 |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3455 <listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3456 <literal>NGX_MAIL_MAIN_CONF_OFFSET</literal> — mail block configuration |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3457 </listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3458 |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3459 <listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3460 <literal>NGX_MAIL_SRV_CONF_OFFSET</literal> — mail server configuration |
1928
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3461 </listitem> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3462 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3463 </list> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3464 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3465 </para> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3466 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3467 <para> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3468 The <literal>offset</literal> defines an offset of a field in a module |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3469 configuration structure that holds values of this particular directive. |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3470 The typical use is to employ <literal>offsetof()</literal> macro. |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3471 </para> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3472 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3473 <para> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3474 The <literal>post</literal> is a twofold field: it may be used to define |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3475 a handler to be called after main handler completed or to pass additional |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3476 data to the main handler. |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3477 In the first case, <literal>ngx_conf_post_t</literal> structure needs to |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3478 be initialized with a pointer to handler, for example: |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3479 <programlisting> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3480 static char *ngx_do_foo(ngx_conf_t *cf, void *post, void *data); |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3481 static ngx_conf_post_t ngx_foo_post = { ngx_do_foo }; |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3482 </programlisting> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3483 The <literal>post</literal> argument is the <literal>ngx_conf_post_t</literal> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3484 object itself, and the <literal>data</literal> is a pointer to value, |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3485 converted from arguments by the main handler with the appropriate type. |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3486 </para> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3487 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3488 </section> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3489 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3490 </section> |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3491 |
2c14a16c61eb
Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1920
diff
changeset
|
3492 |
1929
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3493 <section name="HTTP" id="http"> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3494 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3495 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3496 <section name="Connection" id="http_connection"> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3497 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3498 <para> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3499 Each client HTTP connection runs through the following stages: |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3500 </para> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3501 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3502 <list type="bullet"> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3503 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3504 <listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3505 <literal>ngx_event_accept()</literal> accepts a client TCP connection. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3506 This handler is called in response to a read notification on a listen socket. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3507 A new <literal>ngx_connecton_t</literal> object is created at this stage. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3508 The object wraps the newly accepted client socket. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3509 Each nginx listener provides a handler to pass the new connection object to. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3510 For HTTP connections it's <literal>ngx_http_init_connection(c)</literal> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3511 </listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3512 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3513 <listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3514 <literal>ngx_http_init_connection()</literal> performs early initialization of |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3515 an HTTP connection. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3516 At this stage an <literal>ngx_http_connection_t</literal> object is created for |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3517 the connection and its reference is stored in connection's |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3518 <literal>data</literal> field. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3519 Later it will be substituted with an HTTP request object. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3520 PROXY protocol parser and SSL handshake are started at this stage as well |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3521 </listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3522 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3523 <listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3524 <literal>ngx_http_wait_request_handler()</literal> is a read event handler, that |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3525 is called when data is available in the client socket. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3526 At this stage an HTTP request object <literal>ngx_http_request_t</literal> is |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3527 created and set to connection's <literal>data</literal> field |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3528 </listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3529 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3530 <listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3531 <literal>ngx_http_process_request_line()</literal> is a read event handler, |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3532 which reads client request line. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3533 The handler is set by <literal>ngx_http_wait_request_handler()</literal>. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3534 Reading is done into connection's <literal>buffer</literal>. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3535 The size of the buffer is initially set by the directive |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3536 <link doc="../http/ngx_http_core_module.xml" id="client_header_buffer_size"/>. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3537 The entire client header is supposed to fit the buffer. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3538 If the initial size is not enough, a bigger buffer is allocated, whose size is |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3539 set by the <literal>large_client_header_buffers</literal> directive |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3540 </listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3541 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3542 <listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3543 <literal>ngx_http_process_request_headers()</literal> is a read event handler, |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3544 which is set after <literal>ngx_http_process_request_line()</literal> to read |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3545 client request header |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3546 </listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3547 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3548 <listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3549 <literal>ngx_http_core_run_phases()</literal> is called when the request header |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3550 is completely read and parsed. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3551 This function runs request phases from |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3552 <literal>NGX_HTTP_POST_READ_PHASE</literal> to |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3553 <literal>NGX_HTTP_CONTENT_PHASE</literal>. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3554 The last phase is supposed to generate response and pass it along the filter |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3555 chain. |
1979 | 3556 The response is not necessarily sent to the client at this phase. |
1929
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3557 It may remain buffered and will be sent at the finalization stage |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3558 </listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3559 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3560 <listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3561 <literal>ngx_http_finalize_request()</literal> is usually called when the |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3562 request has generated all the output or produced an error. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3563 In the latter case an appropriate error page is looked up and used as the |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3564 response. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3565 If the response is not completely sent to the client by this point, an |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3566 HTTP writer <literal>ngx_http_writer()</literal> is activated to finish |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3567 sending outstanding data |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3568 </listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3569 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3570 <listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3571 <literal>ngx_http_finalize_connection()</literal> is called when the response is |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3572 completely sent to the client and the request can be destroyed. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3573 If client connection keepalive feature is enabled, |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3574 <literal>ngx_http_set_keepalive()</literal> is called, which destroys current |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3575 request and waits for the next request on the connection. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3576 Otherwise, <literal>ngx_http_close_request()</literal> destroys both the |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3577 request and the connection |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3578 </listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3579 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3580 </list> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3581 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3582 </section> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3583 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3584 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3585 <section name="Request" id="http_request"> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3586 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3587 <para> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3588 For each client HTTP request the <literal>ngx_http_request_t</literal> object is |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3589 created. Some of the fields of this object: |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3590 </para> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3591 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3592 <list type="bullet"> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3593 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3594 <listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3595 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3596 <para> |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3597 <literal>connection</literal> — pointer to a <literal>ngx_connection_t</literal> |
1929
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3598 client connection object. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3599 Several requests may reference the same connection object at the same time - |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3600 one main request and its subrequests. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3601 After a request is deleted, a new request may be created on the same connection. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3602 </para> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3603 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3604 <para> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3605 Note that for HTTP connections <literal>ngx_connection_t</literal>'s |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3606 <literal>data</literal> field points back to the request. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3607 Such request is called active, as opposed to the other requests tied with the |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3608 connection. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3609 Active request is used to handle client connection events and is allowed to |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3610 output its response to the client. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3611 Normally, each request becomes active at some point to be able to send its |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3612 output |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3613 </para> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3614 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3615 </listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3616 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3617 <listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3618 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3619 <para> |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3620 <literal>ctx</literal> — array of HTTP module contexts. |
1929
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3621 Each module of type <literal>NGX_HTTP_MODULE</literal> can store any value |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3622 (normally, a pointer to a structure) in the request. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3623 The value is stored in the <literal>ctx</literal> array at the module's |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3624 <literal>ctx_index</literal> position. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3625 The following macros provide a convenient way to get and set request contexts: |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3626 </para> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3627 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3628 <list type="bullet"> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3629 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3630 <listitem> |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3631 <literal>ngx_http_get_module_ctx(r, module)</literal> — returns |
1929
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3632 <literal>module</literal>'s context |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3633 </listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3634 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3635 <listitem> |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3636 <literal>ngx_http_set_ctx(r, c, module)</literal> — sets <literal>c</literal> |
1929
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3637 as <literal>module</literal>'s context |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3638 </listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3639 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3640 </list> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3641 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3642 </listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3643 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3644 <listitem> |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3645 <literal>main_conf, srv_conf, loc_conf</literal> — arrays of current request |
1929
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3646 configurations. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3647 Configurations are stored at module's <literal>ctx_index</literal> positions |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3648 </listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3649 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3650 <listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3651 <literal>read_event_handler</literal>, <literal>write_event_handler</literal> - |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3652 read and write event handlers for the request. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3653 Normally, an HTTP connection has <literal>ngx_http_request_handler()</literal> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3654 set as both read and write event handlers. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3655 This function calls <literal>read_event_handler</literal> and |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3656 <literal>write_event_handler</literal> handlers of the currently active request |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3657 </listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3658 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3659 <listitem> |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3660 <literal>cache</literal> — request cache object for caching upstream response |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3661 </listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3662 |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3663 <listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3664 <literal>upstream</literal> — request upstream object for proxying |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3665 </listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3666 |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3667 <listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3668 <literal>pool</literal> — request pool. |
1929
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3669 This pool is destroyed when the request is deleted. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3670 The request object itself is allocated in this pool. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3671 For allocations which should be available throughout the client connection's |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3672 lifetime, <literal>ngx_connection_t</literal>'s pool should be used instead |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3673 </listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3674 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3675 <listitem> |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3676 <literal>header_in</literal> — buffer where client HTTP request header in read |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3677 </listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3678 |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3679 <listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3680 <literal>headers_in, headers_out</literal> — input and output HTTP headers |
1929
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3681 objects. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3682 Both objects contain the <literal>headers</literal> field of type |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3683 <literal>ngx_list_t</literal> keeping the raw list of headers. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3684 In addition to that, specific headers are available for getting and setting as |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3685 separate fields, for example <literal>content_length_n</literal>, |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3686 <literal>status</literal> etc |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3687 </listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3688 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3689 <listitem> |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3690 <literal>request_body</literal> — client request body object |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3691 </listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3692 |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3693 <listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3694 <literal>start_sec, start_msec</literal> — time point when the request was |
1929
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3695 created. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3696 Used for tracking request duration |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3697 </listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3698 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3699 <listitem> |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3700 <literal>method, method_name</literal> — numeric and textual representation of |
1929
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3701 client HTTP request method. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3702 Numeric values for methods are defined in |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3703 <literal>src/http/ngx_http_request.h</literal> with macros |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3704 <literal>NGX_HTTP_GET, NGX_HTTP_HEAD, NGX_HTTP_POST</literal> etc |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3705 </listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3706 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3707 <listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3708 <literal>http_protocol, http_version, http_major, http_minor</literal> - |
1936
8f996938fe23
Style: proper quotes usage.
Vladimir Homutov <vl@nginx.com>
parents:
1935
diff
changeset
|
3709 client HTTP protocol version in its original textual form (“HTTP/1.0”, |
8f996938fe23
Style: proper quotes usage.
Vladimir Homutov <vl@nginx.com>
parents:
1935
diff
changeset
|
3710 “HTTP/1.1” etc), numeric form (<literal>NGX_HTTP_VERSION_10</literal>, |
1929
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3711 <literal>NGX_HTTP_VERSION_11</literal> etc) and separate major and minor |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3712 versions |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3713 </listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3714 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3715 <listitem> |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3716 <literal>request_line, unparsed_uri</literal> — client original request line |
1929
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3717 and URI |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3718 </listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3719 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3720 <listitem> |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3721 <literal>uri, args, exten</literal> — current request URI, arguments and file |
1929
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3722 extention. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3723 The URI value here might differ from the original URI sent by the client due to |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3724 normalization. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3725 Throughout request processing, these value can change while performing internal |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3726 redirects |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3727 </listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3728 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3729 <listitem> |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3730 <literal>main</literal> — pointer to a main request object. |
1929
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3731 This object is created to process client HTTP request, as opposed to |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3732 subrequests, created to perform a specific sub-task within the main request |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3733 </listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3734 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3735 <listitem> |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3736 <literal>parent</literal> — pointer to a parent request of a subrequest |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3737 </listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3738 |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3739 <listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3740 <literal>postponed</literal> — list of output buffers and subrequests in the |
1929
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3741 order they are sent and created. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3742 The list is used by the postpone filter to provide consistent request output, |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3743 when parts of it are created by subrequests |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3744 </listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3745 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3746 <listitem> |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3747 <literal>post_subrequest</literal> — pointer to a handler with context to be |
1929
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3748 called when a subrequest gets finalized. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3749 Unused for main requests |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3750 </listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3751 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3752 <listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3753 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3754 <para> |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3755 <literal>posted_requests</literal> — list of requests to be started or |
1929
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3756 resumed. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3757 Starting or resuming is done by calling the request's |
1932 | 3758 <literal>write_event_handler</literal>. |
1929
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3759 Normally, this handler holds the request main function, which at first runs |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3760 request phases and then produces the output. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3761 </para> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3762 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3763 <para> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3764 A request is usually posted by the |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3765 <literal>ngx_http_post_request(r, NULL)</literal> call. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3766 It is always posted to the main request <literal>posted_requests</literal> list. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3767 The function <literal>ngx_http_run_posted_requests(c)</literal> runs all |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3768 requests, posted in the main request of the passed connection's active request. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3769 This function should be called in all event handlers, which can lead to new |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3770 posted requests. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3771 Normally, it's called always after invoking a request's read or write handler |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3772 </para> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3773 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3774 </listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3775 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3776 <listitem> |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3777 <literal>phase_handler</literal> — index of current request phase |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3778 </listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3779 |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3780 <listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3781 <literal>ncaptures, captures, captures_data</literal> — regex captures produced |
1929
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3782 by the last regex match of the request. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3783 While processing a request, there's a number of places where a regex match can |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3784 happen: map lookup, server lookup by SNI or HTTP Host, rewrite, proxy_redirect |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3785 etc. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3786 Captures produced by a lookup are stored in the above mentioned fields. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3787 The field <literal>ncaptures</literal> holds the number of captures, |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3788 <literal>captures</literal> holds captures boundaries, |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3789 <literal>captures_data</literal> holds a string, against which the regex was |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3790 matched and which should be used to extract captures. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3791 After each new regex match request captures are reset to hold new values |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3792 </listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3793 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3794 <listitem> |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3795 <literal>count</literal> — request reference counter. |
1929
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3796 The field only makes sense for the main request. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3797 Increasing the counter is done by simple <literal>r->main->count++</literal>. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3798 To decrease the counter <literal>ngx_http_finalize_request(r, rc)</literal> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3799 should be called. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3800 Creation of a subrequest or running request body read process increase the |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3801 counter |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3802 </listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3803 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3804 <listitem> |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3805 <literal>subrequests</literal> — current subrequest nesting level. |
1929
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3806 Each subrequest gets the nesting level of its parent decreased by one. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3807 Once the value reaches zero an error is generated. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3808 The value for the main request is defined by the |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3809 <literal>NGX_HTTP_MAX_SUBREQUESTS</literal> constant |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3810 </listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3811 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3812 <listitem> |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3813 <literal>uri_changes</literal> — number of URI changes left for the request. |
1929
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3814 The total number of times a request can change its URI is limited by the |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3815 <literal>NGX_HTTP_MAX_URI_CHANGES</literal> constant. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3816 With each change the value is decreased until it reaches zero. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3817 In the latter case an error is generated. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3818 The actions considered as URI changes are rewrites and internal redirects to |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3819 normal or named locations |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3820 </listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3821 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3822 <listitem> |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3823 <literal>blocked</literal> — counter of blocks held on the request. |
1929
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3824 While this value is non-zero, request cannot be terminated. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3825 Currently, this value is increased by pending AIO operations (POSIX AIO and |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3826 thread operations) and active cache lock |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3827 </listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3828 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3829 <listitem> |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3830 <literal>buffered</literal> — bitmask showing which modules have buffered the |
1929
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3831 output produced by the request. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3832 A number of filters can buffer output, for example sub_filter can buffer data |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3833 due to a partial string match, copy filter can buffer data because of the lack |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3834 of free output_buffers etc. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3835 As long as this value is non-zero, request is not finalized, expecting the flush |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3836 </listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3837 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3838 <listitem> |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3839 <literal>header_only</literal> — flag showing that output does not require body. |
1929
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3840 For example, this flag is used by HTTP HEAD requests |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3841 </listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3842 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3843 <listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3844 <para> |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3845 <literal>keepalive</literal> — flag showing if client connection keepalive is |
1929
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3846 supported. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3847 The value is inferred from HTTP version and <header>Connection</header> header |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3848 value |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3849 </para> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3850 </listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3851 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3852 <listitem> |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3853 <literal>header_sent</literal> — flag showing that output header has already |
1929
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3854 been sent by the request |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3855 </listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3856 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3857 <listitem> |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3858 <literal>internal</literal> — flag showing that current request is internal. |
1929
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3859 To enter the internal state, a request should pass through an internal |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3860 redirect or be a subrequest. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3861 Internal requests are allowed to enter internal locations |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3862 </listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3863 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3864 <listitem> |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3865 <literal>allow_ranges</literal> — flag showing that partial response can be |
1929
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3866 sent to client, if requested by the HTTP Range header |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3867 </listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3868 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3869 <listitem> |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3870 <literal>subrequest_ranges</literal> — flag showing that a partial response is |
1929
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3871 allowed to be sent while processing a subrequest |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3872 </listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3873 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3874 <listitem> |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3875 <literal>single_range</literal> — flag showing that only a single continuous |
1929
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3876 range of output data can be sent to the client. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3877 This flag is usually set when sending a stream of data, for example from a |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3878 proxied server, and the entire response is not available at once |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3879 </listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3880 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3881 <listitem> |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3882 <literal>main_filter_need_in_memory, filter_need_in_memory</literal> — flags |
1929
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3883 showing that the output should be produced in memory buffers but not in files. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3884 This is a signal to the copy filter to read data from file buffers even if |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3885 sendfile is enabled. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3886 The difference between these two flags is the location of filter modules which |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3887 set them. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3888 Filters called before the postpone filter in filter chain, set |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3889 <literal>filter_need_in_memory</literal> requesting that only the current |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3890 request output should come in memory buffers. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3891 Filters called later in filter chain set |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3892 <literal>main_filter_need_in_memory</literal> requiring that both the main |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3893 request and all the subrequest read files in memory while sending output |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3894 </listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3895 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3896 <listitem> |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
3897 <literal>filter_need_temporary</literal> — flag showing that the request output |
1929
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3898 should be produced in temporary buffers, but not in readonly memory buffers or |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3899 file buffers. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3900 This is used by filters which may change output directly in the buffers, where |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3901 it's sent </listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3902 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3903 </list> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3904 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3905 </section> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3906 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3907 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3908 <section name="Configuration" id="http_conf"> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3909 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3910 <para> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3911 Each HTTP module may have three types of configuration: |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3912 </para> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3913 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3914 <list type="bullet"> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3915 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3916 <listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3917 Main configuration. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3918 This configuration applies to the entire nginx http{} block. This is global |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3919 configuration. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3920 It stores global settings for a module |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3921 </listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3922 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3923 <listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3924 Server configuration. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3925 This configuraion applies to a single nginx server{}. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3926 It stores server-specific settings for a module |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3927 </listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3928 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3929 <listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3930 Location configuration. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3931 This configuraion applies to a single location{}, if{} or limit_except() block. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3932 This configuration stores settings specific to a location |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3933 </listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3934 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3935 </list> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3936 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3937 <para> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3938 Configuration structures are created at nginx configuration stage by calling |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3939 functions, which allocate these structures, initialize them and merge. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3940 The following example shows how to create a simple module location |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3941 configuration. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3942 The configuration has one setting <literal>foo</literal> of unsiged integer |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3943 type. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3944 </para> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3945 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3946 <programlisting> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3947 typedef struct { |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3948 ngx_uint_t foo; |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3949 } ngx_http_foo_loc_conf_t; |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3950 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3951 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3952 static ngx_http_module_t ngx_http_foo_module_ctx = { |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3953 NULL, /* preconfiguration */ |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3954 NULL, /* postconfiguration */ |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3955 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3956 NULL, /* create main configuration */ |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3957 NULL, /* init main configuration */ |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3958 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3959 NULL, /* create server configuration */ |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3960 NULL, /* merge server configuration */ |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3961 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3962 ngx_http_foo_create_loc_conf, /* create location configuration */ |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3963 ngx_http_foo_merge_loc_conf /* merge location configuration */ |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3964 }; |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3965 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3966 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3967 static void * |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3968 ngx_http_foo_create_loc_conf(ngx_conf_t *cf) |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3969 { |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3970 ngx_http_foo_loc_conf_t *conf; |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3971 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3972 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_foo_loc_conf_t)); |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3973 if (conf == NULL) { |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3974 return NULL; |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3975 } |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3976 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3977 conf->foo = NGX_CONF_UNSET_UINT; |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3978 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3979 return conf; |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3980 } |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3981 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3982 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3983 static char * |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3984 ngx_http_foo_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3985 { |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3986 ngx_http_foo_loc_conf_t *prev = parent; |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3987 ngx_http_foo_loc_conf_t *conf = child; |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3988 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3989 ngx_conf_merge_uint_value(conf->foo, prev->foo, 1); |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3990 } |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3991 </programlisting> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3992 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3993 <para> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3994 As seen in the example, <literal>ngx_http_foo_create_loc_conf()</literal> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3995 function creates a new configuration structure and |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3996 <literal>ngx_http_foo_merge_loc_conf()</literal> merges a configuration with |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3997 another configuration from a higher level. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3998 In fact, server and location configuration do not only exist at server and |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
3999 location levels, but also created for all the levels above. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4000 Specifically, a server configuration is created at the main level as well and |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4001 location configurations are created for main, server and location levels. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4002 These configurations make it possible to specify server and location-specific |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4003 settings at any level of nginx configuration file. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4004 Eventually configurations are merged down. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4005 To indicate a missing setting and ignore it while merging, nginx provides a |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4006 number of macros like <literal>NGX_CONF_UNSET</literal> and |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4007 <literal>NGX_CONF_UNSET_UINT</literal>. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4008 Standard nginx merge macros like <literal>ngx_conf_merge_value()</literal> and |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4009 <literal>ngx_conf_merge_uint_value()</literal> provide a convenient way to |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4010 merge a setting and set the default value if none of configurations provided an |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4011 explicit value. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4012 For complete list of macros for different types see |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4013 <literal>src/core/ngx_conf_file.h</literal>. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4014 </para> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4015 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4016 <para> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4017 To access configuration of any HTTP module at configuration time, the following |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4018 macros are available. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4019 They receive <literal>ngx_conf_t</literal> reference as the first argument. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4020 </para> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4021 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4022 <list type="bullet"> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4023 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4024 <listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4025 <literal>ngx_http_conf_get_module_main_conf(cf, module)</literal> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4026 </listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4027 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4028 <listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4029 <literal>ngx_http_conf_get_module_srv_conf(cf, module)</literal> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4030 </listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4031 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4032 <listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4033 <literal>ngx_http_conf_get_module_loc_conf(cf, module)</literal> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4034 </listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4035 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4036 </list> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4037 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4038 <para> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4039 The following example gets a pointer to a location configuration of |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4040 standard nginx core module |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4041 <link doc="../http/ngx_http_core_module.xml">ngx_http_core_module</link> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4042 and changes |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4043 location content handler kept in the <literal>handler</literal> field of the |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4044 structure. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4045 </para> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4046 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4047 <programlisting> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4048 static ngx_int_t ngx_http_foo_handler(ngx_http_request_t *r); |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4049 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4050 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4051 static ngx_command_t ngx_http_foo_commands[] = { |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4052 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4053 { ngx_string("foo"), |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4054 NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS, |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4055 ngx_http_foo, |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4056 0, |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4057 0, |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4058 NULL }, |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4059 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4060 ngx_null_command |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4061 }; |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4062 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4063 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4064 static char * |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4065 ngx_http_foo(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4066 { |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4067 ngx_http_core_loc_conf_t *clcf; |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4068 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4069 clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module); |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4070 clcf->handler = ngx_http_bar_handler; |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4071 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4072 return NGX_CONF_OK; |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4073 } |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4074 </programlisting> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4075 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4076 <para> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4077 In runtime the following macros are available to get configurations of HTTP |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4078 modules. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4079 </para> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4080 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4081 <list type="bullet"> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4082 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4083 <listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4084 <literal>ngx_http_get_module_main_conf(r, module)</literal> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4085 </listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4086 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4087 <listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4088 <literal>ngx_http_get_module_srv_conf(r, module)</literal> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4089 </listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4090 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4091 <listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4092 <literal>ngx_http_get_module_loc_conf(r, module)</literal> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4093 </listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4094 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4095 </list> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4096 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4097 <para> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4098 These macros receive a reference to an HTTP request |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4099 <literal>ngx_http_request_t</literal>. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4100 Main configuration of a request never changes. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4101 Server configuration may change from a default one after choosing a virtual |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4102 server for a request. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4103 Request location configuration may change multiple times as a result of a |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4104 rewrite or internal redirect. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4105 The following example shows how to access HTTP configuration in runtime. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4106 </para> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4107 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4108 <programlisting> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4109 static ngx_int_t |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4110 ngx_http_foo_handler(ngx_http_request_t *r) |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4111 { |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4112 ngx_http_foo_loc_conf_t *flcf; |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4113 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4114 flcf = ngx_http_get_module_loc_conf(r, ngx_http_foo_module); |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4115 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4116 ... |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4117 } |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4118 </programlisting> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4119 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4120 </section> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4121 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4122 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4123 <section name="Phases" id="http_phases"> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4124 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4125 <para> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4126 Each HTTP request passes through a list of HTTP phases. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4127 Each phase is specialized in a particular type of processing. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4128 Most phases allow installing handlers. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4129 The phase handlers are called successively once the request reaches the phase. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4130 Many standard nginx modules install their phase handlers as a way to get called |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4131 at a specific request processing stage. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4132 Following is the list of nginx HTTP phases. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4133 </para> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4134 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4135 <list type="bullet"> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4136 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4137 <listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4138 <literal>NGX_HTTP_POST_READ_PHASE</literal> is the earliest phase. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4139 The <link doc="../http/ngx_http_realip_module.xml">ngx_http_realip_module</link> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4140 installs its handler at this phase. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4141 This allows to substitute client address before any other module is invoked |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4142 </listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4143 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4144 <listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4145 <literal>NGX_HTTP_SERVER_REWRITE_PHASE</literal> is used to run rewrite script, |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4146 defined at the server level, that is out of any location block. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4147 The |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4148 <link doc="../http/ngx_http_rewrite_module.xml">ngx_http_rewrite_module</link> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4149 installs its handler at this phase |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4150 </listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4151 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4152 <listitem> |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
4153 <literal>NGX_HTTP_FIND_CONFIG_PHASE</literal> — a special phase used to choose a |
1929
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4154 location based on request URI. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4155 This phase does not allow installing any handlers. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4156 It only performs the default action of choosing a location. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4157 Before this phase, the server default location is assigned to the request. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4158 Any module requesting a location configuration, will receive the default server |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4159 location configuration. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4160 After this phase a new location is assigned to the request |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4161 </listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4162 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4163 <listitem> |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
4164 <literal>NGX_HTTP_REWRITE_PHASE</literal> — same as |
1929
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4165 <literal>NGX_HTTP_SERVER_REWRITE_PHASE</literal>, but for a new location, |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4166 chosen at the prevous phase |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4167 </listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4168 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4169 <listitem> |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
4170 <literal>NGX_HTTP_POST_REWRITE_PHASE</literal> — a special phase, used to |
1929
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4171 redirect the request to a new location, if the URI was changed during rewrite. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4172 The redirect is done by going back to |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4173 <literal>NGX_HTTP_FIND_CONFIG_PHASE</literal>. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4174 No handlers are allowed at this phase |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4175 </listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4176 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4177 <listitem> |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
4178 <literal>NGX_HTTP_PREACCESS_PHASE</literal> — a common phase for different |
1929
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4179 types of handlers, not associated with access check. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4180 Standard nginx modules |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4181 <link doc="../http/ngx_http_limit_conn_module.xml">ngx_http_limit_conn_module |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4182 </link> and |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4183 <link doc="../http/ngx_http_limit_req_module.xml"> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4184 ngx_http_limit_req_module</link> register their handlers at this phase |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4185 </listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4186 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4187 <listitem> |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
4188 <literal>NGX_HTTP_ACCESS_PHASE</literal> — used to check access permissions |
1929
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4189 for the request. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4190 Standard nginx modules such as |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4191 <link doc="../http/ngx_http_access_module.xml">ngx_http_access_module</link> and |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4192 <link doc="../http/ngx_http_auth_basic_module.xml">ngx_http_auth_basic_module |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4193 </link> register their handlers at this phase. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4194 If configured so by the |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4195 <link doc="../http/ngx_http_core_module.xml" id="satisfy"/> directive, only one |
1971
5fb870087b76
Fixed typo and removed trailing spaces.
Vladimir Homutov <vl@nginx.com>
parents:
1970
diff
changeset
|
4196 of access phase handlers may allow access to the request in order to continue |
1929
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4197 processing |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4198 </listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4199 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4200 <listitem> |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
4201 <literal>NGX_HTTP_POST_ACCESS_PHASE</literal> — a special phase for the |
1929
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4202 <link doc="../http/ngx_http_core_module.xml" id="satisfy">satisfy any</link> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4203 case. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4204 If some access phase handlers denied the access and none of them allowed, the |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4205 request is finalized. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4206 No handlers are supported at this phase |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4207 </listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4208 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4209 <listitem> |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
4210 <literal>NGX_HTTP_TRY_FILES_PHASE</literal> — a special phase, for the |
1929
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4211 <link doc="../http/ngx_http_core_module.xml" id="try_files"/> feature. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4212 No handlers are allowed at this phase |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4213 </listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4214 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4215 <listitem> |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
4216 <literal>NGX_HTTP_CONTENT_PHASE</literal> — a phase, at which the response |
1929
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4217 is supposed to be generated. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4218 Multiple nginx standard modules register their handers at this phase, for |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4219 example |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4220 <link doc="../http/ngx_http_index_module.xml">ngx_http_index_module</link> or |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4221 <literal>ngx_http_static_module</literal>. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4222 All these handlers are called sequentially until one of them finally produces |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4223 the output. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4224 It's also possible to set content handlers on a per-location basis. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4225 If the |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4226 <link doc="../http/ngx_http_core_module.xml">ngx_http_core_module</link>'s |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4227 location configuration has <literal>handler</literal> set, this handler is |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4228 called as the content handler and content phase handlers are ignored |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4229 </listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4230 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4231 <listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4232 <literal>NGX_HTTP_LOG_PHASE</literal> is used to perform request logging. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4233 Currently, only the |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4234 <link doc="../http/ngx_http_log_module.xml">ngx_http_log_module</link> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4235 registers its handler |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4236 at this stage for access logging. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4237 Log phase handlers are called at the very end of request processing, right |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4238 before freeing the request |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4239 </listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4240 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4241 </list> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4242 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4243 <para> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4244 Following is the example of a preaccess phase handler. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4245 </para> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4246 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4247 <programlisting> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4248 static ngx_http_module_t ngx_http_foo_module_ctx = { |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4249 NULL, /* preconfiguration */ |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4250 ngx_http_foo_init, /* postconfiguration */ |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4251 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4252 NULL, /* create main configuration */ |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4253 NULL, /* init main configuration */ |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4254 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4255 NULL, /* create server configuration */ |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4256 NULL, /* merge server configuration */ |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4257 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4258 NULL, /* create location configuration */ |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4259 NULL /* merge location configuration */ |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4260 }; |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4261 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4262 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4263 static ngx_int_t |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4264 ngx_http_foo_handler(ngx_http_request_t *r) |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4265 { |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4266 ngx_str_t *ua; |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4267 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4268 ua = r->headers_in->user_agent; |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4269 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4270 if (ua == NULL) { |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4271 return NGX_DECLINED; |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4272 } |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4273 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4274 /* reject requests with "User-Agent: foo" */ |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4275 if (ua->value.len == 3 && ngx_strncmp(ua->value.data, "foo", 3) == 0) { |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4276 return NGX_HTTP_FORBIDDEN; |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4277 } |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4278 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4279 return NGX_DECLINED; |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4280 } |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4281 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4282 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4283 static ngx_int_t |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4284 ngx_http_foo_init(ngx_conf_t *cf) |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4285 { |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4286 ngx_http_handler_pt *h; |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4287 ngx_http_core_main_conf_t *cmcf; |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4288 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4289 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module); |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4290 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4291 h = ngx_array_push(&cmcf->phases[NGX_HTTP_PREACCESS_PHASE].handlers); |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4292 if (h == NULL) { |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4293 return NGX_ERROR; |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4294 } |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4295 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4296 *h = ngx_http_foo_handler; |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4297 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4298 return NGX_OK; |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4299 } |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4300 </programlisting> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4301 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4302 <para> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4303 Phase handlers are expected to return specific codes: |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4304 </para> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4305 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4306 <list type="bullet"> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4307 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4308 <listitem> |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
4309 <literal>NGX_OK</literal> — proceed to the next phase |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
4310 </listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
4311 |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
4312 <listitem> |
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
4313 <literal>NGX_DECLINED</literal> — proceed to the next handler of the current |
1929
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4314 phase. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4315 If current handler is the last in current phase, move to the next phase |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4316 </listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4317 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4318 <listitem> |
1935
be7490a66d1b
Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents:
1932
diff
changeset
|
4319 <literal>NGX_AGAIN, NGX_DONE</literal> — suspend phase handling until some |
1929
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4320 future event. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4321 This can be for example asynchronous I/O operation or just a delay. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4322 It is supposed, that phase handling will be resumed later by calling |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4323 <literal>ngx_http_core_run_phases()</literal> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4324 </listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4325 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4326 <listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4327 Any other value returned by the phase handler is treated as a request |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4328 finalization code, in particular, HTTP response code. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4329 The request is finalized with the code provided |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4330 </listitem> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4331 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4332 </list> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4333 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4334 <para> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4335 Some phases treat return codes in a slightly different way. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4336 At content phase, any return code other that <literal>NGX_DECLINED</literal> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4337 is considered a finalization code. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4338 As for the location content handlers, any return from them is considered a |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4339 finalization code. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4340 At access phase, in |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4341 <link doc="../http/ngx_http_core_module.xml" id="satisfy">satisfy any</link> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4342 mode, returning a code other |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4343 than <literal>NGX_OK, NGX_DECLINED, NGX_AGAIN, NGX_DONE</literal> is considered |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4344 a denial. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4345 If none of future access handlers allow access or deny with a new |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4346 code, the denial code will become the finalization code. |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4347 </para> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4348 |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4349 </section> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
4350 |
1939
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
4351 |
1959
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4352 <section name="Variables" id="http_variables"> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4353 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4354 <section name="Accessing existing variables" id="http_existing_variables"> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4355 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4356 <para> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4357 Variables may be referenced using index (this is the most common method) |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4358 or names (see below in the section about creating variables). |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4359 Index is created at configuration stage, when a variable is added |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4360 to configuration. |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4361 The variable index can be obtained using |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4362 <literal>ngx_http_get_variable_index()</literal>: |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4363 <programlisting> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4364 ngx_str_t name; /* ngx_string("foo") */ |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4365 ngx_int_t index; |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4366 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4367 index = ngx_http_get_variable_index(cf, &name); |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4368 </programlisting> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4369 Here, the <literal>cf</literal> is a pointer to nginx configuration and the |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4370 <literal>name</literal> points to a string with the variable name. |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4371 The function returns <literal>NGX_ERROR</literal> on error or valid index |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4372 otherwise, which is typically stored somewhere in a module configuration for |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4373 future use. |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4374 </para> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4375 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4376 <para> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4377 All HTTP variables are evaluated in the context of HTTP request and results |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4378 are specific to and cached in HTTP request. |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4379 All functions that evaluate variables return |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4380 <literal>ngx_http_variable_value_t</literal> type, representing |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4381 the variable value: |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4382 <programlisting> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4383 typedef ngx_variable_value_t ngx_http_variable_value_t; |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4384 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4385 typedef struct { |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4386 unsigned len:28; |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4387 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4388 unsigned valid:1; |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4389 unsigned no_cacheable:1; |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4390 unsigned not_found:1; |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4391 unsigned escape:1; |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4392 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4393 u_char *data; |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4394 } ngx_variable_value_t; |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4395 </programlisting> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4396 where: |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4397 <list type="bullet"> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4398 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4399 <listitem> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4400 <literal>len</literal> — length of a value |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4401 </listitem> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4402 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4403 <listitem> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4404 <literal>data</literal> — value itself |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4405 </listitem> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4406 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4407 <listitem> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4408 <literal>valid</literal> — value is valid |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4409 </listitem> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4410 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4411 <listitem> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4412 <literal>not_found</literal> — variable was not found and thus |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4413 the <literal>data</literal> and <literal>len</literal> fields are irrelevant; |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4414 this may happen, for example, with such variables as <var>$arg_foo</var> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4415 when a corresponding argument was not passed in a request |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4416 </listitem> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4417 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4418 <listitem> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4419 <literal>no_cacheable</literal> — do not cache result |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4420 </listitem> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4421 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4422 <listitem> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4423 <literal>escape</literal> — used internally by the logging module to mark |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4424 values that require escaping on output |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4425 </listitem> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4426 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4427 </list> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4428 </para> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4429 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4430 <para> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4431 The <literal>ngx_http_get_flushed_variable()</literal> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4432 and <literal>ngx_http_get_indexed_variable()</literal> functions |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4433 are used to obtain the variable value. |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4434 They have the same interface - accepting a HTTP request <literal>r</literal> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4435 as a context for evaluating the variable and an <literal>index</literal>, |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4436 identifying it. |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4437 Example of typical usage: |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4438 <programlisting> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4439 ngx_http_variable_value_t *v; |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4440 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4441 v = ngx_http_get_flushed_variable(r, index); |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4442 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4443 if (v == NULL || v->not_found) { |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4444 /* we failed to get value or there is no such variable, handle it */ |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4445 return NGX_ERROR; |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4446 } |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4447 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4448 /* some meaningful value is found */ |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4449 </programlisting> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4450 The difference between functions is that the |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4451 <literal>ngx_http_get_indexed_variable()</literal> returns cached value |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4452 and <literal>ngx_http_get_flushed_variable()</literal> flushes cache for |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4453 non-cacheable variables. |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4454 </para> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4455 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4456 <para> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4457 There are cases when it is required to deal with variables which names are |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4458 not known at configuration time and thus they cannot be accessed using indexes, |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4459 for example in modules like SSI or Perl. |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4460 The <literal>ngx_http_get_variable(r, name, key)</literal> function may be |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4461 used in such cases. |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4462 It searches for the <literal>variable</literal> with a given |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4463 <literal>name</literal> and its hash <literal>key</literal>. |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4464 </para> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4465 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4466 </section> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4467 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4468 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4469 <section name="Creating variables" id="http_creating_variables"> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4470 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4471 <para> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4472 To create a variable <literal>ngx_http_add_variable()</literal> function |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4473 is used. |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4474 It takes configuration (where variable is registered), variable name and |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4475 flags that control its behaviour: |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4476 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4477 <list type="bullet"> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4478 <listitem><literal>NGX_HTTP_VAR_CHANGEABLE</literal> — allows redefining |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4479 the variable; If another module will define a variable with such name, |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4480 no conflict will happen. |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4481 For example, this allows user to override variables using the |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4482 <link doc="../http/ngx_http_rewrite_module.xml" id="set"/> directive. |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4483 </listitem> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4484 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4485 <listitem><literal>NGX_HTTP_VAR_NOCACHEABLE</literal> — disables caching, |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4486 is useful for such variables as <literal>$time_local</literal> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4487 </listitem> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4488 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4489 <listitem><literal>NGX_HTTP_VAR_NOHASH</literal> — indicates that |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4490 this variable is only accessible by index, not by name. |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4491 This is a small optimization which may be used when it is known that the |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4492 variable is not needed in modules like SSI or Perl. |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4493 </listitem> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4494 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4495 <listitem><literal>NGX_HTTP_VAR_PREFIX</literal> — the name of this |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4496 variable is a prefix. |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4497 A handler must implement additional logic to obtain value of specific |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4498 variable. |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4499 For example, all “<literal>arg_</literal>” variables are processed by the |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4500 same handler which performs lookup in request arguments and returns value |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4501 of specific argument. |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4502 </listitem> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4503 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4504 </list> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4505 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4506 The function returns NULL in case of error or a pointer to |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4507 <literal>ngx_http_variable_t</literal>: |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4508 <programlisting> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4509 struct ngx_http_variable_s { |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4510 ngx_str_t name; |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4511 ngx_http_set_variable_pt set_handler; |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4512 ngx_http_get_variable_pt get_handler; |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4513 uintptr_t data; |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4514 ngx_uint_t flags; |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4515 ngx_uint_t index; |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4516 }; |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4517 </programlisting> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4518 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4519 The <literal>get</literal> and <literal>set</literal> handlers |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4520 are called to obtain or set the variable value, |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4521 <literal>data</literal> will be passed to variable handlers, |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4522 <literal>index</literal> will hold assigned variable index, used to reference |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4523 the variable. |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4524 </para> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4525 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4526 <para> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4527 Usually, a null-terminated static array of such structures is created |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4528 by a module and processed at the preconfiguration stage to add variables |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4529 into configuration: |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4530 <programlisting> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4531 static ngx_http_variable_t ngx_http_foo_vars[] = { |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4532 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4533 { ngx_string("foo_v1"), NULL, ngx_http_foo_v1_variable, NULL, 0, 0 }, |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4534 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4535 { ngx_null_string, NULL, NULL, 0, 0, 0 } |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4536 }; |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4537 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4538 static ngx_int_t |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4539 ngx_http_foo_add_variables(ngx_conf_t *cf) |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4540 { |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4541 ngx_http_variable_t *var, *v; |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4542 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4543 for (v = ngx_http_foo_vars; v->name.len; v++) { |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4544 var = ngx_http_add_variable(cf, &v->name, v->flags); |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4545 if (var == NULL) { |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4546 return NGX_ERROR; |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4547 } |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4548 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4549 var->get_handler = v->get_handler; |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4550 var->data = v->data; |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4551 } |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4552 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4553 return NGX_OK; |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4554 } |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4555 </programlisting> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4556 This function is used to initialize the <literal>preconfiguration</literal> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4557 field of the HTTP module context and is called before parsing HTTP configuration, |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4558 so it could refer to these variables. |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4559 </para> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4560 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4561 <para> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4562 The <literal>get</literal> handler is responsible for evaluating the variable |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4563 in a context of specific request, for example: |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4564 <programlisting> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4565 static ngx_int_t |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4566 ngx_http_variable_connection(ngx_http_request_t *r, |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4567 ngx_http_variable_value_t *v, uintptr_t data) |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4568 { |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4569 u_char *p; |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4570 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4571 p = ngx_pnalloc(r->pool, NGX_ATOMIC_T_LEN); |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4572 if (p == NULL) { |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4573 return NGX_ERROR; |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4574 } |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4575 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4576 v->len = ngx_sprintf(p, "%uA", r->connection->number) - p; |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4577 v->valid = 1; |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4578 v->no_cacheable = 0; |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4579 v->not_found = 0; |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4580 v->data = p; |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4581 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4582 return NGX_OK; |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4583 } |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4584 </programlisting> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4585 It returns <literal>NGX_ERROR</literal> in case of internal error |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4586 (for example, failed memory allocation) or <literal>NGX_OK</literal> otherwise. |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4587 The status of variable evaluation may be understood by inspecting flags |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4588 of the <literal>ngx_http_variable_value_t</literal> (see description above). |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4589 </para> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4590 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4591 <para> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4592 The <literal>set</literal> handler allows setting the property |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4593 referred by the variable. |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4594 For example, the <literal>$limit_rate</literal> variable set handler |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4595 modifies the request's <literal>limit_rate</literal> field: |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4596 <programlisting> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4597 ... |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4598 { ngx_string("limit_rate"), ngx_http_variable_request_set_size, |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4599 ngx_http_variable_request_get_size, |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4600 offsetof(ngx_http_request_t, limit_rate), |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4601 NGX_HTTP_VAR_CHANGEABLE|NGX_HTTP_VAR_NOCACHEABLE, 0 }, |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4602 ... |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4603 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4604 static void |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4605 ngx_http_variable_request_set_size(ngx_http_request_t *r, |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4606 ngx_http_variable_value_t *v, uintptr_t data) |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4607 { |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4608 ssize_t s, *sp; |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4609 ngx_str_t val; |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4610 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4611 val.len = v->len; |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4612 val.data = v->data; |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4613 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4614 s = ngx_parse_size(&val); |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4615 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4616 if (s == NGX_ERROR) { |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4617 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4618 "invalid size \"%V\"", &val); |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4619 return; |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4620 } |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4621 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4622 sp = (ssize_t *) ((char *) r + data); |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4623 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4624 *sp = s; |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4625 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4626 return; |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4627 } |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4628 </programlisting> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4629 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4630 </para> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4631 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4632 </section> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4633 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4634 </section> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4635 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4636 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4637 <section name="Complex values" id="http_complex_values"> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4638 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4639 <para> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4640 A complex value, despite its name, provides an easy way to evaluate |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4641 expressions that may contain text, variables, and their combination. |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4642 </para> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4643 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4644 <para> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4645 The complex value description in |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4646 <literal>ngx_http_compile_complex_value</literal> is compiled at the |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4647 configuration stage into <literal>ngx_http_complex_value_t</literal> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4648 which is used at runtime to obtain evaluated expression results. |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4649 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4650 <programlisting> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4651 ngx_str_t *value; |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4652 ngx_http_complex_value_t cv; |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4653 ngx_http_compile_complex_value_t ccv; |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4654 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4655 value = cf->args->elts; /* directive arguments */ |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4656 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4657 ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t)); |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4658 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4659 ccv.cf = cf; |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4660 ccv.value = &value[1]; |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4661 ccv.complex_value = &cv; |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4662 ccv.zero = 1; |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4663 ccv.conf_prefix = 1; |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4664 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4665 if (ngx_http_compile_complex_value(&ccv) != NGX_OK) { |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4666 return NGX_CONF_ERROR; |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4667 } |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4668 </programlisting> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4669 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4670 Here, <literal>ccv</literal> holds all parameters that are required to |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4671 initialize the complex value <literal>cv</literal>: |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4672 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4673 <list type="bullet"> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4674 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4675 <listitem> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4676 <literal>cf</literal> — configuration pointer |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4677 </listitem> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4678 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4679 <listitem> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4680 <literal>value</literal> — string for parsing (input) |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4681 </listitem> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4682 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4683 <listitem> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4684 <literal>complex_value</literal> — compiled value (output) |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4685 </listitem> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4686 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4687 <listitem> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4688 <literal>zero</literal> — flag that enables zero-terminating value |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4689 </listitem> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4690 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4691 <listitem> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4692 <literal>conf_prefix</literal> — prefixes result with configuration prefix |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4693 (the directory where nginx is currently looking for configuration) |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4694 </listitem> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4695 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4696 <listitem> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4697 <literal>root_prefix</literal> — prefixes result with root prefix |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4698 (this is the normal nginx installation prefix) |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4699 </listitem> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4700 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4701 </list> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4702 The <literal>zero</literal> flag is usable when results are to be passed to |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4703 libraries that require zero-terminated strings, and prefixes are handy when |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4704 dealing with filenames. |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4705 </para> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4706 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4707 <para> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4708 Upon successful compilation, <literal>cv.lengths</literal> may |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4709 be inspected to get information about the presence of variables |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4710 in the expression. |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4711 The NULL value means that the expression contained static text only, |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4712 and there is no need in storing it as a complex value, |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4713 so a simple string can be used. |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4714 </para> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4715 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4716 <para> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4717 The <literal>ngx_http_set_complex_value_slot()</literal> is a convenient |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4718 function used to initialize complex value completely right in the directive |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4719 declaration. |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4720 </para> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4721 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4722 <para> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4723 At runtime, a complex value may be calculated using the |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4724 <literal>ngx_http_complex_value()</literal> function: |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4725 <programlisting> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4726 ngx_str_t res; |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4727 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4728 if (ngx_http_complex_value(r, &cv, &res) != NGX_OK) { |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4729 return NGX_ERROR; |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4730 } |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4731 </programlisting> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4732 Given the request <literal>r</literal> and previously compiled |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4733 value <literal>cv</literal> the function will evaluate |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4734 expression and put result into <literal>res</literal>. |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4735 </para> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4736 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4737 </section> |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4738 |
d0aebb2337ec
Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1958
diff
changeset
|
4739 |
1967
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4740 <section name="Request redirection" id="http_request_redirection"> |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4741 |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4742 <para> |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4743 An HTTP request is always connected to a location via the |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4744 <literal>loc_conf</literal> field of the <literal>ngx_http_request_t</literal> |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4745 structure. |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4746 This means that at any point the location configuration of any module can be |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4747 retrieved from the request by calling |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4748 <literal>ngx_http_get_module_loc_conf(r, module)</literal>. |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4749 Request location may be changed several times throughout its lifetime. |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4750 Initially, a default server location of the default server is assigned to a |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4751 request. |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4752 Once a request switches to a different server (chosen by the HTTP |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4753 <header>Host</header> header or SSL SNI extension), the request switches to the |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4754 default location of that server as well. |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4755 The next change of the location takes place at the |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4756 <literal>NGX_HTTP_FIND_CONFIG_PHASE</literal> request phase. |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4757 At this phase a location is chosen by request URI among all non-named locations |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4758 configured for the server. |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4759 The |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4760 <link doc="../http/ngx_http_rewrite_module.xml">ngx_http_rewrite_module</link> |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4761 may change the request URI at the |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4762 <literal>NGX_HTTP_REWRITE_PHASE</literal> request phase as a result of |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4763 <link doc="../http/ngx_http_rewrite_module.xml" id="rewrite">rewrite</link> and |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4764 return to the <literal>NGX_HTTP_FIND_CONFIG_PHASE</literal> phase for choosing a |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4765 new location based on the new URI. |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4766 </para> |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4767 |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4768 <para> |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4769 It is also possible to redirect a request to a new location at any point by |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4770 calling one of the functions |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4771 <literal>ngx_http_internal_redirect(r, uri, args)</literal> or |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4772 <literal>ngx_http_named_location(r, name)</literal>. |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4773 </para> |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4774 |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4775 <para> |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4776 The function <literal>ngx_http_internal_redirect(r, uri, args)</literal> changes |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4777 the request URI and returns the request to the |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4778 <literal>NGX_HTTP_SERVER_REWRITE_PHASE</literal> phase. |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4779 The request proceeds with a server default location. |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4780 Later at <literal>NGX_HTTP_FIND_CONFIG_PHASE</literal> a new location is chosen |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4781 based on the new request URI. |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4782 </para> |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4783 |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4784 <para> |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4785 The following example performs an internal redirect with the new request |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4786 arguments. |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4787 </para> |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4788 |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4789 <programlisting> |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4790 ngx_int_t |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4791 ngx_http_foo_redirect(ngx_http_request_t *r) |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4792 { |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4793 ngx_str_t uri, args; |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4794 |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4795 ngx_str_set(&uri, "/foo"); |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4796 ngx_str_set(&args, "bar=1"); |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4797 |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4798 return ngx_http_internal_redirect(r, &uri, &args); |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4799 } |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4800 </programlisting> |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4801 |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4802 <para> |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4803 The function <literal>ngx_http_named_location(r, name)</literal> redirects |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4804 a request to a named location. The name of the location is passed as the |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4805 argument. The location is looked up among all named locations of the current |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4806 server, after which the requests switches to the |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4807 <literal>NGX_HTTP_REWRITE_PHASE</literal> phase. |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4808 </para> |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4809 |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4810 <para> |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4811 The following example performs a redirect to a named location @foo. |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4812 </para> |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4813 |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4814 <programlisting> |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4815 ngx_int_t |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4816 ngx_http_foo_named_redirect(ngx_http_request_t *r) |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4817 { |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4818 ngx_str_t name; |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4819 |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4820 ngx_str_set(&name, "foo"); |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4821 |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4822 return ngx_http_named_location(r, &name); |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4823 } |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4824 </programlisting> |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4825 |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4826 <para> |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4827 Both functions <literal>ngx_http_internal_redirect(r, uri, args)</literal> |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4828 and <literal>ngx_http_named_location(r, name)</literal> may be called when |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4829 a request already has some contexts saved in its <literal>ctx</literal> field |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4830 by nginx modules. These contexts could become inconsistent with the new |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4831 location configuration. To prevent inconsistency, all request contexts are |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4832 erased by both redirect functions. |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4833 </para> |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4834 |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4835 <para> |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4836 Redirected and rewritten requests become internal and may access the |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4837 <link doc="../http/ngx_http_core_module.xml" id="internal">internal</link> |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4838 locations. Internal requests have the <literal>internal</literal> flag set. |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4839 </para> |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4840 |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4841 </section> |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4842 |
ef27e3ef0c46
The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1960
diff
changeset
|
4843 |
1968
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4844 <section name="Subrequests" id="http_subrequests"> |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4845 |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4846 <para> |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4847 Subrequests are primarily used to include output of one request into another, |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4848 possibly mixed with other data. |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4849 A subrequest looks like a normal request, but shares some data with its parent. |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4850 Particularly, all fields related to client input are shared since a subrequest |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4851 does not receive any other input from client. |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4852 The request field <literal>parent</literal> for a subrequest keeps a link to its |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4853 parent request and is NULL for the main request. |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4854 The field <literal>main</literal> keeps a link to the main request in a group of |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4855 requests. |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4856 </para> |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4857 |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4858 <para> |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4859 A subrequest starts with <literal>NGX_HTTP_SERVER_REWRITE_PHASE</literal> phase. |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4860 It passes through the same phases as a normal request and is assigned a location |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4861 based on its own URI. |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4862 </para> |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4863 |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4864 <para> |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4865 Subrequest output header is always ignored. |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4866 Subrequest output body is placed by the |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4867 <literal>ngx_http_postpone_filter</literal> into the right position in relation |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4868 to other data produced by the parent request. |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4869 </para> |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4870 |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4871 <para> |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4872 Subrequests are related to the concept of active requests. |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4873 A request <literal>r</literal> is considered active if |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4874 <literal>c->data == r</literal>, where <literal>c</literal> is the client |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4875 connection object. |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4876 At any point, only the active request in a request group is allowed to output |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4877 its buffers to the client. |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4878 A non-active request can still send its data to the filter chain, but they |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4879 will not pass beyond the <literal>ngx_http_postpone_filter</literal> and will |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4880 remain buffered by that filter until the request becomes active. |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4881 Here are some rules of request activation: |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4882 </para> |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4883 |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4884 <list type="bullet"> |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4885 |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4886 <listitem> |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4887 Initially, the main request is active |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4888 </listitem> |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4889 |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4890 <listitem> |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4891 The first subrequest of an active request becomes active right after creation |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4892 </listitem> |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4893 |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4894 <listitem> |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4895 The <literal>ngx_http_postpone_filter</literal> activates the next request |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4896 in active request's subrequest list, once all data prior to that request are |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4897 sent |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4898 </listitem> |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4899 |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4900 <listitem> |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4901 When a request is finalized, its parent is activated |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4902 </listitem> |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4903 |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4904 </list> |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4905 |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4906 <para> |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4907 A subrequest is created by calling the function |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4908 <literal>ngx_http_subrequest(r, uri, args, psr, ps, flags)</literal>, where |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4909 <literal>r</literal> is the parent request, <literal>uri</literal> and |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4910 <literal>args</literal> are URI and arguments of the |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4911 subrequest, <literal>psr</literal> is the output parameter, receiving the |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4912 newly created subrequest reference, <literal>ps</literal> is a callback object |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4913 for notifying the parent request that the subrequest is being finalized, |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4914 <literal>flags</literal> is subrequest creation flags bitmask. |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4915 The following flags are available: |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4916 </para> |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4917 |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4918 <list type="bullet"> |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4919 |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4920 <listitem> |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4921 <literal>NGX_HTTP_SUBREQUEST_IN_MEMORY</literal> - subrequest output should not |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4922 be sent to the client, but rather stored in memory. |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4923 This only works for proxying subrequests. |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4924 After subrequest finalization its output is available in |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4925 <literal>r->upstream->buffer</literal> buffer of type |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4926 <literal>ngx_buf_t</literal> |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4927 </listitem> |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4928 |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4929 <listitem> |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4930 <literal>NGX_HTTP_SUBREQUEST_WAITED</literal> - the subrequest |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4931 <literal>done</literal> flag is set even if it is finalized being non-active. |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4932 This subrequest flag is used by the SSI filter |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4933 </listitem> |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4934 |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4935 <listitem> |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4936 <literal>NGX_HTTP_SUBREQUEST_CLONE</literal> - the subrequest is created as a |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4937 clone of its parent. |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4938 It is started at the same location and proceeds from the same phase as the |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4939 parent request |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4940 </listitem> |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4941 |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4942 </list> |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4943 |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4944 <para> |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4945 The following example creates a subrequest with the URI of "/foo". |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4946 </para> |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4947 |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4948 <programlisting> |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4949 ngx_int_t rc; |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4950 ngx_str_t uri; |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4951 ngx_http_request_t *sr; |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4952 |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4953 ... |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4954 |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4955 ngx_str_set(&uri, "/foo"); |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4956 |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4957 rc = ngx_http_subrequest(r, &uri, NULL, &sr, NULL, 0); |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4958 if (rc == NGX_ERROR) { |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4959 /* error */ |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4960 } |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4961 </programlisting> |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4962 |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4963 <para> |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4964 This example clones the current request and sets a finalization callback for the |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4965 subrequest. |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4966 </para> |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4967 |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4968 <programlisting> |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4969 ngx_int_t |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4970 ngx_http_foo_clone(ngx_http_request_t *r) |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4971 { |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4972 ngx_http_request_t *sr; |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4973 ngx_http_post_subrequest_t *ps; |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4974 |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4975 ps = ngx_palloc(r->pool, sizeof(ngx_http_post_subrequest_t)); |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4976 if (ps == NULL) { |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4977 return NGX_ERROR; |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4978 } |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4979 |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4980 ps->handler = ngx_http_foo_subrequest_done; |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4981 ps->data = "foo"; |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4982 |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4983 return ngx_http_subrequest(r, &r->uri, &r->args, &sr, ps, |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4984 NGX_HTTP_SUBREQUEST_CLONE); |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4985 } |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4986 |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4987 |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4988 ngx_int_t |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4989 ngx_http_foo_subrequest_done(ngx_http_request_t *r, void *data, ngx_int_t rc) |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4990 { |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4991 char *msg = (char *) data; |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4992 |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4993 ngx_log_error(NGX_LOG_INFO, r->connection->log, 0, |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4994 "done subrequest r:%p msg:%s rc:%i", r, msg, rc); |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4995 |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4996 return rc; |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4997 } |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4998 </programlisting> |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
4999 |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5000 <para> |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5001 Subrequests are normally created in a body filter. |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5002 In this case subrequest output can be treated as any other explicit request |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5003 output. |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5004 This means that eventually the output of a subrequest is sent to the client |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5005 after all explicit buffers passed prior to subrequest creation and before any |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5006 buffers passed later. |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5007 This ordering is preserved even for large hierarchies of subrequests. |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5008 The following example inserts a subrequest output after all request data |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5009 buffers, but before the final buffer with the <literal>last_buf</literal> flag. |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5010 </para> |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5011 |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5012 <programlisting> |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5013 ngx_int_t |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5014 ngx_http_foo_body_filter(ngx_http_request_t *r, ngx_chain_t *in) |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5015 { |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5016 ngx_int_t rc; |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5017 ngx_buf_t *b; |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5018 ngx_uint_t last; |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5019 ngx_chain_t *cl, out; |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5020 ngx_http_request_t *sr; |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5021 ngx_http_foo_filter_ctx_t *ctx; |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5022 |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5023 ctx = ngx_http_get_module_ctx(r, ngx_http_foo_filter_module); |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5024 if (ctx == NULL) { |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5025 return ngx_http_next_body_filter(r, in); |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5026 } |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5027 |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5028 last = 0; |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5029 |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5030 for (cl = in; cl; cl = cl->next) { |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5031 if (cl->buf->last_buf) { |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5032 cl->buf->last_buf = 0; |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5033 cl->buf->last_in_chain = 1; |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5034 cl->buf->sync = 1; |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5035 last = 1; |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5036 } |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5037 } |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5038 |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5039 /* Output explicit output buffers */ |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5040 |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5041 rc = ngx_http_next_body_filter(r, in); |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5042 |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5043 if (rc == NGX_ERROR || !last) { |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5044 return rc; |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5045 } |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5046 |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5047 /* |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5048 * Create the subrequest. The output of the subrequest |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5049 * will automatically be sent after all preceding buffers, |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5050 * but before the last_buf buffer passed later in this function. |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5051 */ |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5052 |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5053 if (ngx_http_subrequest(r, ctx->uri, NULL, &sr, NULL, 0) != NGX_OK) { |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5054 return NGX_ERROR; |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5055 } |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5056 |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5057 ngx_http_set_ctx(r, NULL, ngx_http_foo_filter_module); |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5058 |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5059 |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5060 /* Output the final buffer with the last_buf flag */ |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5061 |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5062 b = ngx_calloc_buf(r->pool); |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5063 if (b == NULL) { |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5064 return NGX_ERROR; |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5065 } |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5066 |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5067 b->last_buf = 1; |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5068 |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5069 out.buf = b; |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5070 out.next = NULL; |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5071 |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5072 return ngx_http_output_filter(r, &out); |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5073 } |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5074 </programlisting> |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5075 |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5076 <para> |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5077 A subrequest may also be created for other purposes than data output. |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5078 For example, the <link doc="../http/ngx_http_auth_request_module.xml"> |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5079 ngx_http_auth_request_module</link> |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5080 creates a subrequest at <literal>NGX_HTTP_ACCESS_PHASE</literal> phase. |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5081 To disable any output at this point, the subrequest |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5082 <literal>header_only</literal> flag is set. |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5083 This prevents subrequest body from being sent to the client. |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5084 Its header is ignored anyway. |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5085 The result of the subrequest can be analyzed in the callback handler. |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5086 </para> |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5087 |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5088 </section> |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5089 |
69908bd68481
The HTTP subrequests section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1967
diff
changeset
|
5090 |
1969
275c928ab386
The HTTP request finalization section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1968
diff
changeset
|
5091 <section name="Request finalization" id="http_request_finalization"> |
275c928ab386
The HTTP request finalization section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1968
diff
changeset
|
5092 |
275c928ab386
The HTTP request finalization section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1968
diff
changeset
|
5093 <para> |
275c928ab386
The HTTP request finalization section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1968
diff
changeset
|
5094 An HTTP request is finalized by calling the function |
275c928ab386
The HTTP request finalization section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1968
diff
changeset
|
5095 <literal>ngx_http_finalize_request(r, rc)</literal>. |
275c928ab386
The HTTP request finalization section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1968
diff
changeset
|
5096 It is usually finalized by the content handler after sending all output buffers |
275c928ab386
The HTTP request finalization section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1968
diff
changeset
|
5097 to the filter chain. |
275c928ab386
The HTTP request finalization section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1968
diff
changeset
|
5098 At this point the output may not be completely sent to the client, but remain |
275c928ab386
The HTTP request finalization section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1968
diff
changeset
|
5099 buffered somewhere along the filter chain. |
275c928ab386
The HTTP request finalization section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1968
diff
changeset
|
5100 If it is, the <literal>ngx_http_finalize_request(r, rc)</literal> function will |
275c928ab386
The HTTP request finalization section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1968
diff
changeset
|
5101 automatically install a special handler <literal>ngx_http_writer(r)</literal> |
275c928ab386
The HTTP request finalization section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1968
diff
changeset
|
5102 to finish sending the output. |
275c928ab386
The HTTP request finalization section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1968
diff
changeset
|
5103 A request is also finalized in case of an error or if a standard HTTP response |
275c928ab386
The HTTP request finalization section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1968
diff
changeset
|
5104 code needs to be returned to the client. |
275c928ab386
The HTTP request finalization section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1968
diff
changeset
|
5105 </para> |
275c928ab386
The HTTP request finalization section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1968
diff
changeset
|
5106 |
275c928ab386
The HTTP request finalization section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1968
diff
changeset
|
5107 <para> |
275c928ab386
The HTTP request finalization section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1968
diff
changeset
|
5108 The function <literal>ngx_http_finalize_request(r, rc)</literal> expects the |
275c928ab386
The HTTP request finalization section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1968
diff
changeset
|
5109 following <literal>rc</literal> values: |
275c928ab386
The HTTP request finalization section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1968
diff
changeset
|
5110 </para> |
275c928ab386
The HTTP request finalization section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1968
diff
changeset
|
5111 |
275c928ab386
The HTTP request finalization section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1968
diff
changeset
|
5112 <list type="bullet"> |
275c928ab386
The HTTP request finalization section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1968
diff
changeset
|
5113 |
275c928ab386
The HTTP request finalization section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1968
diff
changeset
|
5114 <listitem> |
275c928ab386
The HTTP request finalization section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1968
diff
changeset
|
5115 <literal>NGX_DONE</literal> - fast finalization. |
275c928ab386
The HTTP request finalization section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1968
diff
changeset
|
5116 Decrement request <literal>count</literal> and destroy the request if it |
275c928ab386
The HTTP request finalization section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1968
diff
changeset
|
5117 reaches zero. |
275c928ab386
The HTTP request finalization section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1968
diff
changeset
|
5118 The client connection may still be used for more requests after that |
275c928ab386
The HTTP request finalization section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1968
diff
changeset
|
5119 </listitem> |
275c928ab386
The HTTP request finalization section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1968
diff
changeset
|
5120 |
275c928ab386
The HTTP request finalization section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1968
diff
changeset
|
5121 <listitem> |
275c928ab386
The HTTP request finalization section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1968
diff
changeset
|
5122 <literal>NGX_ERROR</literal>, <literal>NGX_HTTP_REQUEST_TIME_OUT</literal> |
275c928ab386
The HTTP request finalization section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1968
diff
changeset
|
5123 (408), <literal>NGX_HTTP_CLIENT_CLOSED_REQUEST</literal> (499) - error |
275c928ab386
The HTTP request finalization section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1968
diff
changeset
|
5124 finalization. |
275c928ab386
The HTTP request finalization section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1968
diff
changeset
|
5125 Terminate the request as soon as possible and close the client connection. |
275c928ab386
The HTTP request finalization section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1968
diff
changeset
|
5126 </listitem> |
275c928ab386
The HTTP request finalization section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1968
diff
changeset
|
5127 |
275c928ab386
The HTTP request finalization section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1968
diff
changeset
|
5128 <listitem> |
275c928ab386
The HTTP request finalization section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1968
diff
changeset
|
5129 <literal>NGX_HTTP_CREATED</literal> (201), |
275c928ab386
The HTTP request finalization section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1968
diff
changeset
|
5130 <literal>NGX_HTTP_NO_CONTENT</literal> (204), codes greater than or equal to |
275c928ab386
The HTTP request finalization section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1968
diff
changeset
|
5131 <literal>NGX_HTTP_SPECIAL_RESPONSE</literal> (300) - special response |
275c928ab386
The HTTP request finalization section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1968
diff
changeset
|
5132 finalization. |
275c928ab386
The HTTP request finalization section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1968
diff
changeset
|
5133 For these values nginx either sends a default code response page to the client |
275c928ab386
The HTTP request finalization section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1968
diff
changeset
|
5134 or performs the internal redirect to an |
275c928ab386
The HTTP request finalization section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1968
diff
changeset
|
5135 <link doc="../http/ngx_http_core_module.xml" id="error_page"/> location if it's |
275c928ab386
The HTTP request finalization section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1968
diff
changeset
|
5136 configured for the code |
275c928ab386
The HTTP request finalization section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1968
diff
changeset
|
5137 </listitem> |
275c928ab386
The HTTP request finalization section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1968
diff
changeset
|
5138 |
275c928ab386
The HTTP request finalization section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1968
diff
changeset
|
5139 <listitem> |
275c928ab386
The HTTP request finalization section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1968
diff
changeset
|
5140 Other codes are considered success finalization codes and may activate the |
275c928ab386
The HTTP request finalization section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1968
diff
changeset
|
5141 request writer to finish sending the response body. |
275c928ab386
The HTTP request finalization section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1968
diff
changeset
|
5142 Once body is completely sent, request <literal>count</literal> is decremented. |
275c928ab386
The HTTP request finalization section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1968
diff
changeset
|
5143 If it reaches zero, the request is destroyed, but the client connection may |
275c928ab386
The HTTP request finalization section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1968
diff
changeset
|
5144 still be used for other requests. |
275c928ab386
The HTTP request finalization section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1968
diff
changeset
|
5145 If <literal>count</literal> is positive, there are unfinished activities |
275c928ab386
The HTTP request finalization section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1968
diff
changeset
|
5146 within the request, which will be finalized at a later point. |
275c928ab386
The HTTP request finalization section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1968
diff
changeset
|
5147 </listitem> |
275c928ab386
The HTTP request finalization section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1968
diff
changeset
|
5148 |
275c928ab386
The HTTP request finalization section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1968
diff
changeset
|
5149 </list> |
275c928ab386
The HTTP request finalization section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1968
diff
changeset
|
5150 |
275c928ab386
The HTTP request finalization section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1968
diff
changeset
|
5151 </section> |
275c928ab386
The HTTP request finalization section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1968
diff
changeset
|
5152 |
275c928ab386
The HTTP request finalization section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1968
diff
changeset
|
5153 |
1970
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5154 <section name="Request body" id="http_request_body"> |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5155 |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5156 <para> |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5157 For dealing with client request body, nginx provides the following functions: |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5158 <literal>ngx_http_read_client_request_body(r, post_handler)</literal> and |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5159 <literal>ngx_http_discard_request_body(r)</literal>. |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5160 The first function reads the request body and makes it available via the |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5161 <literal>request_body</literal> request field. |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5162 The second function instructs nginx to discard (read and ignore) the request |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5163 body. |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5164 One of these functions must be called for every request. |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5165 Normally, it is done in the content handler. |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5166 </para> |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5167 |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5168 <para> |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5169 Reading or discarding client request body from a subrequest is not allowed. |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5170 It should always be done in the main request. |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5171 When a subrequest is created, it inherits the parent |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5172 <literal>request_body</literal> object which can be used by the subrequest if |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5173 the main request has previously read the request body. |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5174 </para> |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5175 |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5176 <para> |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5177 The function |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5178 <literal>ngx_http_read_client_request_body(r, post_handler)</literal> starts |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5179 the process of reading the request body. |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5180 When the body is completely read, the <literal>post_handler</literal> callback |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5181 is called to continue processing the request. |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5182 If request body is missing or already read, the callback is called immediately. |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5183 The function |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5184 <literal>ngx_http_read_client_request_body(r, post_handler)</literal> |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5185 allocates the <literal>request_body</literal> request field of type |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5186 <literal>ngx_http_request_body_t</literal>. |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5187 The field <literal>bufs</literal> of this object keeps the result as a buffer |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5188 chain. |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5189 The body can be saved in memory buffers or file buffers, if |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5190 <link doc="../http/ngx_http_core_module.xml" id="client_body_buffer_size"/> |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5191 is not enough to fit the entire body in memory. |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5192 </para> |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5193 |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5194 <para> |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5195 The following example reads client request body and returns its size. |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5196 </para> |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5197 |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5198 <programlisting> |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5199 ngx_int_t |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5200 ngx_http_foo_content_handler(ngx_http_request_t *r) |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5201 { |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5202 ngx_int_t rc; |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5203 |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5204 rc = ngx_http_read_client_request_body(r, ngx_http_foo_init); |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5205 |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5206 if (rc >= NGX_HTTP_SPECIAL_RESPONSE) { |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5207 /* error */ |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5208 return rc; |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5209 } |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5210 |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5211 return NGX_DONE; |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5212 } |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5213 |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5214 |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5215 void |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5216 ngx_http_foo_init(ngx_http_request_t *r) |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5217 { |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5218 off_t len; |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5219 ngx_buf_t *b; |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5220 ngx_int_t rc; |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5221 ngx_chain_t *in, out; |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5222 |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5223 if (r->request_body == NULL) { |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5224 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5225 return; |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5226 } |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5227 |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5228 len = 0; |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5229 |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5230 for (in = r->request_body->bufs; in; in = in->next) { |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5231 len += ngx_buf_size(in->buf); |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5232 } |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5233 |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5234 b = ngx_create_temp_buf(r->pool, NGX_OFF_T_LEN); |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5235 if (b == NULL) { |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5236 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5237 return; |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5238 } |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5239 |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5240 b->last = ngx_sprintf(b->pos, "%O", len); |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5241 b->last_buf = (r == r->main) ? 1: 0; |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5242 b->last_in_chain = 1; |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5243 |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5244 r->headers_out.status = NGX_HTTP_OK; |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5245 r->headers_out.content_length_n = b->last - b->pos; |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5246 |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5247 rc = ngx_http_send_header(r); |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5248 |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5249 if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) { |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5250 ngx_http_finalize_request(r, rc); |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5251 return; |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5252 } |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5253 |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5254 out.buf = b; |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5255 out.next = NULL; |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5256 |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5257 rc = ngx_http_output_filter(r, &out); |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5258 |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5259 ngx_http_finalize_request(r, rc); |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5260 } |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5261 </programlisting> |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5262 |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5263 <para> |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5264 The following fields of the request affect the way request body is read: |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5265 </para> |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5266 |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5267 <list type="bullet"> |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5268 |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5269 <listitem> |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5270 <literal>request_body_in_single_buf</literal> - read body to a single memory |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5271 buffer |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5272 </listitem> |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5273 |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5274 <listitem> |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5275 <literal>request_body_in_file_only</literal> - always read body to a file, |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5276 even if fits the memory buffer |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5277 </listitem> |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5278 |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5279 <listitem> |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5280 <literal>request_body_in_persistent_file</literal> - do not unlink the file |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5281 right after creation. |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5282 Such a file can be moved to another directory |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5283 </listitem> |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5284 |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5285 <listitem> |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5286 <literal>request_body_in_clean_file</literal> - unlink the file the when the |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5287 request is finalized. |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5288 This can be useful when a file was supposed to be moved to another directory |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5289 but eventually was not moved for some reason |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5290 </listitem> |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5291 |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5292 <listitem> |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5293 <literal>request_body_file_group_access</literal> - enable file group access. |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5294 By default a file is created with 0600 access mask. |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5295 When the flag is set, 0660 access mask is used |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5296 </listitem> |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5297 |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5298 <listitem> |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5299 <literal>request_body_file_log_level</literal> - log file errors with this |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5300 log level |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5301 </listitem> |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5302 |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5303 <listitem> |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5304 <literal>request_body_no_buffering</literal> - read request body without |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5305 buffering |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5306 </listitem> |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5307 |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5308 </list> |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5309 |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5310 <para> |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5311 When the <literal>request_body_no_buffering</literal> flag is set, the |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5312 unbuffered mode of reading the request body is enabled. |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5313 In this mode, after calling |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5314 <literal>ngx_http_read_client_request_body()</literal>, the |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5315 <literal>bufs</literal> chain may keep only a part of the body. |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5316 To read the next part, the |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5317 <literal>ngx_http_read_unbuffered_request_body(r)</literal> function should be |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5318 called. |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5319 The return value of <literal>NGX_AGAIN</literal> and the request flag |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5320 <literal>reading_body</literal> indicate that more data is available. |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5321 If <literal>bufs</literal> is NULL after calling this function, there is |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5322 nothing to read at the moment. |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5323 The request callback <literal>read_event_handler</literal> will be called when |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5324 the next part of request body is available. |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5325 </para> |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5326 |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5327 </section> |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5328 |
a1d29eda04b6
The HTTP request body section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1969
diff
changeset
|
5329 |
1960
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5330 <section name="Response" id="http_response"> |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5331 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5332 <para> |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5333 An HTTP response in nginx is produced by sending the response header followed by |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5334 the optional response body. |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5335 Both header and body are passed through a chain of filters and eventually get |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5336 written to the client socket. |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5337 An nginx module can install its handler into the header or body filter chain |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5338 and process the output coming from the previous handler. |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5339 </para> |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5340 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5341 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5342 <section name="Response header" id="http_response_header"> |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5343 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5344 <para> |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5345 Output header is sent by the function |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5346 <literal>ngx_http_send_header(r)</literal>. |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5347 Prior to calling this function, <literal>r->headers_out</literal> should contain |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5348 all the data required to produce the HTTP response header. |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5349 It's always required to set the <literal>status</literal> field of |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5350 <literal>r->headers_out</literal>. |
1971
5fb870087b76
Fixed typo and removed trailing spaces.
Vladimir Homutov <vl@nginx.com>
parents:
1970
diff
changeset
|
5351 If the response status suggests that a response body follows the header, |
1960
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5352 <literal>content_length_n</literal> can be set as well. |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5353 The default value for this field is -1, which means that the body size is |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5354 unknown. |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5355 In this case, chunked transfer encoding is used. |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5356 To output an arbitrary header, <literal>headers</literal> list should be |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5357 appended. |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5358 </para> |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5359 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5360 <programlisting> |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5361 static ngx_int_t |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5362 ngx_http_foo_content_handler(ngx_http_request_t *r) |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5363 { |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5364 ngx_int_t rc; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5365 ngx_table_elt_t *h; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5366 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5367 /* send header */ |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5368 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5369 r->headers_out.status = NGX_HTTP_OK; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5370 r->headers_out.content_length_n = 3; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5371 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5372 /* X-Foo: foo */ |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5373 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5374 h = ngx_list_push(&r->headers_out.headers); |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5375 if (h == NULL) { |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5376 return NGX_ERROR; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5377 } |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5378 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5379 h->hash = 1; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5380 ngx_str_set(&h->key, "X-Foo"); |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5381 ngx_str_set(&h->value, "foo"); |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5382 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5383 rc = ngx_http_send_header(r); |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5384 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5385 if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) { |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5386 return rc; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5387 } |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5388 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5389 /* send body */ |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5390 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5391 ... |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5392 } |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5393 </programlisting> |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5394 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5395 </section> |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5396 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5397 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5398 <section name="Header filters" id="http_header_filters"> |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5399 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5400 <para> |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5401 The <literal>ngx_http_send_header(r)</literal> function invokes the header |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5402 filter chain by calling the top header filter handler |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5403 <literal>ngx_http_top_header_filter</literal>. |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5404 It's assumed that every header handler calls the next handler in chain until |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5405 the final handler <literal>ngx_http_header_filter(r)</literal> is called. |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5406 The final header handler constructs the HTTP response based on |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5407 <literal>r->headers_out</literal> and passes it to the |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5408 <literal>ngx_http_writer_filter</literal> for output. |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5409 </para> |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5410 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5411 <para> |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5412 To add a handler to the header filter chain, one should store its address in |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5413 <literal>ngx_http_top_header_filter</literal> global variable at configuration |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5414 time. |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5415 The previous handler address is normally stored in a module's static variable |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5416 and is called by the newly added handler before exiting. |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5417 </para> |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5418 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5419 <para> |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5420 The following is an example header filter module, adding the HTTP header |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5421 "X-Foo: foo" to every output with the status 200. |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5422 </para> |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5423 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5424 <programlisting> |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5425 #include <ngx_config.h> |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5426 #include <ngx_core.h> |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5427 #include <ngx_http.h> |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5428 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5429 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5430 static ngx_int_t ngx_http_foo_header_filter(ngx_http_request_t *r); |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5431 static ngx_int_t ngx_http_foo_header_filter_init(ngx_conf_t *cf); |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5432 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5433 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5434 static ngx_http_module_t ngx_http_foo_header_filter_module_ctx = { |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5435 NULL, /* preconfiguration */ |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5436 ngx_http_foo_header_filter_init, /* postconfiguration */ |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5437 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5438 NULL, /* create main configuration */ |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5439 NULL, /* init main configuration */ |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5440 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5441 NULL, /* create server configuration */ |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5442 NULL, /* merge server configuration */ |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5443 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5444 NULL, /* create location configuration */ |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5445 NULL /* merge location configuration */ |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5446 }; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5447 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5448 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5449 ngx_module_t ngx_http_foo_header_filter_module = { |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5450 NGX_MODULE_V1, |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5451 &ngx_http_foo_header_filter_module_ctx, /* module context */ |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5452 NULL, /* module directives */ |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5453 NGX_HTTP_MODULE, /* module type */ |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5454 NULL, /* init master */ |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5455 NULL, /* init module */ |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5456 NULL, /* init process */ |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5457 NULL, /* init thread */ |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5458 NULL, /* exit thread */ |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5459 NULL, /* exit process */ |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5460 NULL, /* exit master */ |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5461 NGX_MODULE_V1_PADDING |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5462 }; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5463 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5464 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5465 static ngx_http_output_header_filter_pt ngx_http_next_header_filter; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5466 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5467 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5468 static ngx_int_t |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5469 ngx_http_foo_header_filter(ngx_http_request_t *r) |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5470 { |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5471 ngx_table_elt_t *h; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5472 |
1971
5fb870087b76
Fixed typo and removed trailing spaces.
Vladimir Homutov <vl@nginx.com>
parents:
1970
diff
changeset
|
5473 /* |
1960
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5474 * The filter handler adds "X-Foo: foo" header |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5475 * to every HTTP 200 response |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5476 */ |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5477 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5478 if (r->headers_out.status != NGX_HTTP_OK) { |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5479 return ngx_http_next_header_filter(r); |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5480 } |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5481 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5482 h = ngx_list_push(&r->headers_out.headers); |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5483 if (h == NULL) { |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5484 return NGX_ERROR; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5485 } |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5486 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5487 h->hash = 1; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5488 ngx_str_set(&h->key, "X-Foo"); |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5489 ngx_str_set(&h->value, "foo"); |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5490 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5491 return ngx_http_next_header_filter(r); |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5492 } |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5493 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5494 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5495 static ngx_int_t |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5496 ngx_http_foo_header_filter_init(ngx_conf_t *cf) |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5497 { |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5498 ngx_http_next_header_filter = ngx_http_top_header_filter; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5499 ngx_http_top_header_filter = ngx_http_foo_header_filter; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5500 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5501 return NGX_OK; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5502 } |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5503 </programlisting> |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5504 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5505 </section> |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5506 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5507 </section> |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5508 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5509 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5510 <section name="Response body" id="http_response_body"> |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5511 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5512 <para> |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5513 Response body is sent by calling the function |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5514 <literal>ngx_http_output_filter(r, cl)</literal>. |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5515 The function can be called multiple times. |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5516 Each time it sends a part of the response body passed as a buffer chain. |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5517 The last body buffer should have the <literal>last_buf</literal> flag set. |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5518 </para> |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5519 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5520 <para> |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5521 The following example produces a complete HTTP output with "foo" as its body. |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5522 In order for the example to work not only as a main request but as a subrequest |
1980 | 5523 as well, the <literal>last_in_chain</literal> flag is set in the last buffer |
1960
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5524 of the output. |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5525 The <literal>last_buf</literal> flag is set only for the main request since |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5526 a subrequest's last buffers does not end the entire output. |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5527 </para> |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5528 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5529 <programlisting> |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5530 static ngx_int_t |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5531 ngx_http_bar_content_handler(ngx_http_request_t *r) |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5532 { |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5533 ngx_int_t rc; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5534 ngx_buf_t *b; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5535 ngx_chain_t out; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5536 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5537 /* send header */ |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5538 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5539 r->headers_out.status = NGX_HTTP_OK; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5540 r->headers_out.content_length_n = 3; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5541 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5542 rc = ngx_http_send_header(r); |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5543 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5544 if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) { |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5545 return rc; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5546 } |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5547 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5548 /* send body */ |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5549 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5550 b = ngx_calloc_buf(r->pool); |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5551 if (b == NULL) { |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5552 return NGX_ERROR; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5553 } |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5554 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5555 b->last_buf = (r == r->main) ? 1: 0; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5556 b->last_in_chain = 1; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5557 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5558 b->memory = 1; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5559 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5560 b->pos = (u_char *) "foo"; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5561 b->last = b->pos + 3; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5562 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5563 out.buf = b; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5564 out.next = NULL; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5565 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5566 return ngx_http_output_filter(r, &out); |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5567 } |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5568 </programlisting> |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5569 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5570 </section> |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5571 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5572 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5573 <section name="Body filters" id="http_body_filters"> |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5574 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5575 <para> |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5576 The function <literal>ngx_http_output_filter(r, cl)</literal> invokes the |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5577 body filter chain by calling the top body filter handler |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5578 <literal>ngx_http_top_body_filter</literal>. |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5579 It's assumed that every body handler calls the next handler in chain until |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5580 the final handler <literal>ngx_http_write_filter(r, cl)</literal> is called. |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5581 </para> |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5582 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5583 <para> |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5584 A body filter handler receives a chain of buffers. |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5585 The handler is supposed to process the buffers and pass a possibly new chain to |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5586 the next handler. |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5587 It's worth noting that the chain links <literal>ngx_chain_t</literal> of the |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5588 incoming chain belong to the caller. |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5589 They should never be reused or changed. |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5590 Right after the handler completes, the caller can use its output chain links |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5591 to keep track of the buffers it has sent. |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5592 To save the buffer chain or to substitute some buffers before sending further, |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5593 a handler should allocate its own chain links. |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5594 </para> |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5595 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5596 <para> |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5597 Following is the example of a simple body filter counting the number of |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5598 body bytes. |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5599 The result is available as the <literal>$counter</literal> variable which can be |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5600 used in the access log. |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5601 </para> |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5602 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5603 <programlisting> |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5604 #include <ngx_config.h> |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5605 #include <ngx_core.h> |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5606 #include <ngx_http.h> |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5607 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5608 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5609 typedef struct { |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5610 off_t count; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5611 } ngx_http_counter_filter_ctx_t; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5612 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5613 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5614 static ngx_int_t ngx_http_counter_body_filter(ngx_http_request_t *r, |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5615 ngx_chain_t *in); |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5616 static ngx_int_t ngx_http_counter_variable(ngx_http_request_t *r, |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5617 ngx_http_variable_value_t *v, uintptr_t data); |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5618 static ngx_int_t ngx_http_counter_add_variables(ngx_conf_t *cf); |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5619 static ngx_int_t ngx_http_counter_filter_init(ngx_conf_t *cf); |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5620 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5621 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5622 static ngx_http_module_t ngx_http_counter_filter_module_ctx = { |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5623 ngx_http_counter_add_variables, /* preconfiguration */ |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5624 ngx_http_counter_filter_init, /* postconfiguration */ |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5625 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5626 NULL, /* create main configuration */ |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5627 NULL, /* init main configuration */ |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5628 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5629 NULL, /* create server configuration */ |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5630 NULL, /* merge server configuration */ |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5631 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5632 NULL, /* create location configuration */ |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5633 NULL /* merge location configuration */ |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5634 }; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5635 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5636 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5637 ngx_module_t ngx_http_counter_filter_module = { |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5638 NGX_MODULE_V1, |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5639 &ngx_http_counter_filter_module_ctx, /* module context */ |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5640 NULL, /* module directives */ |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5641 NGX_HTTP_MODULE, /* module type */ |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5642 NULL, /* init master */ |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5643 NULL, /* init module */ |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5644 NULL, /* init process */ |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5645 NULL, /* init thread */ |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5646 NULL, /* exit thread */ |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5647 NULL, /* exit process */ |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5648 NULL, /* exit master */ |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5649 NGX_MODULE_V1_PADDING |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5650 }; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5651 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5652 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5653 static ngx_http_output_body_filter_pt ngx_http_next_body_filter; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5654 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5655 static ngx_str_t ngx_http_counter_name = ngx_string("counter"); |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5656 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5657 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5658 static ngx_int_t |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5659 ngx_http_counter_body_filter(ngx_http_request_t *r, ngx_chain_t *in) |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5660 { |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5661 ngx_chain_t *cl; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5662 ngx_http_counter_filter_ctx_t *ctx; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5663 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5664 ctx = ngx_http_get_module_ctx(r, ngx_http_counter_filter_module); |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5665 if (ctx == NULL) { |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5666 ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_counter_filter_ctx_t)); |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5667 if (ctx == NULL) { |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5668 return NGX_ERROR; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5669 } |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5670 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5671 ngx_http_set_ctx(r, ctx, ngx_http_counter_filter_module); |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5672 } |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5673 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5674 for (cl = in; cl; cl = cl->next) { |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5675 ctx->count += ngx_buf_size(cl->buf); |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5676 } |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5677 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5678 return ngx_http_next_body_filter(r, in); |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5679 } |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5680 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5681 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5682 static ngx_int_t |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5683 ngx_http_counter_variable(ngx_http_request_t *r, ngx_http_variable_value_t *v, |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5684 uintptr_t data) |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5685 { |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5686 u_char *p; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5687 ngx_http_counter_filter_ctx_t *ctx; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5688 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5689 ctx = ngx_http_get_module_ctx(r, ngx_http_counter_filter_module); |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5690 if (ctx == NULL) { |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5691 v->not_found = 1; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5692 return NGX_OK; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5693 } |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5694 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5695 p = ngx_pnalloc(r->pool, NGX_OFF_T_LEN); |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5696 if (p == NULL) { |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5697 return NGX_ERROR; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5698 } |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5699 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5700 v->data = p; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5701 v->len = ngx_sprintf(p, "%O", ctx->count) - p; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5702 v->valid = 1; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5703 v->no_cacheable = 0; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5704 v->not_found = 0; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5705 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5706 return NGX_OK; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5707 } |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5708 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5709 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5710 static ngx_int_t |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5711 ngx_http_counter_add_variables(ngx_conf_t *cf) |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5712 { |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5713 ngx_http_variable_t *var; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5714 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5715 var = ngx_http_add_variable(cf, &ngx_http_counter_name, 0); |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5716 if (var == NULL) { |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5717 return NGX_ERROR; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5718 } |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5719 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5720 var->get_handler = ngx_http_counter_variable; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5721 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5722 return NGX_OK; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5723 } |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5724 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5725 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5726 static ngx_int_t |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5727 ngx_http_counter_filter_init(ngx_conf_t *cf) |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5728 { |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5729 ngx_http_next_body_filter = ngx_http_top_body_filter; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5730 ngx_http_top_body_filter = ngx_http_counter_body_filter; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5731 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5732 return NGX_OK; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5733 } |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5734 </programlisting> |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5735 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5736 </section> |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5737 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5738 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5739 <section name="Building filter modules" id="http_building_filter_modules"> |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5740 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5741 <para> |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5742 When writing a body or header filter, a special care should be taken of the |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5743 filters order. |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5744 There's a number of header and body filters registered by nginx standard |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5745 modules. |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5746 It's important to register a filter module in the right place in respect to |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5747 other filters. |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5748 Normally, filters are registered by modules in their postconfiguration handlers. |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5749 The order in which filters are called is obviously the reverse of when they are |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5750 registered. |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5751 </para> |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5752 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5753 <para> |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5754 A special slot <literal>HTTP_AUX_FILTER_MODULES</literal> for third-party filter |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5755 modules is provided by nginx. |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5756 To register a filter module in this slot, the <literal>ngx_module_type</literal> |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5757 variable should be set to the value of <literal>HTTP_AUX_FILTER</literal> in |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5758 module's configuration. |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5759 </para> |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5760 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5761 <para> |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5762 The following example shows a filter module config file assuming it only has |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5763 one source file <literal>ngx_http_foo_filter_module.c</literal> |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5764 </para> |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5765 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5766 <programlisting> |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5767 ngx_module_type=HTTP_AUX_FILTER |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5768 ngx_module_name=ngx_http_foo_filter_module |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5769 ngx_module_srcs="$ngx_addon_dir/ngx_http_foo_filter_module.c" |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5770 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5771 . auto/module |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5772 </programlisting> |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5773 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5774 </section> |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5775 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5776 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5777 <section name="Buffer reuse" id="http_body_buffers_reuse"> |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5778 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5779 <para> |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5780 When issuing or altering a stream of buffers, it's often desirable to reuse the |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5781 allocated buffers. |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5782 A standard approach widely adopted in nginx code is to keep two buffer chains |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5783 for this purpose: <literal>free</literal> and <literal>busy</literal>. |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5784 The <literal>free</literal> chain keeps all free buffers. |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5785 These buffers can be reused. |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5786 The <literal>busy</literal> chain keeps all buffers sent by the current |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5787 module which are still in use by some other filter handler. |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5788 A buffer is considered in use if its size is greater than zero. |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5789 Normally, when a buffer is consumed by a filter, its <literal>pos</literal> |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5790 (or <literal>file_pos</literal> for a file buffer) is moved towards |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5791 <literal>last</literal> (<literal>file_last</literal> for a file buffer). |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5792 Once a buffer is completely consumed, it's ready to be reused. |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5793 To update the <literal>free</literal> chain with newly freed buffers, |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5794 it's enough to iterate over the <literal>busy</literal> chain and move the zero |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5795 size buffers at the head of it to <literal>free</literal>. |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5796 This operation is so common that there is a special function |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5797 <literal>ngx_chain_update_chains(free, busy, out, tag)</literal> which does |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5798 this. |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5799 The function appends the output chain <literal>out</literal> to |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5800 <literal>busy</literal> and moves free buffers from the top of |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5801 <literal>busy</literal> to <literal>free</literal>. |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5802 Only the buffers with the given <literal>tag</literal> are reused. |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5803 This lets a module reuse only the buffers allocated by itself. |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5804 </para> |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5805 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5806 <para> |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5807 The following example is a body filter inserting the “foo” string before each |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5808 incoming buffer. |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5809 The new buffers allocated by the module are reused if possible. |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5810 Note that for this example to work properly, it's also required to set up a |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5811 header filter and reset <literal>content_length_n</literal> to -1, which is |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5812 beyond the scope of this section. |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5813 </para> |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5814 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5815 <programlisting> |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5816 typedef struct { |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5817 ngx_chain_t *free; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5818 ngx_chain_t *busy; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5819 } ngx_http_foo_filter_ctx_t; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5820 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5821 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5822 ngx_int_t |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5823 ngx_http_foo_body_filter(ngx_http_request_t *r, ngx_chain_t *in) |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5824 { |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5825 ngx_int_t rc; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5826 ngx_buf_t *b; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5827 ngx_chain_t *cl, *tl, *out, **ll; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5828 ngx_http_foo_filter_ctx_t *ctx; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5829 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5830 ctx = ngx_http_get_module_ctx(r, ngx_http_foo_filter_module); |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5831 if (ctx == NULL) { |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5832 ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_foo_filter_ctx_t)); |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5833 if (ctx == NULL) { |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5834 return NGX_ERROR; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5835 } |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5836 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5837 ngx_http_set_ctx(r, ctx, ngx_http_foo_filter_module); |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5838 } |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5839 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5840 /* create a new chain "out" from "in" with all the changes */ |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5841 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5842 ll = &out; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5843 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5844 for (cl = in; cl; cl = cl->next) { |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5845 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5846 /* append "foo" in a reused buffer if possible */ |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5847 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5848 tl = ngx_chain_get_free_buf(r->pool, &ctx->free); |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5849 if (tl == NULL) { |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5850 return NGX_ERROR; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5851 } |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5852 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5853 b = tl->buf; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5854 b->tag = (ngx_buf_tag_t) &ngx_http_foo_filter_module; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5855 b->memory = 1; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5856 b->pos = (u_char *) "foo"; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5857 b->last = b->pos + 3; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5858 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5859 *ll = tl; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5860 ll = &tl->next; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5861 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5862 /* append the next incoming buffer */ |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5863 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5864 tl = ngx_alloc_chain_link(r->pool); |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5865 if (tl == NULL) { |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5866 return NGX_ERROR; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5867 } |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5868 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5869 tl->buf = cl->buf; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5870 *ll = tl; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5871 ll = &tl->next; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5872 } |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5873 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5874 *ll = NULL; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5875 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5876 /* send the new chain */ |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5877 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5878 rc = ngx_http_next_body_filter(r, out); |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5879 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5880 /* update "busy" and "free" chains for reuse */ |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5881 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5882 ngx_chain_update_chains(r->pool, &ctx->free, &ctx->busy, &out, |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5883 (ngx_buf_tag_t) &ngx_http_foo_filter_module); |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5884 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5885 return rc; |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5886 } |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5887 </programlisting> |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5888 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5889 </section> |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5890 |
9550ea66abdd
HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1959
diff
changeset
|
5891 |
1939
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5892 <section name="Load balancing" id="http_load_balancing"> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5893 |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5894 <para> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5895 The |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5896 <link doc="../http/ngx_http_upstream_module.xml">ngx_http_upstream_module</link> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5897 provides basic functionality to pass requests to remote servers. |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5898 This functionality is used by modules that implement specific protocols, |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5899 such as HTTP or FastCGI. |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5900 The module also provides an interface for creating custom |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5901 load balancing modules and implements a default round-robin balancing method. |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5902 </para> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5903 |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5904 <para> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5905 Examples of modules that implement alternative load balancing methods are |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5906 <link doc="../http/ngx_http_upstream_module.xml" id="least_conn"/> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5907 and <link doc="../http/ngx_http_upstream_module.xml" id="hash"/>. |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5908 Note that these modules are actually implemented as extensions of the upstream |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5909 module and share a lot of code, such as representation of a server group. |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5910 The <link doc="../http/ngx_http_upstream_module.xml" id="keepalive"/> module |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5911 is an example of an independent module, extending upstream functionality. |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5912 </para> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5913 |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5914 <para> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5915 The |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5916 <link doc="../http/ngx_http_upstream_module.xml">ngx_http_upstream_module</link> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5917 may be configured explicitly by placing the corresponding |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5918 <link doc="../http/ngx_http_upstream_module.xml" id="upstream"/> block into |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5919 the configuration file, or implicitly by using directives |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5920 that accept a URL evaluated at some point to the list of servers, |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5921 for example, |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5922 <link doc="../http/ngx_http_proxy_module.xml" id="proxy_pass"/>. |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5923 Only explicit configurations may use an alternative load balancing method. |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5924 The upstream module configuration has its own directive context |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5925 <literal>NGX_HTTP_UPS_CONF</literal>. |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5926 The structure is defined as follows: |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5927 <programlisting> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5928 struct ngx_http_upstream_srv_conf_s { |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5929 ngx_http_upstream_peer_t peer; |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5930 void **srv_conf; |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5931 |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5932 ngx_array_t *servers; /* ngx_http_upstream_server_t */ |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5933 |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5934 ngx_uint_t flags; |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5935 ngx_str_t host; |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5936 u_char *file_name; |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5937 ngx_uint_t line; |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5938 in_port_t port; |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5939 ngx_uint_t no_port; /* unsigned no_port:1 */ |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5940 |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5941 #if (NGX_HTTP_UPSTREAM_ZONE) |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5942 ngx_shm_zone_t *shm_zone; |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5943 #endif |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5944 }; |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5945 </programlisting> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5946 |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5947 <list type="bullet"> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5948 |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5949 <listitem> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5950 <literal>srv_conf</literal> — configuration context of upstream modules |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5951 </listitem> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5952 |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5953 <listitem> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5954 <literal>servers</literal> — array of |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5955 <literal>ngx_http_upstream_server_t</literal>, the result of parsing a set of |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5956 <link doc="../http/ngx_http_upstream_module.xml" id="server"/> directives |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5957 in the <literal>upstream</literal> block |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5958 </listitem> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5959 |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5960 <listitem> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5961 <literal>flags</literal> — flags that mostly mark which features |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5962 (configured as parameters of |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5963 the <link doc="../http/ngx_http_upstream_module.xml" id="server"/> directive) |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5964 are supported by the particular load balancing method. |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5965 |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5966 <list type="bullet"> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5967 |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5968 <listitem> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5969 <literal>NGX_HTTP_UPSTREAM_CREATE</literal> — used to distinguish explicitly |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5970 defined upstreams from automatically created by |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5971 <link doc="../http/ngx_http_proxy_module.xml" id="proxy_pass"/> and “friends” |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5972 (FastCGI, SCGI, etc.) |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5973 </listitem> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5974 |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5975 <listitem> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5976 <literal>NGX_HTTP_UPSTREAM_WEIGHT</literal> — “<literal>weight</literal>” |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5977 is supported |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5978 </listitem> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5979 |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5980 <listitem> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5981 <literal>NGX_HTTP_UPSTREAM_MAX_FAILS</literal> — “<literal>max_fails</literal>” |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5982 is supported |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5983 </listitem> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5984 |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5985 <listitem> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5986 <literal>NGX_HTTP_UPSTREAM_FAIL_TIMEOUT</literal> — |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5987 “<literal>fail_timeout</literal>” is supported |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5988 </listitem> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5989 |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5990 <listitem> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5991 <literal>NGX_HTTP_UPSTREAM_DOWN</literal> — “<literal>down</literal>” |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5992 is supported |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5993 </listitem> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5994 |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5995 <listitem> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5996 <literal>NGX_HTTP_UPSTREAM_BACKUP</literal> — “<literal>backup</literal>” |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5997 is supported |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5998 </listitem> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
5999 |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6000 <listitem> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6001 <literal>NGX_HTTP_UPSTREAM_MAX_CONNS</literal> — “<literal>max_conns</literal>” |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6002 is supported |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6003 </listitem> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6004 |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6005 </list> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6006 |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6007 </listitem> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6008 |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6009 <listitem> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6010 <literal>host</literal> — the name of an upstream |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6011 </listitem> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6012 |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6013 <listitem> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6014 <literal>file_name, line</literal> — the name of the configuration file |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6015 and the line where the <literal>upstream</literal> block is located |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6016 </listitem> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6017 |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6018 <listitem> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6019 <literal>port</literal> and <literal>no_port</literal> — unused by explicit |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6020 upstreams |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6021 </listitem> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6022 |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6023 <listitem> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6024 <literal>shm_zone</literal> — a shared memory zone used by this upstream, if any |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6025 </listitem> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6026 |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6027 <listitem> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6028 <literal>peer</literal> — an object that holds generic methods for |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6029 initializing upstream configuration: |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6030 |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6031 <programlisting> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6032 typedef struct { |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6033 ngx_http_upstream_init_pt init_upstream; |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6034 ngx_http_upstream_init_peer_pt init; |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6035 void *data; |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6036 } ngx_http_upstream_peer_t; |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6037 </programlisting> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6038 A module that implements a load balancing algorithm must set these |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6039 methods and initialize private <literal>data</literal>. |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6040 If <literal>init_upstream</literal> was not initialized during configuration |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6041 parsing, <literal>ngx_http_upstream_module</literal> sets it to default |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6042 <literal>ngx_http_upstream_init_round_robin</literal>. |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6043 |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6044 <list type="bullet"> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6045 <listitem> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6046 <literal>init_upstream(cf, us)</literal> — configuration-time |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6047 method responsible for initializing a group of servers and |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6048 initializing the <literal>init()</literal> method in case of success. |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6049 A typical load balancing module uses a list of servers in the upstream block |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6050 to create some efficient data structure that it uses and saves own |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6051 configuration to the <literal>data</literal> field. |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6052 </listitem> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6053 |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6054 <listitem> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6055 <literal>init(r, us)</literal> — initializes per-request |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6056 <literal>ngx_http_upstream_peer_t.peer</literal> (not to be confused with the |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6057 <literal>ngx_http_upstream_srv_conf_t.peer</literal> described above which |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6058 is per-upstream) structure that is used for load balancing. |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6059 It will be passed as <literal>data</literal> argument to all callbacks that |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6060 deal with server selection. |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6061 </listitem> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6062 </list> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6063 |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6064 </listitem> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6065 </list> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6066 </para> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6067 |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6068 <para> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6069 When nginx has to pass a request to another host for processing, it uses |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6070 a configured load balancing method to obtain an address to connect to. |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6071 The method is taken from the |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6072 <literal>ngx_http_upstream_peer_t.peer</literal> object |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6073 of type <literal>ngx_peer_connection_t</literal>: |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6074 <programlisting> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6075 struct ngx_peer_connection_s { |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6076 [...] |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6077 |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6078 struct sockaddr *sockaddr; |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6079 socklen_t socklen; |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6080 ngx_str_t *name; |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6081 |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6082 ngx_uint_t tries; |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6083 |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6084 ngx_event_get_peer_pt get; |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6085 ngx_event_free_peer_pt free; |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6086 ngx_event_notify_peer_pt notify; |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6087 void *data; |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6088 |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6089 #if (NGX_SSL || NGX_COMPAT) |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6090 ngx_event_set_peer_session_pt set_session; |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6091 ngx_event_save_peer_session_pt save_session; |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6092 #endif |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6093 |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6094 [..] |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6095 }; |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6096 </programlisting> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6097 |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6098 The structure has the following fields: |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6099 |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6100 <list type="bullet"> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6101 <listitem> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6102 <literal>sockaddr</literal>, <literal>socklen</literal>, |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6103 <literal>name</literal> — address of an upstream server to connect to; |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6104 this is the output parameter of a load balancing method |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6105 </listitem> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6106 |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6107 <listitem> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6108 <literal>data</literal> — per-request load balancing method data; keeps the |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6109 state of selection algorithm and usually includes the link to upstream |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6110 configuration. |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6111 It will be passed as an argument to all methods that deal with server selection |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6112 (see below) |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6113 </listitem> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6114 |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6115 <listitem> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6116 <literal>tries</literal> — allowed |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6117 <link doc="../http/ngx_http_proxy_module.xml" id="proxy_next_upstream_tries">number</link> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6118 of attempts to connect to an upstream. |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6119 </listitem> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6120 |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6121 <listitem> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6122 <literal>get</literal>, <literal>free</literal>, <literal>notify</literal>, |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6123 <literal>set_session</literal>, and <literal>save_session</literal> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6124 - methods of the load balancing module, see description below |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6125 </listitem> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6126 |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6127 </list> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6128 |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6129 </para> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6130 |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6131 <para> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6132 All methods accept at least two arguments: peer connection object |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6133 <literal>pc</literal> and the <literal>data</literal> created by |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6134 <literal>ngx_http_upstream_srv_conf_t.peer.init()</literal>. |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6135 Note that in general case it may differ from <literal>pc.data</literal> due |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6136 to “chaining” of load balancing modules. |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6137 </para> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6138 |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6139 <para> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6140 |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6141 <list type="bullet"> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6142 <listitem> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6143 <literal>get(pc, data)</literal> — the method is called when the upstream |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6144 module is ready to pass a request to an upstream server and needs to know |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6145 its address. |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6146 The method is responsible to fill in the <literal>sockaddr</literal>, |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6147 <literal>socklen</literal>, and <literal>name</literal> fields of |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6148 <literal>ngx_peer_connection_t</literal> structure. |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6149 The return value may be one of: |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6150 |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6151 <list type="bullet"> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6152 |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6153 <listitem> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6154 <literal>NGX_OK</literal> — server was selected |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6155 </listitem> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6156 |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6157 <listitem> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6158 <literal>NGX_ERROR</literal> — internal error occurred |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6159 </listitem> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6160 |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6161 <listitem> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6162 <literal>NGX_BUSY</literal> — there are no available servers at the moment. |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6163 This can happen due to many reasons, such as: dynamic server group is empty, |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6164 all servers in the group are in the failed state, |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6165 all servers in the group are already |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6166 handling the maximum number of connections or similar. |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6167 </listitem> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6168 |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6169 <listitem> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6170 <literal>NGX_DONE</literal> — this is set by the <literal>keepalive</literal> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6171 module to indicate that the underlying connection was reused and there is no |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6172 need to create a new connection to the upstream server. |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6173 </listitem> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6174 |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6175 <!-- |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6176 <listitem> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6177 <literal>NGX_ABORT</literal> — this is set by the <literal>queue</literal> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6178 module to indicate that the request was queued and the further processing |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6179 of this request should be postponed. |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6180 </listitem> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6181 --> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6182 |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6183 </list> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6184 |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6185 </listitem> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6186 |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6187 <listitem> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6188 <literal>free(pc, data, state)</literal> — the method is called when an |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6189 upstream module has finished work with a particular server. |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6190 The <literal>state</literal> argument is the status of upstream connection |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6191 completion. |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6192 This is a bitmask, the following values may be set: |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6193 <literal>NGX_PEER_FAILED</literal> — this attempt is considered |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6194 <link doc="../http/ngx_http_upstream_module.xml" id="max_fails">unsuccessful</link>, |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6195 <literal>NGX_PEER_NEXT</literal> — a special case with codes 403 and 404 |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6196 (see link above), which are not considered a failure. |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6197 <literal>NGX_PEER_KEEPALIVE</literal>. |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6198 Also, <literal>tries</literal> counter is decremented by this method. |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6199 </listitem> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6200 |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6201 <listitem> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6202 <literal>notify(pc, data, type)</literal> — currently unused |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6203 in the OSS version. |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6204 </listitem> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6205 |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6206 <listitem> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6207 <literal>set_session(pc, data)</literal> and |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6208 <literal>save_session(pc, data)</literal> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6209 — SSL-specific methods that allow to cache sessions to upstream |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6210 servers. |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6211 The implementation is provided by the round-robin balancing method. |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6212 </listitem> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6213 |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6214 </list> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6215 |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6216 </para> |
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6217 |
1929
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
6218 </section> |
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
6219 |
1939
c1b0169e8f54
Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents:
1936
diff
changeset
|
6220 </section> |
1929
7f290929b32d
HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents:
1928
diff
changeset
|
6221 |
1899 | 6222 </article> |