annotate xml/en/docs/dev/development_guide.xml @ 1967:ef27e3ef0c46

The HTTP request redirection section of the development guide.
author Roman Arutyunyan <arut@nginx.com>
date Wed, 19 Apr 2017 18:35:05 +0300
parents 9550ea66abdd
children 69908bd68481
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1 <?xml version="1.0"?>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
3 <!--
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
4 Copyright (C) Nginx, Inc.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
5 -->
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
6
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
7 <!DOCTYPE article SYSTEM "../../../../dtd/article.dtd">
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
8
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
9 <article name="Development guide"
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
10 link="/en/docs/dev/development_guide.html"
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
11 lang="en"
1914
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
12 rev="2">
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
13
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
14 <section name="Introduction" id="introduction">
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
15
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
16
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
17 <section name="Code layout" id="code_layout">
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
18
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
19 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
20 <list type="bullet">
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
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
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
23 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
24
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
25 <listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
26 <literal>src</literal>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
27
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
28 <list type="bullet">
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
29
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
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
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
32 pool etc
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
33 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
34
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
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
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
37
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
38 <list type="bullet">
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
39
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
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
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
42 select etc
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
43 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
44
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
45 </list>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
46
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
47 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
48
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
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
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
51
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
52 <list type="bullet">
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
53
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
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
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
60 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
61
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
62 </list>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
63
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
64 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
65
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
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
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
72
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
73 <list type="bullet">
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
74
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
75 <listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
76 <literal>unix</literal>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
77 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
78
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
79 <listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
80 <literal>win32</literal>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
81 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
82
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
83 </list>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
84
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
85 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
86
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
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
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
89 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
90
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
91 </list>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
92
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
93 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
94
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
95 </list>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
96 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
97
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
98 </section>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
99
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
100
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
101 <section name="Include files" id="include_files">
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
102
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
103 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
104 Each nginx file should start with including the following two files:
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
105 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
106
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
107
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
108 <programlisting>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
109 #include &lt;ngx_config.h>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
110 #include &lt;ngx_core.h>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
111 </programlisting>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
112
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
113 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
114 In addition to that, HTTP code should include
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
115 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
116
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
117
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
118 <programlisting>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
119 #include &lt;ngx_http.h>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
120 </programlisting>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
121
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
122 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
123 Mail code should include
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
124 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
125
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
126
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
127 <programlisting>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
128 #include &lt;ngx_mail.h>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
129 </programlisting>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
130
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
131 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
132 Stream code should include
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
133 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
134
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
135
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
136 <programlisting>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
137 #include &lt;ngx_stream.h>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
138 </programlisting>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
139
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
140 </section>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
141
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
142
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
143 <section name="Integers" id="integers">
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
144
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
145 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
146 For general purpose, nginx code uses the following two integer types
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
147 <literal>ngx_int_t</literal> and <literal>ngx_uint_t</literal> which are
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
148 typedefs for <literal>intptr_t</literal> and <literal>uintptr_t</literal>.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
149 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
150
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
151 </section>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
152
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
153
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
154 <section name="Common return codes" id="common_return_codes">
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
155
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
156 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
157 Most functions in nginx return the following codes:
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
158 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
159
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
160 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
161 <list type="bullet">
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
162
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
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
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
173 again
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
174 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
175
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
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
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
178 in configuration. This is never an error
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
179 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
180
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
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
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
187 Also used as an alternative success code
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
188 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
189
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
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
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
192 Also used as an alternative error code
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
193 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
194
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
195 </list>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
196 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
197
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
198 </section>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
199
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
200
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
201 <section name="Error handling" id="error_handling">
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
202
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
203 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
204 For getting the last system error code, the <literal>ngx_errno</literal> macro
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
205 is available.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
206 It's mapped to <literal>errno</literal> on POSIX platforms and to
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
207 <literal>GetLastError()</literal> call in Windows.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
208 For getting the last socket error number, the
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
209 <literal>ngx_socket_errno</literal> macro is available.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
210 It's mapped to <literal>errno</literal> on POSIX systems as well,
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
211 and to <literal>WSAGetLastError()</literal> call on Windows.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
212 For performance reasons the values of <literal>ngx_errno</literal> or
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
213 <literal>ngx_socket_errno</literal> should not be accessed more than
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
214 once in a row.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
215 The error value should be stored in a local variable of type
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
216 <literal>ngx_err_t</literal> for using multiple times, if required.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
217 For setting errors, <literal>ngx_set_errno(errno)</literal> and
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
218 <literal>ngx_set_socket_errno(errno)</literal> macros are available.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
219 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
220
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
221 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
222 The values of <literal>ngx_errno</literal> or
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
223 <literal>ngx_socket_errno</literal> can be passed to logging functions
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
224 <literal>ngx_log_error()</literal> and <literal>ngx_log_debugX()</literal>, in
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
225 which case system error text is added to the log message.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
226 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
227
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
228 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
229 Example using <literal>ngx_errno</literal>:
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
230 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
231
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
232
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
233 <programlisting>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
234 void
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
235 ngx_my_kill(ngx_pid_t pid, ngx_log_t *log, int signo)
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
236 {
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
237 ngx_err_t err;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
238
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
239 if (kill(pid, signo) == -1) {
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
240 err = ngx_errno;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
241
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
242 ngx_log_error(NGX_LOG_ALERT, log, err, "kill(%P, %d) failed", pid, signo);
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
243
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
244 if (err == NGX_ESRCH) {
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
245 return 2;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
246 }
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
247
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
248 return 1;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
249 }
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
250
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
251 return 0;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
252 }
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
253 </programlisting>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
254
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
255 </section>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
256
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
257
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
258 </section>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
259
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
260
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
261 <section name="Strings" id="strings">
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
262
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
263
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
264 <section name="Overview" id="overview">
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
265
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
266 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
267 For C strings, nginx code uses unsigned character type pointer
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
268 <literal>u_char *</literal>.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
269 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
270
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
271 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
272 The nginx string type <literal>ngx_str_t</literal> is defined as follows:
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
273 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
274
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
275
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
276 <programlisting>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
277 typedef struct {
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
278 size_t len;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
279 u_char *data;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
280 } ngx_str_t;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
281 </programlisting>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
282
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
283 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
284 The <literal>len</literal> field holds the string length,
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
285 <literal>data</literal> holds the string data.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
286 The string, held in <literal>ngx_str_t</literal>, may or may not be
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
287 null-terminated after the <literal>len</literal> bytes.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
288 In most cases it’s not.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
289 However, in certain parts of code (for example, when parsing configuration),
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
290 <literal>ngx_str_t</literal> objects are known to be null-terminated, and that
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
291 knowledge is used to simplify string comparison and makes it easier to pass
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
292 those strings to syscalls.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
293 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
294
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
295 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
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
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
298 Some of them are wrappers around standard C functions:
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
299 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
300
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
301 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
302 <list type="bullet">
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
303
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
304 <listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
305 <literal>ngx_strcmp()</literal>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
306 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
307
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
308 <listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
309 <literal>ngx_strncmp()</literal>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
310 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
311
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
312 <listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
313 <literal>ngx_strstr()</literal>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
314 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
315
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
316 <listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
317 <literal>ngx_strlen()</literal>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
318 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
319
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
320 <listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
321 <literal>ngx_strchr()</literal>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
322 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
323
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
324 <listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
325 <literal>ngx_memcmp()</literal>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
326 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
327
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
328 <listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
329 <literal>ngx_memset()</literal>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
330 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
331
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
332 <listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
333 <literal>ngx_memcpy()</literal>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
334 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
335
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
336 <listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
337 <literal>ngx_memmove()</literal>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
338 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
339
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
340 </list>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
341
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
342 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
343
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
344 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
345 Some nginx-specific string functions:
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
346 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
347
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
348 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
349 <list type="bullet">
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
350
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
351 <listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
352 <literal>ngx_memzero()</literal> fills memory with zeroes
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
353 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
354
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
355 <listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
356 <literal>ngx_cpymem()</literal> does the same as
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
357 <literal>ngx_memcpy()</literal>, but returns the final destination address
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
358 This one is handy for appending multiple strings in a row
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
359 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
360
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
361 <listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
362 <literal>ngx_movemem()</literal> does the same as
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
363 <literal>ngx_memmove()</literal>, but returns the final destination address.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
364 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
365
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
366 <listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
367 <literal>ngx_strlchr()</literal> searches for a character in a string,
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
368 delimited by two pointers
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
369 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
370 </list>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
371 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
372
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
373 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
374 Some case conversion and comparison functions:
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
375 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
376
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
377 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
378 <list type="bullet">
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
379
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
380 <listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
381 <literal>ngx_tolower()</literal>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
382 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
383
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
384 <listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
385 <literal>ngx_toupper()</literal>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
386 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
387
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
388 <listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
389 <literal>ngx_strlow()</literal>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
390 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
391
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
392 <listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
393 <literal>ngx_strcasecmp()</literal>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
394 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
395
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
396 <listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
397 <literal>ngx_strncasecmp()</literal>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
398 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
399
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
400 </list>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
401 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
402
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
403 </section>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
404
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
405
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
406 <section name="Formatting" id="formatting">
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
407
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
408 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
409 A number of formatting functions are provided by nginx. These functions support nginx-specific types:
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
410 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
411
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
412
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
413 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
414 <list type="bullet">
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
415
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
416 <listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
417 <literal>ngx_sprintf(buf, fmt, ...)</literal>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
418 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
419
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
420 <listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
421 <literal>ngx_snprintf(buf, max, fmt, ...)</literal>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
422 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
423
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
424 <listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
425 <literal>ngx_slrintf(buf, last, fmt, ...)</literal>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
426 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
427
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
428 <listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
429 <literal>ngx_vslprint(buf, last, fmt, args)</literal>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
430 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
431
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
432 <listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
433 <literal>ngx_vsnprint(buf, max, fmt, args)</literal>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
434 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
435
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
436 </list>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
437 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
438
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
439 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
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
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
442 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
443
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
444
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
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
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
454 </programlisting>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
455
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
456 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
457 The ‘u’ modifier makes most types unsigned, ‘X’/‘x’ convert output to hex.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
458 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
459
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
460 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
461 Example:
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
462
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
463 <programlisting>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
464 u_char buf[NGX_INT_T_LEN];
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
465 size_t len;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
466 ngx_int_t n;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
467
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
468 /* set n here */
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
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
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
471 </programlisting>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
472
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
473 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
474
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
475 </section>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
476
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
477
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
478 <section name="Numeric conversion" id="numeric_conversion">
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
479
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
480 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
481 Several functions for numeric conversion are implemented in nginx:
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
482 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
483
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
484 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
485 <list type="bullet">
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
486
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
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
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
489 positive integer of type <literal>ngx_int_t</literal>.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
490 Returns <literal>NGX_ERROR</literal> on error
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
491 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
492
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
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
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
495 type
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
496 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
497
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
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
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
500 type
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
501 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
502
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
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
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
505 type
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
506 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
507
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
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
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
510 number of given length to a positive integer of type
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
511 <literal>ngx_int_t</literal>.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
512 The result is shifted left by <literal>points</literal> decimal
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
513 positions. The string representation of the number is expected to have no more
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
514 than <literal>points</literal> fractional digits.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
515 Returns <literal>NGX_ERROR</literal> on error. For example,
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
516 <literal>ngx_atofp("10.5", 4, 2)</literal> returns <literal>1050</literal>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
517 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
518
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
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
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
521 a positive integer to <literal>ngx_int_t</literal>. Returns
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
522 <literal>NGX_ERROR</literal> on error
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
523 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
524
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
525 </list>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
526 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
527
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
528 </section>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
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 '(?&lt;cw&gt;\\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(&amp;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(&amp;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", &amp;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, &amp;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 &lt; 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 &lt; 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 = &amp;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] &lt;&lt; 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 = &amp;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
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
651
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
652 </section>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
653
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
654
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
655 <section name="Containers" id="containers">
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
656
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
657
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
658 <section name="Array" id="array">
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
659
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
660 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
661 The nginx array type <literal>ngx_array_t</literal> is defined as follows
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
662 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
663
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
664
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
665 <programlisting>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
666 typedef struct {
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
667 void *elts;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
668 ngx_uint_t nelts;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
669 size_t size;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
670 ngx_uint_t nalloc;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
671 ngx_pool_t *pool;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
672 } ngx_array_t;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
673 </programlisting>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
674
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
675 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
676 The elements of array are available through the <literal>elts</literal> field.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
677 The number of elements is held in the <literal>nelts</literal> field.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
678 The <literal>size</literal> field holds the size of a single element and is set
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
679 when initializing the array.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
680 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
681
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
682 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
683 An array can be created in a pool with the
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
684 <literal>ngx_array_create(pool, n, size)</literal> call.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
685 An already allocated array object can be initialized with the
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
686 <literal>ngx_array_init(array, pool, n, size)</literal> call.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
687 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
688
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
689
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
690 <programlisting>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
691 ngx_array_t *a, b;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
692
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
693 /* create an array of strings with preallocated memory for 10 elements */
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
694 a = ngx_array_create(pool, 10, sizeof(ngx_str_t));
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
695
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
696 /* initialize string array for 10 elements */
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
697 ngx_array_init(&amp;b, pool, 10, sizeof(ngx_str_t));
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
698 </programlisting>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
699
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
700 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
701 Adding elements to array are done with the following functions:
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
702 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
703
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
704 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
705 <list type="bullet">
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
706
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
707 <listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
708 <literal>ngx_array_push(a)</literal> adds one tail element and returns pointer
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
709 to it
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
710 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
711
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
712 <listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
713 <literal>ngx_array_push_n(a, n)</literal> adds <literal>n</literal> tail elements
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
714 and returns pointer to the first one
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
715 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
716
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
717 </list>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
718 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
719
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
720 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
721 If currently allocated memory is not enough for new elements, a new memory for
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
722 elements is allocated and existing elements are copied to that memory.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
723 The new memory block is normally twice as large, as the existing one.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
724 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
725
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
726
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
727 <programlisting>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
728 s = ngx_array_push(a);
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
729 ss = ngx_array_push_n(&amp;b, 3);
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
730 </programlisting>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
731
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
732 </section>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
733
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
734
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
735 <section name="List" id="list">
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
736
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
737 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
738 List in nginx is a sequence of arrays, optimized for inserting a potentially
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
739 large number of items. The list type is defined as follows:
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
740 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
741
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
742
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
743 <programlisting>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
744 typedef struct {
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
745 ngx_list_part_t *last;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
746 ngx_list_part_t part;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
747 size_t size;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
748 ngx_uint_t nalloc;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
749 ngx_pool_t *pool;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
750 } ngx_list_t;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
751 </programlisting>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
752
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
753 <para>
1958
95a7e6eb5270 Fixed spelling.
Roman Arutyunyan <arut@nginx.com>
parents: 1939
diff changeset
754 The actual items are stored in list parts, defined as follows:
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
755 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
756
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
757
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
758 <programlisting>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
759 typedef struct ngx_list_part_s ngx_list_part_t;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
760
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
761 struct ngx_list_part_s {
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
762 void *elts;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
763 ngx_uint_t nelts;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
764 ngx_list_part_t *next;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
765 };
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
766 </programlisting>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
767
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
768 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
769 Initially, a list must be initialized by calling
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
770 <literal>ngx_list_init(list, pool, n, size)</literal> or created by calling
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
771 <literal>ngx_list_create(pool, n, size)</literal>.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
772 Both functions receive the size of a single item and a number of items per list
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
773 part.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
774 The <literal>ngx_list_push(list)</literal> function is used to add an item to the
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
775 list. Iterating over the items is done by direct accessing the list fields, as
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
776 seen in the example:
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
777 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
778
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
779
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
780 <programlisting>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
781 ngx_str_t *v;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
782 ngx_uint_t i;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
783 ngx_list_t *list;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
784 ngx_list_part_t *part;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
785
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
786 list = ngx_list_create(pool, 100, sizeof(ngx_str_t));
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
787 if (list == NULL) { /* error */ }
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
788
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
789 /* add items to the list */
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
790
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
791 v = ngx_list_push(list);
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
792 if (v == NULL) { /* error */ }
1936
8f996938fe23 Style: proper quotes usage.
Vladimir Homutov <vl@nginx.com>
parents: 1935
diff changeset
793 ngx_str_set(v, "foo");
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
794
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
795 v = ngx_list_push(list);
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
796 if (v == NULL) { /* error */ }
1936
8f996938fe23 Style: proper quotes usage.
Vladimir Homutov <vl@nginx.com>
parents: 1935
diff changeset
797 ngx_str_set(v, "bar");
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
798
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
799 /* iterate over the list */
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
800
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
801 part = &amp;list->part;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
802 v = part->elts;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
803
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
804 for (i = 0; /* void */; i++) {
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
805
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
806 if (i >= part->nelts) {
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
807 if (part->next == NULL) {
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
808 break;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
809 }
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
810
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
811 part = part->next;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
812 v = part->elts;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
813 i = 0;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
814 }
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
815
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
816 ngx_do_smth(&amp;v[i]);
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
817 }
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
818 </programlisting>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
819
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
820 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
821 The primary use for the list in nginx is HTTP input and output headers.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
822 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
823
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
824 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
825 The list does not support item removal.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
826 However, when needed, items can internally be marked as missing without actual
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
827 removing from the list.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
828 For example, HTTP output headers which are stored as
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
829 <literal>ngx_table_elt_t</literal> objects, are marked as missing by setting
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
830 the <literal>hash</literal> field of <literal>ngx_table_elt_t</literal> to
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
831 zero. Such items are explicitly skipped, when iterating over the headers.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
832 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
833
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
834 </section>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
835
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
836
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
837 <section name="Queue" id="queue">
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
838
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
839 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
840 Queue in nginx is an intrusive doubly linked list, with each node defined as
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
841 follows:
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
842 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
843
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
844
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
845 <programlisting>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
846 typedef struct ngx_queue_s ngx_queue_t;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
847
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
848 struct ngx_queue_s {
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
849 ngx_queue_t *prev;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
850 ngx_queue_t *next;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
851 };
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
852 </programlisting>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
853
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
854 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
855 The head queue node is not linked with any data. Before using, the list head
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
856 should be initialized with <literal>ngx_queue_init(q)</literal> call.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
857 Queues support the following operations:
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
858 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
859
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
860 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
861 <list type="bullet">
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
862
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
863 <listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
864 <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
865 <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
866 </listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
867
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
868 <listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
869 <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
870 </listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
871
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
872 <listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
873 <literal>ngx_queue_split(h, q, n)</literal> — split a queue at a node,
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
874 queue tail is returned in a separate queue
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
875 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
876
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
877 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
878 <literal>ngx_queue_add(h, n)</literal> — add second queue to the first queue
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
879 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
880
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
881 <listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
882 <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
883 <literal>ngx_queue_last(h)</literal> — get first or last queue node
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
884 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
885
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
886 <listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
887 <literal>ngx_queue_sentinel(h)</literal>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
888 - get a queue sentinel object to end iteration at
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
889 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
890
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
891 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
892 <literal>ngx_queue_data(q, type, link)</literal> — get reference to the beginning of a
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
893 queue node data structure, considering the queue field offset in it
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
894 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
895
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
896 </list>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
897 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
898
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
899 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
900 Example:
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
901 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
902
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
903
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
904 <programlisting>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
905 typedef struct {
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
906 ngx_str_t value;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
907 ngx_queue_t queue;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
908 } ngx_foo_t;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
909
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
910 ngx_foo_t *f;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
911 ngx_queue_t values;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
912
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
913 ngx_queue_init(&amp;values);
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
914
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
915 f = ngx_palloc(pool, sizeof(ngx_foo_t));
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
916 if (f == NULL) { /* error */ }
1936
8f996938fe23 Style: proper quotes usage.
Vladimir Homutov <vl@nginx.com>
parents: 1935
diff changeset
917 ngx_str_set(&amp;f->value, "foo");
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
918
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
919 ngx_queue_insert_tail(&amp;values, f);
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
920
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
921 /* insert more nodes here */
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
922
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
923 for (q = ngx_queue_head(&amp;values);
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
924 q != ngx_queue_sentinel(&amp;values);
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
925 q = ngx_queue_next(q))
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
926 {
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
927 f = ngx_queue_data(q, ngx_foo_t, queue);
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
928
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
929 ngx_do_smth(&amp;f->value);
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
930 }
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
931 </programlisting>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
932
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
933 </section>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
934
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
935
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
936 <section name="Red-Black tree" id="red_black_tree">
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
937
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
938 <para>
1915
8b7c3b0ef1a4 Semantically marked paths.
Vladimir Homutov <vl@nginx.com>
parents: 1914
diff changeset
939 The <path>src/core/ngx_rbtree.h</path> header file provides access to the
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
940 effective implementation of red-black trees.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
941 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
942
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
943
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
944 <programlisting>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
945 typedef struct {
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
946 ngx_rbtree_t rbtree;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
947 ngx_rbtree_node_t sentinel;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
948
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
949 /* custom per-tree data here */
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
950 } my_tree_t;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
951
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
952 typedef struct {
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
953 ngx_rbtree_node_t rbnode;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
954
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
955 /* custom per-node data */
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
956 foo_t val;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
957 } my_node_t;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
958 </programlisting>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
959
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
960 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
961 To deal with a tree as a whole, you need two nodes: root and sentinel.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
962 Typically, they are added to some custom structure, thus allowing to
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
963 organize your data into a tree which leaves contain a link to or embed
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
964 your data.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
965 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
966
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
967 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
968 To initialize a tree:
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
969 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
970
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
971
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
972 <programlisting>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
973 my_tree_t root;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
974
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
975 ngx_rbtree_init(&amp;root.rbtree, &amp;root.sentinel, insert_value_function);
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
976 </programlisting>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
977
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
978 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
979 The <literal>insert_value_function</literal> is a function that is
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
980 responsible for traversing the tree and inserting new values into correct
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
981 place.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
982 For example, the <literal>ngx_str_rbtree_insert_value</literal> functions is
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
983 designed to deal with <literal>ngx_str_t</literal> type.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
984 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
985
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
986
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
987 <programlisting>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
988 void ngx_str_rbtree_insert_value(ngx_rbtree_node_t *temp,
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
989 ngx_rbtree_node_t *node,
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
990 ngx_rbtree_node_t *sentinel)
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
991 </programlisting>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
992
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
993 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
994 Its arguments are pointers to a root node of an insertion, newly created node
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
995 to be added, and a tree sentinel.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
996 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
997
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
998 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
999 The traversal is pretty straightforward and can be demonstrated with the
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1000 following lookup function pattern:
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1001 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1002
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1003
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1004 <programlisting>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1005 my_node_t *
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1006 my_rbtree_lookup(ngx_rbtree_t *rbtree, foo_t *val, uint32_t hash)
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1007 {
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1008 ngx_int_t rc;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1009 my_node_t *n;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1010 ngx_rbtree_node_t *node, *sentinel;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1011
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1012 node = rbtree->root;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1013 sentinel = rbtree->sentinel;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1014
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1015 while (node != sentinel) {
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1016
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1017 n = (my_node_t *) node;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1018
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1019 if (hash != node->key) {
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1020 node = (hash &lt; node->key) ? node->left : node->right;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1021 continue;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1022 }
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1023
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1024 rc = compare(val, node->val);
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1025
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1026 if (rc &lt; 0) {
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1027 node = node->left;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1028 continue;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1029 }
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1030
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1031 if (rc > 0) {
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1032 node = node->right;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1033 continue;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1034 }
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1035
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1036 return n;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1037 }
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1038
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1039 return NULL;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1040 }
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1041 </programlisting>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1042
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1043 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1044 The <literal>compare()</literal> is a classic comparator function returning
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1045 value less, equal or greater than zero. To speed up lookups and avoid comparing
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1046 user objects that can be big, integer hash field is used.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1047 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1048
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1049 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1050 To add a node to a tree, allocate a new node, initialize it and call
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1051 <literal>ngx_rbtree_insert()</literal>:
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1052 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1053
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1054
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1055 <programlisting>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1056 my_node_t *my_node;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1057 ngx_rbtree_node_t *node;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1058
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1059 my_node = ngx_palloc(...);
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1060 init_custom_data(&amp;my_node->val);
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1061
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1062 node = &amp;my_node->rbnode;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1063 node->key = create_key(my_node->val);
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1064
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1065 ngx_rbtree_insert(&amp;root->rbtree, node);
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1066 </programlisting>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1067
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1068 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1069 to remove a node:
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1070 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1071
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1072
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1073 <programlisting>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1074 ngx_rbtree_delete(&amp;root->rbtree, node);
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1075 </programlisting>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1076
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1077 </section>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1078
1914
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1079 <section name="Hash" id="hash">
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1080
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1081 <para>
1920
de5251816480 Fixed a typo.
Vladimir Homutov <vl@nginx.com>
parents: 1919
diff changeset
1082 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
1083 Exact and wildcard matching is supported.
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1084 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
1085 </para>
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1086
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1087 <para>
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1088 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
1089 so that nginx can build the hash optimally.
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1090 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
1091 and <literal>bucket_size</literal>.
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1092 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
1093 <link doc="../hash.xml">document</link>.
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1094 Usually, these two parameters are configurable by user.
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1095 Hash initialization settings are stored as the
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1096 <literal>ngx_hash_init_t</literal> type,
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1097 and the hash itself is <literal>ngx_hash_t</literal>:
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1098 <programlisting>
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1099 ngx_hash_t foo_hash;
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1100 ngx_hash_init_t hash;
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1101
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1102 hash.hash = &amp;foo_hash;
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1103 hash.key = ngx_hash_key;
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1104 hash.max_size = 512;
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1105 hash.bucket_size = ngx_align(64, ngx_cacheline_size);
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1106 hash.name = "foo_hash";
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1107 hash.pool = cf-&gt;pool;
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1108 hash.temp_pool = cf-&gt;temp_pool;
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1109 </programlisting>
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1110 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
1111 key from a string.
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1112 Two generic functions are provided:
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1113 <literal>ngx_hash_key(data, len)</literal> and
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1114 <literal>ngx_hash_key_lc(data, len)</literal>.
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1115 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
1116 be writable.
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1117 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
1118 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
1119 </para>
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1120
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1121 <para>
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1122 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
1123 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
1124 <programlisting>
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1125 ngx_hash_keys_arrays_t foo_keys;
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1126
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1127 foo_keys.pool = cf-&gt;pool;
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1128 foo_keys.temp_pool = cf-&gt;temp_pool;
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1129
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1130 ngx_hash_keys_array_init(&amp;foo_keys, NGX_HASH_SMALL);
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1131 </programlisting>
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1132 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
1133 <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
1134 resources for the hash.
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1135 If you expect the hash to contain thousands elements,
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1136 use <literal>NGX_HASH_LARGE</literal>.
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1137 </para>
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1138
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1139 <para>
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1140 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
1141 function is used to insert keys into hash keys array;
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1142 <programlisting>
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1143 ngx_str_t k1 = ngx_string("key1");
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1144 ngx_str_t k2 = ngx_string("key2");
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1145
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1146 ngx_hash_add_key(&amp;foo_keys, &amp;k1, &amp;my_data_ptr_1, NGX_HASH_READONLY_KEY);
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1147 ngx_hash_add_key(&amp;foo_keys, &amp;k2, &amp;my_data_ptr_2, NGX_HASH_READONLY_KEY);
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1148 </programlisting>
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1149 </para>
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1150
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1151 <para>
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1152 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
1153 <literal>ngx_hash_init(hinit, key_names, nelts)</literal>:
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1154
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1155 <programlisting>
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1156 ngx_hash_init(&amp;hash, foo_keys.keys.elts, foo_keys.keys.nelts);
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1157 </programlisting>
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1158
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1159 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
1160 parameters are not big enough.
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1161 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
1162 function may be used to look up elements:
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1163 <programlisting>
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1164 my_data_t *data;
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1165 ngx_uint_t key;
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1166
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1167 key = ngx_hash_key(k1.data, k1.len);
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1168
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1169 data = ngx_hash_find(&amp;foo_hash, key, k1.data, k1.len);
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1170 if (data == NULL) {
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1171 /* key not found */
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1172 }
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1173 </programlisting>
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1174
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1175 </para>
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1176
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1177 <section name="Wildcard matching" id="wildcard_matching">
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1178
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1179 <para>
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1180 To create a hash that works with wildcards,
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1181 <literal>ngx_hash_combined_t</literal> type is used.
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1182 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
1183 <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
1184 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
1185 <programlisting>
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1186 ngx_hash_init_t hash
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1187 ngx_hash_combined_t foo_hash;
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 hash.hash = &amp;foo_hash.hash;
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1190 hash.key = ...;
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1191 </programlisting>
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1192 </para>
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1193
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1194 <para>
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1195 It is possible to add wildcard keys using the
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1196 <literal>NGX_HASH_WILDCARD_KEY</literal> flag:
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1197 <programlisting>
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1198 /* k1 = ".example.org"; */
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1199 /* k2 = "foo.*"; */
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1200 ngx_hash_add_key(&amp;foo_keys, &amp;k1, &amp;data1, NGX_HASH_WILDCARD_KEY);
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1201 ngx_hash_add_key(&amp;foo_keys, &amp;k2, &amp;data2, NGX_HASH_WILDCARD_KEY);
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1202 </programlisting>
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1203 The function recognizes wildcards and adds keys into corresponding arrays.
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1204 Please refer to the
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1205 <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
1206 documentation for the description of the wildcard syntax and
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1207 matching algorithm.
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1208 </para>
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1209
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1210 <para>
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1211 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
1212 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
1213 starting from head or tail of a string:
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1214 <programlisting>
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1215 if (foo_keys.dns_wc_head.nelts) {
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1216
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1217 ngx_qsort(foo_keys.dns_wc_head.elts,
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1218 (size_t) foo_keys.dns_wc_head.nelts,
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1219 sizeof(ngx_hash_key_t),
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1220 cmp_dns_wildcards);
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1221
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1222 hash.hash = NULL;
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1223 hash.temp_pool = pool;
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1224
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1225 if (ngx_hash_wildcard_init(&amp;hash, foo_keys.dns_wc_head.elts,
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1226 foo_keys.dns_wc_head.nelts)
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1227 != NGX_OK)
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 return NGX_ERROR;
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1230 }
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 foo_hash.wc_head = (ngx_hash_wildcard_t *) hash.hash;
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1233 }
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1234 </programlisting>
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1235 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
1236 to the combined hash.
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1237 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
1238 </para>
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1239
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1240 <para>
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1241 The lookup in a combined hash is handled by the
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1242 <literal>ngx_hash_find_combined(chash, key, name, len)</literal>:
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1243 <programlisting>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
1244 /* 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
1245 /* key = "foo.example.com"; — will match "foo.*" */
1914
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1246
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1247 hkey = ngx_hash_key(key.data, key.len);
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1248 res = ngx_hash_find_combined(&amp;foo_hash, hkey, key.data, key.len);
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1249 </programlisting>
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1250 </para>
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1251
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1252 </section>
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1253
f8659301a260 Added hash API description.
Vladimir Homutov <vl@nginx.com>
parents: 1907
diff changeset
1254 </section>
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1255
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1256 </section>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1257
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1258
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1259 <section name="Memory management" id="memory_management">
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1260
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1261
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1262 <section name="Heap" id="heap">
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1263
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1264 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1265 To allocate memory from system heap, the following functions are provided by
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1266 nginx:
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1267 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1268
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1269 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1270 <list type="bullet">
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1271
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1272 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
1273 <literal>ngx_alloc(size, log)</literal> — allocate memory from system heap.
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1274 This is a wrapper around <literal>malloc()</literal> with logging support.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1275 Allocation error and debugging information is logged to <literal>log</literal>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1276 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1277
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1278 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
1279 <literal>ngx_calloc(size, log)</literal> — same as
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1280 <literal>ngx_alloc()</literal>, but memory is filled with zeroes after
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1281 allocation
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1282 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1283
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1284 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
1285 <literal>ngx_memalign(alignment, size, log)</literal> — allocate aligned memory
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1286 from system heap. This is a wrapper around <literal>posix_memalign()</literal>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1287 on those platforms which provide it.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1288 Otherwise implementation falls back to <literal>ngx_alloc()</literal> which
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1289 provides maximum alignment
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1290 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1291
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1292 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
1293 <literal>ngx_free(p)</literal> — free allocated memory.
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1294 This is a wrapper around <literal>free()</literal>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1295 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1296
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1297 </list>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1298 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1299
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1300 </section>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1301
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1302
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1303 <section name="Pool" id="pool">
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1304
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1305 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1306 Most nginx allocations are done in pools. Memory allocated in an nginx pool is
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1307 freed automatically when the pool in destroyed. This provides good
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1308 allocation performance and makes memory control easy.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1309 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1310
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1311 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1312 A pool internally allocates objects in continuous blocks of memory. Once a
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1313 block is full, a new one is allocated and added to the pool memory
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1314 block list. When a large allocation is requested which does not fit into
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1315 a block, such allocation is forwarded to the system allocator and the
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1316 returned pointer is stored in the pool for further deallocation.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1317 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1318
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1319 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1320 Nginx pool has the type <literal>ngx_pool_t</literal>.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1321 The following operations are supported:
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1322 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1323
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1324 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1325 <list type="bullet">
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1326
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1327 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
1328 <literal>ngx_create_pool(size, log)</literal> — create a pool with given
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1329 block size. The pool object returned is allocated in the pool as well.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1330 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1331
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1332 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
1333 <literal>ngx_destroy_pool(pool)</literal> — free all pool memory, including
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1334 the pool object itself.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1335 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1336
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1337 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
1338 <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
1339 </listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
1340
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
1341 <listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
1342 <literal>ngx_pcalloc(pool, size)</literal> — allocated aligned memory
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1343 from pool and fill it with zeroes
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1344 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1345
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1346 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
1347 <literal>ngx_pnalloc(pool, size)</literal> — allocate unaligned memory from pool.
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1348 Mostly used for allocating strings
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1349 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1350
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1351 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
1352 <literal>ngx_pfree(pool, p)</literal> — free memory, previously allocated
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1353 in the pool.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1354 Only allocations, forwarded to the system allocator, can be freed.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1355 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1356
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1357 </list>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1358 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1359
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1360 <programlisting>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1361 u_char *p;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1362 ngx_str_t *s;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1363 ngx_pool_t *pool;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1364
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1365 pool = ngx_create_pool(1024, log);
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1366 if (pool == NULL) { /* error */ }
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1367
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1368 s = ngx_palloc(pool, sizeof(ngx_str_t));
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1369 if (s == NULL) { /* error */ }
1936
8f996938fe23 Style: proper quotes usage.
Vladimir Homutov <vl@nginx.com>
parents: 1935
diff changeset
1370 ngx_str_set(s, "foo");
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1371
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1372 p = ngx_pnalloc(pool, 3);
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1373 if (p == NULL) { /* error */ }
1936
8f996938fe23 Style: proper quotes usage.
Vladimir Homutov <vl@nginx.com>
parents: 1935
diff changeset
1374 ngx_memcpy(p, "foo", 3);
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1375 </programlisting>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1376
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1377 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1378 Since chain links <literal>ngx_chain_t</literal> are actively used in nginx,
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1379 nginx pool provides a way to reuse them.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1380 The <literal>chain</literal> field of <literal>ngx_pool_t</literal> keeps a
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1381 list of previously allocated links ready for reuse. For efficient allocation of
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1382 a chain link in a pool, the function
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1383 <literal>ngx_alloc_chain_link(pool)</literal> should be used.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1384 This function looks up a free chain link in the pool list and only if it's
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1385 empty allocates a new one. To free a link <literal>ngx_free_chain(pool, cl)</literal>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1386 should be called.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1387 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1388
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1389 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1390 Cleanup handlers can be registered in a pool.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1391 Cleanup handler is a callback with an argument which is called when pool is
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1392 destroyed.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1393 Pool is usually tied with a specific nginx object (like HTTP request) and
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1394 destroyed in the end of that object’s lifetime, releasing the object itself.
1902
862e96a39fe3 Typo fixed.
Maxim Konovalov <maxim@nginx.com>
parents: 1899
diff changeset
1395 Registering a pool cleanup is a convenient way to release resources, close file
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1396 descriptors or make final adjustments to shared data, associated with the main
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1397 object.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1398 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1399
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1400 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1401 A pool cleanup is registered by calling <literal>ngx_pool_cleanup_add(pool,
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1402 size)</literal> which returns <literal>ngx_pool_cleanup_t</literal> pointer to
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1403 be filled by the caller. The <literal>size</literal> argument allows allocating
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1404 context for the cleanup handler.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1405 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1406
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1407
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1408 <programlisting>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1409 ngx_pool_cleanup_t *cln;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1410
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1411 cln = ngx_pool_cleanup_add(pool, 0);
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1412 if (cln == NULL) { /* error */ }
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1413
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1414 cln->handler = ngx_my_cleanup;
1936
8f996938fe23 Style: proper quotes usage.
Vladimir Homutov <vl@nginx.com>
parents: 1935
diff changeset
1415 cln->data = "foo";
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1416
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1417 ...
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1418
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1419 static void
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1420 ngx_my_cleanup(void *data)
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1421 {
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1422 u_char *msg = data;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1423
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1424 ngx_do_smth(msg);
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1425 }
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1426 </programlisting>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1427
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1428 </section>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1429
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1430
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1431 <section name="Shared memory" id="shared_memory">
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1432
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1433 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1434 Shared memory is used by nginx to share common data between processes.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1435 Function <literal>ngx_shared_memory_add(cf, name, size, tag)</literal> adds a
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1436 new shared memory entry <literal>ngx_shm_zone_t</literal> to the cycle. The
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1437 function receives <literal>name</literal> and <literal>size</literal> of the
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1438 zone.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1439 Each shared zone must have a unique name.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1440 If a shared zone entry with the provided name exists, the old zone entry is
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1441 reused, if its tag value matches too.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1442 Mismatched tag is considered an error.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1443 Usually, the address of the module structure is passed as tag, making it
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1444 possible to reuse shared zones by name within one nginx module.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1445 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1446
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1447 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1448 The shared memory entry structure <literal>ngx_shm_zone_t</literal> has the
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1449 following fields:
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1450 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1451
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1452 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1453 <list type="bullet">
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1454
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1455 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
1456 <literal>init</literal> — initialization callback, called after shared zone is
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1457 mapped to actual memory
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1458 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1459
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1460 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
1461 <literal>data</literal> — data context, used to pass arbitrary data to the
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1462 <literal>init</literal> callback
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1463 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1464
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1465 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
1466 <literal>noreuse</literal> — flag, disabling shared zone reuse from the
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1467 old cycle
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1468 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1469
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1470 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
1471 <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
1472 </listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
1473
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
1474 <listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
1475 <literal>shm</literal> — platform-specific object of type
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1476 <literal>ngx_shm_t</literal>, having at least the following fields:
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1477 <list type="bullet">
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1478
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1479 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
1480 <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
1481 </listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
1482
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
1483 <listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
1484 <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
1485 </listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
1486
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
1487 <listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
1488 <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
1489 </listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
1490
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
1491 <listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
1492 <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
1493 </listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
1494
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
1495 <listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
1496 <literal>exists</literal> — flag, showing that shared memory was inherited
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1497 from the master process (Windows-specific)
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1498 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1499
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1500 </list>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1501 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1502
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1503 </list>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1504 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1505
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1506 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1507 Shared zone entries are mapped to actual memory in
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1508 <literal>ngx_init_cycle()</literal> after configuration is parsed.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1509 On POSIX systems, <literal>mmap()</literal> syscall is used to create shared
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1510 anonymous mapping.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1511 On Windows, <literal>CreateFileMapping()/MapViewOfFileEx()</literal> pair is
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1512 used.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1513 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1514
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1515 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1516 For allocating in shared memory, nginx provides slab pool
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1517 <literal>ngx_slab_pool_t</literal>.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1518 In each nginx shared zone, a slab pool is automatically created for allocating
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1519 memory in that zone.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1520 The pool is located in the beginning of the shared zone and can be accessed by
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1521 the expression <literal>(ngx_slab_pool_t *) shm_zone->shm.addr</literal>.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1522 Allocation in shared zone is done by calling one of the functions
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1523 <literal>ngx_slab_alloc(pool, size)/ngx_slab_calloc(pool, size)</literal>.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1524 Memory is freed by calling <literal>ngx_slab_free(pool, p)</literal>.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1525 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1526
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1527 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1528 Slab pool divides all shared zone into pages.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1529 Each page is used for allocating objects of the same size.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1530 Only the sizes which are powers of 2, and not less than 8, are considered.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1531 Other sizes are rounded up to one of these values.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1532 For each page, a bitmask is kept, showing which blocks within that page are in
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1533 use and which are free for allocation.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1534 For sizes greater than half-page (usually, 2048 bytes), allocation is done by
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1535 entire pages.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1536 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1537
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1538 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1539 To protect data in shared memory from concurrent access, mutex is available in
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1540 the <literal>mutex</literal> field of <literal>ngx_slab_pool_t</literal>.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1541 The mutex is used by the slab pool while allocating and freeing memory.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1542 However, it can be used to protect any other user data structures,
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1543 allocated in the shared zone.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1544 Locking is done by calling
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1545 <literal>ngx_shmtx_lock(&amp;shpool->mutex)</literal>, unlocking is done by
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1546 calling <literal>ngx_shmtx_unlock(&amp;shpool->mutex)</literal>.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1547 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1548
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1549
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1550 <programlisting>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1551 ngx_str_t name;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1552 ngx_foo_ctx_t *ctx;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1553 ngx_shm_zone_t *shm_zone;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1554
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1555 ngx_str_set(&amp;name, "foo");
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1556
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1557 /* allocate shared zone context */
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1558 ctx = ngx_pcalloc(cf->pool, sizeof(ngx_foo_ctx_t));
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1559 if (ctx == NULL) {
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1560 /* error */
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1561 }
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1562
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1563 /* add an entry for 65k shared zone */
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1564 shm_zone = ngx_shared_memory_add(cf, &amp;name, 65536, &amp;ngx_foo_module);
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1565 if (shm_zone == NULL) {
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1566 /* error */
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1567 }
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1568
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1569 /* register init callback and context */
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1570 shm_zone->init = ngx_foo_init_zone;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1571 shm_zone->data = ctx;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1572
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1573
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1574 ...
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1575
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1576
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1577 static ngx_int_t
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1578 ngx_foo_init_zone(ngx_shm_zone_t *shm_zone, void *data)
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1579 {
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1580 ngx_foo_ctx_t *octx = data;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1581
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1582 size_t len;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1583 ngx_foo_ctx_t *ctx;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1584 ngx_slab_pool_t *shpool;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1585
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1586 value = shm_zone->data;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1587
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1588 if (octx) {
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1589 /* reusing a shared zone from old cycle */
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1590 ctx->value = octx->value;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1591 return NGX_OK;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1592 }
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1593
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1594 shpool = (ngx_slab_pool_t *) shm_zone->shm.addr;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1595
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1596 if (shm_zone->shm.exists) {
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1597 /* initialize shared zone context in Windows nginx worker */
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1598 ctx->value = shpool->data;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1599 return NGX_OK;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1600 }
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1601
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1602 /* initialize shared zone */
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1603
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1604 ctx->value = ngx_slab_alloc(shpool, sizeof(ngx_uint_t));
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1605 if (ctx->value == NULL) {
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1606 return NGX_ERROR;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1607 }
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1608
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1609 shpool->data = ctx->value;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1610
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1611 return NGX_OK;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1612 }
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1613 </programlisting>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1614
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1615 </section>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1616
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1617
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1618 </section>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1619
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1620
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1621 <section name="Logging" id="logging">
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1622
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1623 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1624 For logging nginx code uses <literal>ngx_log_t</literal> objects.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1625 Nginx logger provides support for several types of output:
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1626
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1627 <list type="bullet">
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1628
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1629 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
1630 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
1631 </listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
1632
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
1633 <listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
1634 file — logging to file
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
1635 </listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
1636
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
1637 <listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
1638 syslog — logging to syslog
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
1639 </listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
1640
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
1641 <listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
1642 memory — logging to internal memory storage for development purposes.
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1643 The memory could be accessed later with debugger
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1644 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1645
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1646 </list>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1647 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1648
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1649 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1650 A logger instance may actually be a chain of loggers, linked to each other with
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1651 the <literal>next</literal> field.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1652 Each message is written to all loggers in chain.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1653 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1654
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1655 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1656 Each logger has an error level which limits the messages written to that log.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1657 The following error levels are supported by nginx:
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1658 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1659
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1660 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1661 <list type="bullet">
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1662
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1663 <listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1664 <literal>NGX_LOG_EMERG</literal>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1665 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1666
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1667 <listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1668 <literal>NGX_LOG_ALERT</literal>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1669 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1670
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1671 <listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1672 <literal>NGX_LOG_CRIT</literal>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1673 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1674
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1675 <listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1676 <literal>NGX_LOG_ERR</literal>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1677 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1678
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1679 <listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1680 <literal>NGX_LOG_WARN</literal>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1681 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1682
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1683 <listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1684 <literal>NGX_LOG_NOTICE</literal>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1685 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1686
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1687 <listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1688 <literal>NGX_LOG_INFO</literal>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1689 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1690
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1691 <listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1692 <literal>NGX_LOG_DEBUG</literal>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1693 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1694
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1695 </list>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1696 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1697
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1698 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1699 For debug logging, debug mask is checked as well. The following debug masks
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1700 exist:
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1701 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1702
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1703 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1704 <list type="bullet">
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1705
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1706 <listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1707 <literal>NGX_LOG_DEBUG_CORE</literal>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1708 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1709
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1710 <listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1711 <literal>NGX_LOG_DEBUG_ALLOC</literal>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1712 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1713
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1714 <listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1715 <literal>NGX_LOG_DEBUG_MUTEX</literal>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1716 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1717
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1718 <listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1719 <literal>NGX_LOG_DEBUG_EVENT</literal>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1720 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1721
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1722 <listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1723 <literal>NGX_LOG_DEBUG_HTTP</literal>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1724 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1725
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1726 <listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1727 <literal>NGX_LOG_DEBUG_MAIL</literal>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1728 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1729
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1730 <listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1731 <literal>NGX_LOG_DEBUG_STREAM</literal>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1732 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1733
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1734 </list>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1735 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1736
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1737 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1738 Normally, loggers are created by existing nginx code from
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1739 <literal>error_log</literal> directives and are available at nearly every stage
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1740 of processing in cycle, configuration, client connection and other objects.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1741 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1742
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1743 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1744 Nginx provides the following logging macros:
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1745 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1746
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1747 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1748 <list type="bullet">
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1749
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1750 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
1751 <literal>ngx_log_error(level, log, err, fmt, ...)</literal> — error logging
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1752 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1753
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1754 <listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1755 <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
1756 <literal>ngx_log_debug1(level, log, err, fmt, arg1)</literal> etc — debug
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1757 logging, up to 8 formatting arguments are supported
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1758 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1759
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1760 </list>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1761 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1762
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1763 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1764 A log message is formatted in a buffer of size
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1765 <literal>NGX_MAX_ERROR_STR</literal> (currently, 2048 bytes) on stack.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1766 The message is prepended with error level, process PID, connection id (stored
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1767 in <literal>log->connection</literal>) and system error text.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1768 For non-debug messages <literal>log->handler</literal> is called as well to
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1769 prepend more specific information to the log message.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1770 HTTP module sets <literal>ngx_http_log_error()</literal> function as log
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1771 handler to log client and server addresses, current action (stored in
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1772 <literal>log->action</literal>), client request line, server name etc.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1773 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1774
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1775 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1776 Example:
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1777 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1778
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1779
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1780 <programlisting>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1781 /* specify what is currently done */
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1782 log->action = "sending mp4 to client”;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1783
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1784 /* error and debug log */
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1785 ngx_log_error(NGX_LOG_INFO, c->log, 0, "client prematurely
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1786 closed connection”);
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1787
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1788 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, mp4->file.log, 0,
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1789 "mp4 start:%ui, length:%ui”, mp4->start, mp4->length);
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1790 </programlisting>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1791
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1792 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1793 Logging result:
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1794 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1795
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1796
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1797 <programlisting>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1798 2016/09/16 22:08:52 [info] 17445#0: *1 client prematurely closed connection while
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1799 sending mp4 to client, client: 127.0.0.1, server: , request: "GET /file.mp4 HTTP/1.1”
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1800 2016/09/16 23:28:33 [debug] 22140#0: *1 mp4 start:0, length:10000
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1801 </programlisting>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1802
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1803 </section>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1804
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1805
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1806 <section name="Cycle" id="cycle">
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1807
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1808 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1809 Cycle object keeps nginx runtime context, created from a specific
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1810 configuration.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1811 The type of the cycle is <literal>ngx_cycle_t</literal>.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1812 Upon configuration reload a new cycle is created from the new version of nginx
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1813 configuration.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1814 The old cycle is usually deleted after a new one is successfully created.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1815 Currently active cycle is held in the <literal>ngx_cycle</literal> global
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1816 variable and is inherited by newly started nginx workers.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1817 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1818
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1819 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1820 A cycle is created by the function <literal>ngx_init_cycle()</literal>.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1821 The function receives the old cycle as the argument.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1822 It's used to locate the configuration file and inherit as much resources as
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1823 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
1824 When nginx starts, a fake cycle called “init cycle” is created and is then
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1825 replaced by a normal cycle, built from configuration.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1826 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1827
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1828 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1829 Some members of the cycle:
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1830 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1831
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1832 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1833 <list type="bullet">
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1834
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1835 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
1836 <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
1837 </listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
1838
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
1839 <listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
1840 <literal>log</literal> — cycle log. Initially, this log is inherited from the
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1841 old cycle.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1842 After reading configuration, this member is set to point to
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1843 <literal>new_log</literal>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1844 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1845
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1846 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
1847 <literal>new_log</literal> — cycle log, created by the configuration.
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1848 It's affected by the root scope <literal>error_log</literal> directive
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1849 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1850
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1851 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
1852 <literal>connections</literal>, <literal>connections_n</literal> — per-worker
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1853 array of connections of type <literal>ngx_connection_t</literal>, created by
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1854 the event module while initializing each nginx worker.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1855 The number of connections is set by the <literal>worker_connections</literal>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1856 directive
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1857 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1858
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1859 <listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1860 <literal>free_connections</literal>,
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
1861 <literal>free_connections_n</literal> — the and number of currently available
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1862 connections.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1863 If no connections are available, nginx worker refuses to accept new clients
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1864 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1865
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1866 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
1867 <literal>files</literal>, <literal>files_n</literal> — array for mapping file
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1868 descriptors to nginx connections.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1869 This mapping is used by the event modules, having the
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1870 <literal>NGX_USE_FD_EVENT</literal> flag (currently, it's poll and devpoll)
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1871 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1872
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1873 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
1874 <literal>conf_ctx</literal> — array of core module configurations.
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1875 The configurations are created and filled while reading nginx configuration
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1876 files
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1877 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1878
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1879 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
1880 <literal>modules</literal>, <literal>modules_n</literal> — array of modules
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1881 <literal>ngx_module_t</literal>, both static and dynamic, loaded by current
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1882 configuration
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1883 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1884
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1885 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
1886 <literal>listening</literal> — array of listening objects
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1887 <literal>ngx_listening_t</literal>.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1888 Listening objects are normally added by the the <literal>listen</literal>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1889 directive of different modules which call the
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1890 <literal>ngx_create_listening()</literal> function.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1891 Based on listening objects, listen sockets are created by nginx
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1892 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1893
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1894 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
1895 <literal>paths</literal> — array of paths <literal>ngx_path_t</literal>.
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1896 Paths are added by calling the function <literal>ngx_add_path()</literal> from
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1897 modules which are going to operate on certain directories.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1898 These directories are created by nginx after reading configuration, if missing.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1899 Moreover, two handlers can be added for each path:
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1900
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1901 <list type="bullet">
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1902
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1903 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
1904 path loader — executed only once in 60 seconds after starting or reloading
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1905 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
1906 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
1907 cache loader”
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1908 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1909
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1910 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
1911 path manager — executed periodically. Normally, removes old files from the
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1912 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
1913 called from a dedicated nginx process “nginx cache manager”
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1914 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1915
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1916 </list>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1917 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1918
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1919 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
1920 <literal>open_files</literal> — list of <literal>ngx_open_file_t</literal>
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1921 objects.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1922 An open file object is created by calling the function
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1923 <literal>ngx_conf_open_file()</literal>.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1924 After reading configuration nginx opens all files from the
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1925 <literal>open_files</literal> list and stores file descriptors in the
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1926 <literal>fd</literal> field of each open file object.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1927 The files are opened in append mode and created if missing.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1928 The files from this list are reopened by nginx workers upon receiving the
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1929 reopen signal (usually it's <literal>USR1</literal>).
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1930 In this case the <literal>fd</literal> fields are changed to new descriptors.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1931 The open files are currently used for logging
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1932 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1933
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1934 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
1935 <literal>shared_memory</literal> — list of shared memory zones, each added by
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1936 calling the <literal>ngx_shared_memory_add()</literal> function.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1937 Shared zones are mapped to the same address range in all nginx processes and
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1938 are used to share common data, for example HTTP cache in-memory tree
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1939 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1940
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1941 </list>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1942 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1943
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1944 </section>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1945
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1946 <section name="Buffer" id="buffer">
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1947
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1948 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1949 For input/output operations, nginx provides the buffer type
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1950 <literal>ngx_buf_t</literal>.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1951 Normally, it's used to hold data to be written to a destination or read from a
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1952 source.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1953 Buffer can reference data in memory and in file.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1954 Technically it's possible that a buffer references both at the same time.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1955 Memory for the buffer is allocated separately and is not related to the buffer
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1956 structure <literal>ngx_buf_t</literal>.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1957 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1958
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1959 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1960 The structure <literal>ngx_buf_t</literal> has the following fields:
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1961 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1962
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1963 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1964 <list type="bullet">
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1965
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1966 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
1967 <literal>start</literal>, <literal>end</literal> — the boundaries of memory
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1968 block, allocated for the buffer
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1969 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1970
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1971 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
1972 <literal>pos</literal>, <literal>last</literal> — memory buffer boundaries,
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1973 normally a subrange of <literal>start</literal> .. <literal>end</literal>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1974 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1975
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
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>file_pos</literal>, <literal>file_last</literal> — file buffer
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1978 boundaries, these are offsets from the beginning of the file
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1979 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1980
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1981 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
1982 <literal>tag</literal> — unique value, used to distinguish buffers, created by
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1983 different nginx module, usually, for the purpose of buffer reuse
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1984 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1985
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1986 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
1987 <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
1988 </listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
1989
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
1990 <listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
1991 <literal>temporary</literal> — flag, meaning that the buffer references
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1992 writable memory
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1993 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1994
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1995 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
1996 <literal>memory</literal> — flag, meaning that the buffer references read-only
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1997 memory
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1998 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
1999
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2000 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
2001 <literal>in_file</literal> — flag, meaning that current buffer references data
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2002 in a file
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2003 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2004
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2005 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
2006 <literal>flush</literal> — flag, meaning that all data prior to this buffer
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2007 should be flushed
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2008 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2009
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2010 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
2011 <literal>recycled</literal> — flag, meaning that the buffer can be reused and
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2012 should be consumed as soon as possible
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2013 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2014
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2015 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
2016 <literal>sync</literal> — flag, meaning that the buffer carries no data or
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2017 special signal like <literal>flush</literal> or <literal>last_buf</literal>.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2018 Normally, such buffers are considered an error by nginx. This flags allows
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2019 skipping the error checks
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2020 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2021
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
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>last_buf</literal> — flag, meaning that current buffer is the last in
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2024 output
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2025 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2026
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2027 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
2028 <literal>last_in_chain</literal> — flag, meaning that there's no more data
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2029 buffers in a (sub)request
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2030 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2031
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2032 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
2033 <literal>shadow</literal> — reference to another buffer, related to the current
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2034 buffer. Usually current buffer uses data from the shadow buffer. Once current
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2035 buffer is consumed, the shadow buffer should normally also be marked as
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2036 consumed
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2037 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2038
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2039 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
2040 <literal>last_shadow</literal> — flag, meaning that current buffer is the last
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2041 buffer, referencing a particular shadow buffer
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2042 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2043
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2044 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
2045 <literal>temp_file</literal> — flag, meaning that the buffer is in a temporary
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2046 file
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2047 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2048
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2049 </list>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2050 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2051
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2052 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2053 For input and output buffers are linked in chains.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2054 Chain is a sequence of chain links <literal>ngx_chain_t</literal>, defined as
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2055 follows:
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2056 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2057
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2058
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2059 <programlisting>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2060 typedef struct ngx_chain_s ngx_chain_t;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2061
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2062 struct ngx_chain_s {
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2063 ngx_buf_t *buf;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2064 ngx_chain_t *next;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2065 };
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2066 </programlisting>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2067
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2068 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2069 Each chain link keeps a reference to its buffer and a reference to the next
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2070 chain link.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2071 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2072
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2073 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2074 Example of using buffers and chains:
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2075 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2076
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2077
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2078 <programlisting>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2079 ngx_chain_t *
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2080 ngx_get_my_chain(ngx_pool_t *pool)
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2081 {
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2082 ngx_buf_t *b;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2083 ngx_chain_t *out, *cl, **ll;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2084
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2085 /* first buf */
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2086 cl = ngx_alloc_chain_link(pool);
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2087 if (cl == NULL) { /* error */ }
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2088
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2089 b = ngx_calloc_buf(pool);
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2090 if (b == NULL) { /* error */ }
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2091
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2092 b->start = (u_char *) "foo";
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2093 b->pos = b->start;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2094 b->end = b->start + 3;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2095 b->last = b->end;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2096 b->memory = 1; /* read-only memory */
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2097
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2098 cl->buf = b;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2099 out = cl;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2100 ll = &amp;cl->next;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2101
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2102 /* second buf */
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2103 cl = ngx_alloc_chain_link(pool);
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2104 if (cl == NULL) { /* error */ }
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2105
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2106 b = ngx_create_temp_buf(pool, 3);
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2107 if (b == NULL) { /* error */ }
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2108
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2109 b->last = ngx_cpymem(b->last, "foo", 3);
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2110
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2111 cl->buf = b;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2112 cl->next = NULL;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2113 *ll = cl;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2114
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2115 return out;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2116 }
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2117 </programlisting>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2118
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2119 </section>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2120
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2121
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2122 <section name="Networking" id="networking">
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2123
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2124
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2125 <!--
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2126 <section name="Network data types" id="network_data_types">
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2127
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2128 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2129 TBD: ngx_addr_t, ngx_url_t, ngx_socket_t, ngx_sockaddr_t, parse url, parse
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2130 address...
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2131 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2132
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2133 </section>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2134 -->
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2135
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2136 <section name="Connection" id="connection">
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2137
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2138 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2139 Connection type <literal>ngx_connection_t</literal> is a wrapper around a
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2140 socket descriptor. Some of the structure fields are:
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2141 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2142
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2143 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2144 <list type="bullet">
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2145
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2146 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
2147 <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
2148 </listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
2149
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
2150 <listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
2151 <literal>data</literal> — arbitrary connection context.
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2152 Normally, a pointer to a higher level object, built on top of the connection,
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2153 like HTTP request or Stream session
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2154 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2155
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2156 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
2157 <literal>read</literal>, <literal>write</literal> — read and write events for
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2158 the connection
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2159 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2160
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2161 <listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2162 <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
2163 <literal>recv_chain</literal>, <literal>send_chain</literal> — I/O operations
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2164 for the connection
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2165 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2166
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2167 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
2168 <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
2169 </listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
2170
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
2171 <listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
2172 <literal>log</literal> — connection log
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2173 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2174
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2175 <listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2176 <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
2177 <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
2178 </listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
2179
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
2180 <listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
2181 <literal>local_sockaddr</literal>, <literal>local_socklen</literal> — local
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2182 socket address in binary form.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2183 Initially, these fields are empty.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2184 Function <literal>ngx_connection_local_sockaddr()</literal> should be used to
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2185 get socket local address
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2186 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2187
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2188 <listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2189 <literal>proxy_protocol_addr</literal>, <literal>proxy_protocol_port</literal>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2190 - PROXY protocol client address and port, if PROXY protocol is enabled for the
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2191 connection
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2192 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2193
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2194 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
2195 <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
2196 </listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
2197
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
2198 <listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
2199 <literal>reusable</literal> — flag, meaning, that the connection is at the
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2200 state, when it can be reused
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2201 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2202
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2203 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
2204 <literal>close</literal> — flag, meaning, that the connection is being reused
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2205 and should be closed
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2206 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2207
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2208 </list>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2209 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2210
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2211 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2212 An nginx connection can transparently encapsulate SSL layer.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2213 In this case the connection <literal>ssl</literal> field holds a pointer to an
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2214 <literal>ngx_ssl_connection_t</literal> structure, keeping all SSL-related data
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2215 for the connection, including <literal>SSL_CTX</literal> and
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2216 <literal>SSL</literal>.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2217 The handlers <literal>recv</literal>, <literal>send</literal>,
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2218 <literal>recv_chain</literal>, <literal>send_chain</literal> are set as well to
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2219 SSL functions.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2220 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2221
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2222 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2223 The number of connections per nginx worker is limited by the
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2224 <literal>worker_connections</literal> value.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2225 All connection structures are pre-created when a worker starts and stored in
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2226 the <literal>connections</literal> field of the cycle object.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2227 To reach out for a connection structure, <literal>ngx_get_connection(s,
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2228 log)</literal> function is used.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2229 The function receives a socket descriptor <literal>s</literal> which needs to
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2230 be wrapped in a connection structure.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2231 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2232
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2233 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2234 Since the number of connections per worker is limited, nginx provides a
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2235 way to grab connections which are currently in use.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2236 To enable or disable reuse of a connection, function
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2237 <literal>ngx_reusable_connection(c, reusable)</literal> is called.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2238 Calling <literal>ngx_reusable_connection(c, 1)</literal> sets the
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2239 <literal>reuse</literal> flag of the connection structure and inserts the
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2240 connection in the <literal>reusable_connections_queue</literal> of the cycle.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2241 Whenever <literal>ngx_get_connection()</literal> finds out there are no
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2242 available connections in the <literal>free_connections</literal> list of the
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2243 cycle, it calls <literal>ngx_drain_connections()</literal> to release a
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2244 specific number of reusable connections.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2245 For each such connection, the <literal>close</literal> flag is set and its read
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2246 handler is called which is supposed to free the connection by calling
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2247 <literal>ngx_close_connection(c)</literal> and make it available for reuse.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2248 To exit the state when a connection can be reused
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2249 <literal>ngx_reusable_connection(c, 0)</literal> is called.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2250 An example of reusable connections in nginx is HTTP client connections which
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2251 are marked as reusable until some data is received from the client.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2252 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2253
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2254 </section>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2255
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2256
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2257 </section>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2258
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2259
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2260 <section name="Events" id="events">
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2261
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2262
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2263 <section name="Event" id="event">
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2264
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2265 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2266 Event object <literal>ngx_event_t</literal> in nginx provides a way to be
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2267 notified of a specific event happening.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2268 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2269
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2270 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2271 Some of the fields of the <literal>ngx_event_t</literal> are:
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2272 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2273
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2274 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2275 <list type="bullet">
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2276
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2277 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
2278 <literal>data</literal> — arbitrary event context, used in event handler,
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2279 usually, a pointer to a connection, tied with the event
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2280 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2281
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2282 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
2283 <literal>handler</literal> — callback function to be invoked when the event
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2284 happens
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2285 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2286
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2287 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
2288 <literal>write</literal> — flag, meaning that this is the write event.
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2289 Used to distinguish between read and write events
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2290 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2291
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2292 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
2293 <literal>active</literal> — flag, meaning that the event is registered for
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2294 receiving I/O notifications, normally from notification mechanisms like epoll,
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2295 kqueue, poll
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2296 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2297
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2298 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
2299 <literal>ready</literal> — flag, meaning that the event has received an
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2300 I/O notification
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2301 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2302
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2303 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
2304 <literal>delayed</literal> — flag, meaning that I/O is delayed due to rate
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2305 limiting
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2306 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2307
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2308 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
2309 <literal>timer</literal> — Red-Black tree node for inserting the event into
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2310 the timer tree
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2311 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2312
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2313 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
2314 <literal>timer_set</literal> — flag, meaning that the event timer is set,
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2315 but not yet expired
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2316 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2317
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2318 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
2319 <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
2320 </listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
2321
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
2322 <listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
2323 <literal>eof</literal> — read event flag, meaning that the eof has happened
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2324 while reading data
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2325 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2326
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2327 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
2328 <literal>pending_eof</literal> — flag, meaning that the eof is pending on the
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2329 socket, even though there may be some data available before it.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2330 The flag is delivered via <literal>EPOLLRDHUP</literal> epoll event or
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2331 <literal>EV_EOF</literal> kqueue flag
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2332 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2333
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2334 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
2335 <literal>error</literal> — flag, meaning that an error has happened while
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2336 reading (for read event) or writing (for write event)
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2337 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2338
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2339 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
2340 <literal>cancelable</literal> — timer event flag, meaning that the event
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2341 handler should be called while performing nginx worker graceful shutdown, event
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2342 though event timeout has not yet expired. The flag provides a way to finalize
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2343 certain activities, for example, flush log files
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2344 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2345
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2346 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
2347 <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
2348 </listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
2349
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
2350 <listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
2351 <literal>queue</literal> — queue node for posting the event to a queue
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2352 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2353
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2354 </list>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2355 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2356
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2357 </section>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2358
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2359
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2360 <section name="I/O events" id="i_o_events">
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2361
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2362 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2363 Each connection, received with the
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2364 <literal>ngx_get_connection()</literal> call, has two events attached to it:
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2365 <literal>c->read</literal> and <literal>c->write</literal>.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2366 These events are used to receive notifications about the socket being ready for
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2367 reading or writing.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2368 All such events operate in Edge-Triggered mode, meaning that they only trigger
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2369 notifications when the state of the socket changes.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2370 For example, doing a partial read on a socket will not make nginx deliver a
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2371 repeated read notification until more data arrive in the socket.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2372 Even when the underlying I/O notification mechanism is essentially
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2373 Level-Triggered (poll, select etc), nginx will turn the notifications into
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2374 Edge-Triggered.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2375 To make nginx event notifications consistent across all notifications systems
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2376 on different platforms, it's required, that the functions
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2377 <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
2378 <literal>ngx_handle_write_event(wev, lowat)</literal> are called after handling
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2379 an I/O socket notification or calling any I/O functions on that socket.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2380 Normally, these functions are called once in the end of each read or write
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2381 event handler.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2382 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2383
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2384 </section>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2385
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2386
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2387 <section name="Timer events" id="timer_events">
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2388
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2389 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2390 An event can be set to notify a timeout expiration.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2391 The function <literal>ngx_add_timer(ev, timer)</literal> sets a timeout for an
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2392 event, <literal>ngx_del_timer(ev)</literal> deletes a previously set timeout.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2393 Timeouts currently set for all existing events, are kept in a global timeout
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2394 Red-Black tree <literal>ngx_event_timer_rbtree</literal>.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2395 The key in that tree has the type <literal>ngx_msec_t</literal> and is the time
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2396 in milliseconds since the beginning of January 1, 1970 (modulus
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2397 <literal>ngx_msec_t</literal> max value) at which the event should expire.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2398 The tree structure provides fast inserting and deleting operations, as well as
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2399 accessing the nearest timeouts.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2400 The latter is used by nginx to find out for how long to wait for I/O events
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2401 and for expiring timeout events afterwards.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2402 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2403
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2404 </section>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2405
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2406
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2407 <section name="Posted events" id="posted_events">
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2408
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2409 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2410 An event can be posted which means that its handler will be called at some
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2411 point later within the current event loop iteration.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2412 Posting events is a good practice for simplifying code and escaping stack
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2413 overflows.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2414 Posted events are held in a post queue.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2415 The macro <literal>ngx_post_event(ev, q)</literal> posts the event
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2416 <literal>ev</literal> to the post queue <literal>q</literal>.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2417 Macro <literal>ngx_delete_posted_event(ev)</literal> deletes the event
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2418 <literal>ev</literal> from whatever queue it's currently posted.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2419 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
2420 This queue is processed late in the event loop — after all I/O and timer
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2421 events are already handled.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2422 The function <literal>ngx_event_process_posted()</literal> is called to process
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2423 an event queue.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2424 This function calls event handlers until the queue is not empty. This means
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2425 that a posted event handler can post more events to be processed within the
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2426 current event loop iteration.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2427 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2428
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2429 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2430 Example:
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2431 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2432
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2433
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2434 <programlisting>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2435 void
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2436 ngx_my_connection_read(ngx_connection_t *c)
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2437 {
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2438 ngx_event_t *rev;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2439
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2440 rev = c->read;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2441
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2442 ngx_add_timer(rev, 1000);
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2443
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2444 rev->handler = ngx_my_read_handler;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2445
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2446 ngx_my_read(rev);
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2447 }
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2448
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2449
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2450 void
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2451 ngx_my_read_handler(ngx_event_t *rev)
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2452 {
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2453 ssize_t n;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2454 ngx_connection_t *c;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2455 u_char buf[256];
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2456
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2457 if (rev->timedout) { /* timeout expired */ }
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2458
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2459 c = rev->data;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2460
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2461 while (rev->ready) {
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2462 n = c->recv(c, buf, sizeof(buf));
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2463
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2464 if (n == NGX_AGAIN) {
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2465 break;
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2466 }
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2467
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2468 if (n == NGX_ERROR) { /* error */ }
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2469
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2470 /* process buf */
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2471 }
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2472
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2473 if (ngx_handle_read_event(rev, 0) != NGX_OK) { /* error */ }
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2474 }
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2475 </programlisting>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2476
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2477 </section>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2478
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2479
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2480 <section name="Event loop" id="event_loop">
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2481
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2482 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2483 All nginx processes which do I/O, have an event loop.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2484 The only type of process which does not have I/O, is nginx master process which
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2485 spends most of its time in <literal>sigsuspend()</literal> call waiting for
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2486 signals to arrive.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2487 Event loop is implemented in <literal>ngx_process_events_and_timers()</literal>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2488 function.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2489 This function is called repeatedly until the process exits.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2490 It has the following stages:
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2491 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2492
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2493 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2494 <list type="bullet">
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2495
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2496 <listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2497 find nearest timeout by calling <literal>ngx_event_find_timer()</literal>.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2498 This function finds the leftmost timer tree node and returns the number of
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2499 milliseconds until that node expires
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2500 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2501
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2502 <listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2503 process I/O events by calling a handler, specific to event notification
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2504 mechanism, chosen by nginx configuration.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2505 This handler waits for at least one I/O event to happen, but no longer, than
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2506 the nearest timeout.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2507 For each read or write event which has happened, the <literal>ready</literal>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2508 flag is set and its handler is called.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2509 For Linux, normally, the <literal>ngx_epoll_process_events()</literal> handler
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2510 is used which calls <literal>epoll_wait()</literal> to wait for I/O events
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2511 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2512
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2513 <listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2514 expire timers by calling <literal>ngx_event_expire_timers()</literal>.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2515 The timer tree is iterated from the leftmost element to the right until a not
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2516 yet expired timeout is found.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2517 For each expired node the <literal>timedout</literal> event flag is set,
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2518 <literal>timer_set</literal> flag is reset, and the event handler is called
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2519 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2520
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2521 <listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2522 process posted events by calling <literal>ngx_event_process_posted()</literal>.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2523 The function repeatedly removes the first element from the posted events
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2524 queue and calls its handler until the queue gets empty
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2525 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2526
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2527 </list>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2528 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2529
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2530 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2531 All nginx processes handle signals as well.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2532 Signal handlers only set global variables which are checked after the
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2533 <literal>ngx_process_events_and_timers()</literal> call.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2534 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2535
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2536 </section>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2537
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2538
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2539 </section>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2540
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2541
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2542 <section name="Processes" id="processes">
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2543
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2544 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2545 There are several types of processes in nginx.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2546 The type of current process is kept in the <literal>ngx_process</literal>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2547 global variable:
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2548 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2549
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2550 <list type="bullet">
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2551
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2552 <listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2553
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2554 <para>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
2555 <literal>NGX_PROCESS_MASTER</literal> — the master process runs the
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2556 <literal>ngx_master_process_cycle()</literal> function.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2557 Master process does not have any I/O and responds only to signals.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2558 It reads configuration, creates cycles, starts and controls child processes
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2559 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2560
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2561
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2562 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2563
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2564 <listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2565
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2566 <para>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
2567 <literal>NGX_PROCESS_WORKER</literal> — the worker process runs the
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2568 <literal>ngx_worker_process_cycle()</literal> function.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2569 Worker processes are started by master and handle client connections.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2570 They also respond to signals and channel commands, sent from master
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2571 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2572
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2573
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2574 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2575
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2576 <listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2577
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2578 <para>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
2579 <literal>NGX_PROCESS_SINGLE</literal> — single process is the only type
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2580 of processes which exist in the <literal>master_process off</literal> mode.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2581 The cycle function for this process is
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2582 <literal>ngx_single_process_cycle()</literal>.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2583 This process creates cycles and handles client connections
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2584 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2585
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2586
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2587 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2588
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2589 <listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2590
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2591 <para>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
2592 <literal>NGX_PROCESS_HELPER</literal> — currently, there are two types of
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2593 helper processes: cache manager and cache loader.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2594 Both of them share the same cycle function
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2595 <literal>ngx_cache_manager_process_cycle()</literal>.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2596 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2597
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2598
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2599 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2600
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2601 </list>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2602
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2603 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2604 All nginx processes handle the following signals:
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2605 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2606
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2607 <list type="bullet">
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2608
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2609 <listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2610
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2611 <para>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
2612 <literal>NGX_SHUTDOWN_SIGNAL</literal> (<literal>SIGQUIT</literal>) — graceful
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2613 shutdown.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2614 Upon receiving this signal master process sends shutdown signal to all child
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2615 processes.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2616 When no child processes are left, master destroys cycle pool and exits.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2617 A worker process which received this signal, closes all listening sockets and
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2618 waits until timeout tree becomes empty, then destroys cycle pool and exits.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2619 A cache manager process exits right after receiving this signal.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2620 The variable <literal>ngx_quit</literal> is set to one after receiving this
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2621 signal and immediately reset after being processed.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2622 The variable <literal>ngx_exiting</literal> is set to one when worker process
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2623 is in shutdown state
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2624 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2625
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2626
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2627 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2628
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2629 <listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2630
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2631 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2632 <literal>NGX_TERMINATE_SIGNAL</literal> (<literal>SIGTERM</literal>) -
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2633 terminate.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2634 Upon receiving this signal master process sends terminate signal to all child
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2635 processes.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2636 If child processes do not exit in 1 second, they are killed with the
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2637 <literal>SIGKILL</literal> signal.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2638 When no child processes are left, master process destroys cycle pool and exits.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2639 A worker or cache manager process which received this signal destroys cycle
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2640 pool and exits.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2641 The variable <literal>ngx_terminate</literal> is set to one after receiving
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2642 this signal
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2643 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2644
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2645
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2646 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2647
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2648 <listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2649
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2650 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2651 <literal>NGX_NOACCEPT_SIGNAL</literal> (<literal>SIGWINCH</literal>)
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2652 - gracefully shut down worker processes
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2653 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2654
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2655
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2656 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2657
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2658 <listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2659
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2660 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2661 <literal>NGX_RECONFIGURE_SIGNAL</literal> (<literal>SIGHUP</literal>) -
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2662 reconfigure.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2663 Upon receiving this signal master process creates a new cycle from
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2664 configuration file.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2665 If the new cycle was created successfully, the old cycle is deleted and new
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2666 child processes are started.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2667 Meanwhile, the old processes receive the shutdown signal.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2668 In single-process mode, nginx creates a new cycle as well, but keeps the old
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2669 one until all clients, tied to the old cycle, are gone.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2670 Worker and helper processes ignore this signal
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2671 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2672
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2673
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2674 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2675
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2676 <listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2677
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2678 <para>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
2679 <literal>NGX_REOPEN_SIGNAL</literal> (<literal>SIGUSR1</literal>) — reopen
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2680 files.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2681 Master process passes this signal to workers.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2682 Worker processes reopen all <literal>open_files</literal> from the cycle
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2683 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2684
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2685
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2686 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2687
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2688 <listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2689
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2690 <para>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
2691 <literal>NGX_CHANGEBIN_SIGNAL</literal> (<literal>SIGUSR2</literal>) — change
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2692 nginx binary.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2693 Master process starts a new nginx binary and passes there a list of all listen
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2694 sockets.
1936
8f996938fe23 Style: proper quotes usage.
Vladimir Homutov <vl@nginx.com>
parents: 1935
diff changeset
2695 The list is passed in the environment variable <literal>“NGINX”</literal> in
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2696 text format, where descriptor numbers separated with semicolons.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2697 A new nginx instance reads that variable and adds the sockets to its init
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2698 cycle.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2699 Other processes ignore this signal
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2700 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2701
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2702
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2703 </listitem>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2704
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2705 </list>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2706
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2707 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2708 While all nginx worker processes are able to receive and properly handle POSIX
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2709 signals, master process normally does not pass any signals to workers and
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2710 helpers with the standard <literal>kill()</literal> syscall.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2711 Instead, nginx uses inter-process channels which allow sending messages between
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2712 all nginx processes.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2713 Currently, however, messages are only sent from master to its children.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2714 Those messages carry the same signals.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2715 The channels are socketpairs with their ends in different processes.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2716 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2717
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2718 <para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2719 When running nginx binary, several values can be specified next to
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2720 <literal>-s</literal> parameter.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2721 Those values are <literal>stop</literal>, <literal>quit</literal>,
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2722 <literal>reopen</literal>, <literal>reload</literal>.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2723 They are converted to signals <literal>NGX_TERMINATE_SIGNAL</literal>,
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2724 <literal>NGX_SHUTDOWN_SIGNAL</literal>, <literal>NGX_REOPEN_SIGNAL</literal>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2725 and <literal>NGX_RECONFIGURE_SIGNAL</literal> and sent to the nginx master
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2726 process, whose pid is read from nginx pid file.
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2727 </para>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2728
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2729 </section>
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
2730
1928
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2731 <section name="Modules" id="Modules">
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2732
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2733 <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
2734 <para>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2735 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
2736 at least two files:
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2737 <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
2738 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
2739 the module, for example:
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2740 <programlisting>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2741 ngx_module_type=CORE
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2742 ngx_module_name=ngx_foo_module
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2743 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
2744
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2745 . auto/module
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2746
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2747 ngx_addon_name=$ngx_module_name
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2748 </programlisting>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2749 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
2750 following variables:
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2751 <list type="bullet">
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2752
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2753 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
2754 <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
2755 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
2756 <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
2757 <literal>HTTP_AUX_FILTER</literal>, <literal>MAIL</literal>,
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2758 <literal>STREAM</literal>, or <literal>MISC</literal>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2759 </listitem>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2760
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2761 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
2762 <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
2763 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
2764 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
2765 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
2766 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
2767 </listitem>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2768
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2769 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
2770 <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
2771 console output text of the configure script.
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2772 </listitem>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2773
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2774 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
2775 <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
2776 files used to compile the module.
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2777 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
2778 module source.
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2779 </listitem>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2780
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2781 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
2782 <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
2783 </listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
2784
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
2785 <listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
2786 <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
2787 </listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
2788
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
2789 <listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
2790 <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
2791 module.
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2792 For example, libpthread would be linked using
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2793 <literal>ngx_module_libs=-lpthread</literal>.
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2794 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
2795 nginx:
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2796 <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
2797 <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
2798 <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
2799 </listitem>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2800
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2801 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
2802 <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
2803 <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
2804 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
2805 linking type.
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2806 </listitem>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2807
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2808 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
2809 <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
2810 is useful for <literal>HTTP_FILTER</literal> and
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2811 <literal>HTTP_AUX_FILTER</literal> module types.
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2812 The order is stored in a reverse list.
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2813
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2814 <para>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2815 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
2816 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
2817 This reads the data for other filters.
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2818 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
2819 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
2820 </para>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2821
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2822 <para>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2823 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
2824 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
2825 after.
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2826 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
2827 to be currently loaded.
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2828 </para>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2829
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2830 <para>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2831 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
2832 “<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
2833 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
2834 For other module types the default is empty.
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2835 </para>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2836
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2837 </listitem>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2838
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2839 </list>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2840
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2841 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
2842 <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
2843 <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
2844 </para>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2845
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2846 </section>
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
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2849 <section name="Core modules" id="core_modules">
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2850
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2851 <para>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2852 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
2853 implemented as modules.
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2854 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
2855 <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
2856 <programlisting>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2857 struct ngx_module_s {
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2858
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2859 /* private part is omitted */
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2860
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2861 void *ctx;
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2862 ngx_command_t *commands;
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2863 ngx_uint_t type;
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2864
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2865 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
2866
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2867 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
2868
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2869 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
2870 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
2871 void (*exit_thread)(ngx_cycle_t *cycle);
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2872 void (*exit_process)(ngx_cycle_t *cycle);
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2873
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2874 void (*exit_master)(ngx_cycle_t *cycle);
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2875
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2876 /* stubs for future extensions are omitted */
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2877 };
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2878 </programlisting>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2879 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
2880 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
2881 </para>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2882
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2883 <para>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2884 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
2885 recognizes specific configuration directives, specified in the
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2886 <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
2887 nginx lifecycle.
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2888 The module lifecycle consists of the following events:
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2889
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2890 <list type="bullet">
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2891
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2892 <listitem>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2893 Configuration directive handlers are called as they appear
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2894 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
2895 </listitem>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2896
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2897 <listitem>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2898 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
2899 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
2900 </listitem>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2901
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 The master process creates worker process(es) and
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2904 <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
2905 </listitem>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2906
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2907 <listitem>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2908 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
2909 the <literal>exit_process</literal> handler
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2910 </listitem>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2911
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2912 <listitem>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2913 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
2914 exiting.
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2915 </listitem>
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 </list>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2918
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2919 <note>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2920 <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
2921 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
2922 </note>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2923
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2924 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
2925 <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
2926 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
2927 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
2928 </para>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2929
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2930 <para>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2931 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
2932 <literal>ctx</literal> field.
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2933 There are several types of modules:
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2934 <list type="bullet">
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2935 <listitem><literal>NGX_CORE_MODULE</literal></listitem>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2936 <listitem><literal>NGX_EVENT_MODULE</literal></listitem>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2937 <listitem><literal>NGX_HTTP_MODULE</literal></listitem>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2938 <listitem><literal>NGX_MAIL_MODULE</literal></listitem>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2939 <listitem><literal>NGX_STREAM_MODULE</literal></listitem>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2940 </list>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2941 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
2942 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
2943 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
2944 problem domains, like handling events or http requests.
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2945 </para>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2946
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 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
2949 <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
2950 <literal>ngx_thread_pool_module</literal>,
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2951 <literal>ngx_openssl_module</literal>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2952 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
2953 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
2954 <programlisting>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2955 typedef struct {
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2956 ngx_str_t name;
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2957 void *(*create_conf)(ngx_cycle_t *cycle);
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2958 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
2959 } ngx_core_module_t;
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2960 </programlisting>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2961 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
2962 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
2963 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
2964 correspondingly.
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2965 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
2966 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
2967 was parsed successfully.
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2968 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
2969 configuration and sets default values.
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2970 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
2971 perform sanity checks and complete initialization.
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2972 </para>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2973
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2974 <para>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2975 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
2976 this:
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2977 <programlisting>
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 * Copyright (C) Author.
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
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2982
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2983 #include &lt;ngx_config.h&gt;
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2984 #include &lt;ngx_core.h&gt;
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
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2987 typedef struct {
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2988 ngx_flag_t enable;
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2989 } ngx_foo_conf_t;
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2990
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2991
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2992 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
2993 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
2994
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2995 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
2996 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
2997
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2998
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
2999 static ngx_command_t ngx_foo_commands[] = {
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3000
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3001 { ngx_string("foo_enabled"),
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3002 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
3003 ngx_conf_set_flag_slot,
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3004 0,
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3005 offsetof(ngx_foo_conf_t, enable),
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3006 &amp;ngx_foo_enable_post },
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3007
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3008 ngx_null_command
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
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3011
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3012 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
3013 ngx_string("foo"),
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3014 ngx_foo_create_conf,
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3015 ngx_foo_init_conf
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3016 };
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3017
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3018
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3019 ngx_module_t ngx_foo_module = {
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3020 NGX_MODULE_V1,
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3021 &amp;ngx_foo_module_ctx, /* module context */
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3022 ngx_foo_commands, /* module directives */
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3023 NGX_CORE_MODULE, /* module type */
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3024 NULL, /* init master */
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3025 NULL, /* init module */
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3026 NULL, /* init process */
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3027 NULL, /* init thread */
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3028 NULL, /* exit thread */
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3029 NULL, /* exit process */
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3030 NULL, /* exit master */
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3031 NGX_MODULE_V1_PADDING
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
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3034
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3035 static void *
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3036 ngx_foo_create_conf(ngx_cycle_t *cycle)
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3037 {
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3038 ngx_foo_conf_t *fcf;
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3039
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3040 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
3041 if (fcf == NULL) {
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3042 return NULL;
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3043 }
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3044
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3045 fcf->enable = NGX_CONF_UNSET;
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3046
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3047 return fcf;
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3048 }
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
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3051 static char *
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3052 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
3053 {
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3054 ngx_foo_conf_t *fcf = conf;
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3055
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3056 ngx_conf_init_value(fcf->enable, 0);
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3057
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3058 return NGX_CONF_OK;
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3059 }
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3060
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3061
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3062 static char *
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3063 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
3064 {
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3065 ngx_flag_t *fp = data;
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3066
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3067 if (*fp == 0) {
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3068 return NGX_CONF_OK;
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3069 }
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3070
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3071 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
3072
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3073 return NGX_CONF_OK;
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3074 }
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3075 </programlisting>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3076 </para>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3077
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3078 </section>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3079
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3080
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3081 <section name="Configuration directives" id="config_directives">
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3082
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3083 <para>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3084 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
3085 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
3086 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
3087 <programlisting>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3088 struct ngx_command_s {
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3089 ngx_str_t name;
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3090 ngx_uint_t type;
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3091 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
3092 ngx_uint_t conf;
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3093 ngx_uint_t offset;
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3094 void *post;
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3095 };
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3096 </programlisting>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3097 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
3098 “<literal>ngx_null_command</literal>”.
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3099 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
3100 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
3101 “<literal>listen</literal>”.
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3102 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
3103 command type and other properties using corresponding flags.
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3104 Arguments flags:
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3105
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3106 <list type="bullet">
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3107
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3108 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3109 <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
3110 </listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3111
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3112 <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
3113
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3114 <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
3115
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3116 <listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3117 <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
3118 </listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3119
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3120 <listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3121 <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
3122 or other combinations
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3123 </listitem>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3124
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3125 </list>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3126
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3127 Directive types:
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3128
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3129 <list type="bullet">
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3130
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3131 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3132 <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
3133 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
3134 to handle contents inside.
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3135 </listitem>
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 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3138 <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
3139 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
3140 strings.
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3141 </listitem>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3142 </list>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3143
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3144 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
3145 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
3146 <list type="bullet">
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 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3149 <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
3150 </listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3151
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3152 <listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3153 <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
3154 </listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3155
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3156 <listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3157 <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
3158 </listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3159
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3160 <listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3161 <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
3162 </listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3163
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3164 <listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3165 <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
3166 </listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3167
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3168 <listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3169 <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
3170 </listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3171
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3172 <listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3173 <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
3174 </listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3175
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3176 <listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3177 <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
3178 </listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3179
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3180 <listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3181 <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
3182 </listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3183
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3184 <listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3185 <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
3186 </listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3187
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3188 <listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3189 <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
3190 </listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3191
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3192 <listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3193 <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
3194 </listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3195
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3196 <listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3197 <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
3198 </listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3199
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3200 <listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3201 <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
3202 </listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3203
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3204 <listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3205 <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
3206 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
3207 </listitem>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3208 </list>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3209 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
3210 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
3211 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
3212 store their values in distinct places.
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3213 </para>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3214
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3215 <para>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3216 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
3217 and stores parsed values into corresponding configuration.
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3218 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
3219
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3220 <list type="bullet">
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3221
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3222 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3223 <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
3224 “<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
3225 <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
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 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3229 <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
3230 <literal>ngx_str_t</literal> type
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3231 </listitem>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3232
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3233 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3234 <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
3235 <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
3236 The array is created if not yet exists
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3237 </listitem>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3238
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3239 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3240 <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
3241 <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
3242 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
3243 The array is created if not yet exists
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
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3246 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3247 <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
3248 to a <literal>ngx_int_t</literal> value
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3249 </listitem>
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_conf_set_size_slot</literal> — converts
1928
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3253 <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
3254 in bytes
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3255 </listitem>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3256
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3257 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3258 <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
3259 <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
3260 in bytes
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3261 </listitem>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3262
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3263 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3264 <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
3265 <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
3266 in milliseconds
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3267 </listitem>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3268
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3269 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3270 <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
3271 <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
3272 in seconds
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3273 </listitem>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3274
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3275 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3276 <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
3277 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
3278 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
3279 </listitem>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3280
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3281 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3282 <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
3283 into <literal>ngx_uint_t</literal> value.
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3284 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
3285 <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
3286 integer values
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3287 </listitem>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3288
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3289 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3290 <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
3291 <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
3292 forming a bitmask.
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3293 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
3294 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
3295 mask values
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3296 </listitem>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3297
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3298 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3299 <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
3300 <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
3301 See the
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3302 <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
3303 directive description for details
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3304 </listitem>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3305
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3306 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3307 <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
3308 mask.
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3309 See the
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3310 <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
3311 directive description for details
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3312 </listitem>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3313
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3314 </list>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3315
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>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
3320 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
3321 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
3322 <literal>NGX_DIRECT_CONF</literal> flag.
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3323 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
3324 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
3325 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
3326 some limit.
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3327 In this modules, configuration structure is more complex.
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3328 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
3329 they manage their configuration.
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 <list type="bullet">
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3332 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3333 <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
3334 </listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3335
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3336 <listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3337 <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
3338 </listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3339
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3340 <listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3341 <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
3342 </listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3343
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3344 <listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3345 <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
3346 </listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3347
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3348 <listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3349 <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
3350 </listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3351
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3352 <listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3353 <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
3354 </listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3355
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3356 <listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3357 <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
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 </list>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3361
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3362 </para>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3363
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3364 <para>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3365 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
3366 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
3367 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
3368 </para>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3369
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3370 <para>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3371 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
3372 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
3373 data to the main handler.
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3374 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
3375 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
3376 <programlisting>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3377 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
3378 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
3379 </programlisting>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3380 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
3381 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
3382 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
3383 </para>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3384
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3385 </section>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3386
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3387 </section>
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3388
2c14a16c61eb Added modules section to development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1920
diff changeset
3389
1929
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3390 <section name="HTTP" id="http">
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3391
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3392
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3393 <section name="Connection" id="http_connection">
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3394
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3395 <para>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3396 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
3397 </para>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3398
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3399 <list type="bullet">
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3400
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3401 <listitem>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3402 <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
3403 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
3404 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
3405 The object wraps the newly accepted client socket.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3406 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
3407 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
3408 </listitem>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3409
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3410 <listitem>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3411 <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
3412 an HTTP connection.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3413 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
3414 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
3415 <literal>data</literal> field.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3416 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
3417 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
3418 </listitem>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3419
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3420 <listitem>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3421 <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
3422 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
3423 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
3424 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
3425 </listitem>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3426
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3427 <listitem>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3428 <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
3429 which reads client request line.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3430 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
3431 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
3432 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
3433 <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
3434 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
3435 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
3436 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
3437 </listitem>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3438
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3439 <listitem>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3440 <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
3441 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
3442 client request header
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3443 </listitem>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3444
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3445 <listitem>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3446 <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
3447 is completely read and parsed.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3448 This function runs request phases from
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3449 <literal>NGX_HTTP_POST_READ_PHASE</literal> to
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3450 <literal>NGX_HTTP_CONTENT_PHASE</literal>.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3451 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
3452 chain.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3453 The response in not necessarily sent to the client at this phase.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3454 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
3455 </listitem>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3456
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3457 <listitem>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3458 <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
3459 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
3460 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
3461 response.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3462 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
3463 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
3464 sending outstanding data
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3465 </listitem>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3466
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3467 <listitem>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3468 <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
3469 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
3470 If client connection keepalive feature is enabled,
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3471 <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
3472 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
3473 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
3474 request and the connection
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3475 </listitem>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3476
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3477 </list>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3478
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3479 </section>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3480
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3481
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3482 <section name="Request" id="http_request">
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3483
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3484 <para>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3485 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
3486 created. Some of the fields of this object:
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3487 </para>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3488
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3489 <list type="bullet">
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3490
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3491 <listitem>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3492
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3493 <para>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3494 <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
3495 client connection object.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3496 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
3497 one main request and its subrequests.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3498 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
3499 </para>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3500
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3501 <para>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3502 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
3503 <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
3504 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
3505 connection.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3506 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
3507 output its response to the client.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3508 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
3509 output
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3510 </para>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3511
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3512 </listitem>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3513
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3514 <listitem>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3515
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3516 <para>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3517 <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
3518 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
3519 (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
3520 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
3521 <literal>ctx_index</literal> position.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3522 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
3523 </para>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3524
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3525 <list type="bullet">
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3526
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3527 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3528 <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
3529 <literal>module</literal>'s context
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
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3532 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3533 <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
3534 as <literal>module</literal>'s context
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3535 </listitem>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3536
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3537 </list>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3538
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3539 </listitem>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3540
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3541 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3542 <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
3543 configurations.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3544 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
3545 </listitem>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3546
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3547 <listitem>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3548 <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
3549 read and write event handlers for the request.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3550 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
3551 set as both read and write event handlers.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3552 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
3553 <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
3554 </listitem>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3555
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3556 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3557 <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
3558 </listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3559
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3560 <listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3561 <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
3562 </listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3563
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3564 <listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3565 <literal>pool</literal> — request pool.
1929
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3566 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
3567 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
3568 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
3569 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
3570 </listitem>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3571
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3572 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3573 <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
3574 </listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3575
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3576 <listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3577 <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
3578 objects.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3579 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
3580 <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
3581 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
3582 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
3583 <literal>status</literal> etc
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3584 </listitem>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3585
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3586 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3587 <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
3588 </listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3589
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3590 <listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3591 <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
3592 created.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3593 Used for tracking request duration
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 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3597 <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
3598 client HTTP request method.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3599 Numeric values for methods are defined in
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3600 <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
3601 <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
3602 </listitem>
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 <listitem>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3605 <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
3606 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
3607 “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
3608 <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
3609 versions
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3610 </listitem>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3611
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3612 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3613 <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
3614 and URI
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>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3618 <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
3619 extention.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3620 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
3621 normalization.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3622 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
3623 redirects
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3624 </listitem>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3625
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3626 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3627 <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
3628 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
3629 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
3630 </listitem>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3631
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3632 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3633 <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
3634 </listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3635
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3636 <listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3637 <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
3638 order they are sent and created.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3639 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
3640 when parts of it are created by subrequests
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3641 </listitem>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3642
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3643 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3644 <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
3645 called when a subrequest gets finalized.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3646 Unused for main requests
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3647 </listitem>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3648
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3649 <listitem>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3650
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3651 <para>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3652 <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
3653 resumed.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3654 Starting or resuming is done by calling the request's
1932
937e03180281 Fixed trailing spaces.
Vladimir Homutov <vl@nginx.com>
parents: 1929
diff changeset
3655 <literal>write_event_handler</literal>.
1929
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3656 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
3657 request phases and then produces the output.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3658 </para>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3659
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3660 <para>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3661 A request is usually posted by the
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3662 <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
3663 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
3664 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
3665 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
3666 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
3667 posted requests.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3668 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
3669 </para>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3670
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3671 </listitem>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3672
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3673 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3674 <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
3675 </listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3676
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 <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
3679 by the last regex match of the request.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3680 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
3681 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
3682 etc.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3683 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
3684 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
3685 <literal>captures</literal> holds captures boundaries,
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3686 <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
3687 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
3688 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
3689 </listitem>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3690
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3691 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3692 <literal>count</literal> — request reference counter.
1929
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3693 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
3694 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
3695 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
3696 should be called.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3697 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
3698 counter
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3699 </listitem>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3700
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3701 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3702 <literal>subrequests</literal> — current subrequest nesting level.
1929
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3703 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
3704 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
3705 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
3706 <literal>NGX_HTTP_MAX_SUBREQUESTS</literal> constant
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
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3709 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3710 <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
3711 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
3712 <literal>NGX_HTTP_MAX_URI_CHANGES</literal> constant.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3713 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
3714 In the latter case an error is generated.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3715 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
3716 normal or named locations
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3717 </listitem>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3718
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3719 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3720 <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
3721 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
3722 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
3723 thread operations) and active cache lock
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3724 </listitem>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3725
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3726 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3727 <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
3728 output produced by the request.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3729 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
3730 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
3731 of free output_buffers etc.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3732 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
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>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
3737 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
3738 </listitem>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3739
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3740 <listitem>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3741 <para>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3742 <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
3743 supported.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3744 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
3745 value
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3746 </para>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3747 </listitem>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3748
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3749 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3750 <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
3751 been sent by the request
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 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3755 <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
3756 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
3757 redirect or be a subrequest.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3758 Internal requests are allowed to enter internal locations
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3759 </listitem>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3760
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3761 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3762 <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
3763 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
3764 </listitem>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3765
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3766 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3767 <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
3768 allowed to be sent while processing a subrequest
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3769 </listitem>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3770
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3771 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3772 <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
3773 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
3774 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
3775 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
3776 </listitem>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3777
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3778 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3779 <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
3780 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
3781 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
3782 sendfile is enabled.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3783 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
3784 set them.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3785 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
3786 <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
3787 request output should come in memory buffers.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3788 Filters called later in filter chain set
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3789 <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
3790 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
3791 </listitem>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3792
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3793 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
3794 <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
3795 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
3796 file buffers.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3797 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
3798 it's sent </listitem>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3799
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3800 </list>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3801
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3802 </section>
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
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3805 <section name="Configuration" id="http_conf">
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3806
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3807 <para>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3808 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
3809 </para>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3810
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3811 <list type="bullet">
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3812
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3813 <listitem>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3814 Main configuration.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3815 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
3816 configuration.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3817 It stores global settings for a module
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3818 </listitem>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3819
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 Server configuration.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3822 This configuraion applies to a single nginx server{}.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3823 It stores server-specific settings for a module
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3824 </listitem>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3825
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3826 <listitem>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3827 Location configuration.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3828 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
3829 This configuration stores settings specific to a location
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3830 </listitem>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3831
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3832 </list>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3833
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3834 <para>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3835 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
3836 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
3837 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
3838 configuration.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3839 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
3840 type.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3841 </para>
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 <programlisting>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3844 typedef struct {
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3845 ngx_uint_t foo;
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3846 } ngx_http_foo_loc_conf_t;
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3847
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3848
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3849 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
3850 NULL, /* preconfiguration */
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3851 NULL, /* postconfiguration */
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3852
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3853 NULL, /* create main configuration */
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3854 NULL, /* init main configuration */
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3855
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3856 NULL, /* create server configuration */
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3857 NULL, /* merge server configuration */
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3858
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3859 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
3860 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
3861 };
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3862
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 static void *
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3865 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
3866 {
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3867 ngx_http_foo_loc_conf_t *conf;
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 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
3870 if (conf == NULL) {
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3871 return NULL;
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3872 }
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 conf->foo = NGX_CONF_UNSET_UINT;
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3875
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3876 return conf;
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3877 }
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3878
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3879
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3880 static char *
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3881 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
3882 {
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3883 ngx_http_foo_loc_conf_t *prev = parent;
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3884 ngx_http_foo_loc_conf_t *conf = child;
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3885
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3886 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
3887 }
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3888 </programlisting>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3889
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3890 <para>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3891 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
3892 function creates a new configuration structure and
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3893 <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
3894 another configuration from a higher level.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3895 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
3896 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
3897 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
3898 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
3899 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
3900 settings at any level of nginx configuration file.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3901 Eventually configurations are merged down.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3902 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
3903 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
3904 <literal>NGX_CONF_UNSET_UINT</literal>.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3905 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
3906 <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
3907 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
3908 explicit value.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3909 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
3910 <literal>src/core/ngx_conf_file.h</literal>.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3911 </para>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3912
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3913 <para>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3914 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
3915 macros are available.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3916 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
3917 </para>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3918
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3919 <list type="bullet">
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3920
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 <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
3923 </listitem>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3924
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3925 <listitem>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3926 <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
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 <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
3931 </listitem>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3932
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3933 </list>
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 <para>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3936 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
3937 standard nginx core module
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3938 <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
3939 and changes
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3940 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
3941 structure.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3942 </para>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3943
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3944 <programlisting>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3945 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
3946
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3947
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3948 static ngx_command_t ngx_http_foo_commands[] = {
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3949
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3950 { ngx_string("foo"),
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3951 NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS,
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3952 ngx_http_foo,
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3953 0,
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3954 0,
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3955 NULL },
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3956
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3957 ngx_null_command
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
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3960
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3961 static char *
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3962 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
3963 {
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3964 ngx_http_core_loc_conf_t *clcf;
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 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
3967 clcf->handler = ngx_http_bar_handler;
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3968
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3969 return NGX_CONF_OK;
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3970 }
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3971 </programlisting>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3972
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3973 <para>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3974 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
3975 modules.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3976 </para>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3977
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3978 <list type="bullet">
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3979
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3980 <listitem>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3981 <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
3982 </listitem>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3983
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3984 <listitem>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3985 <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
3986 </listitem>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3987
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3988 <listitem>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3989 <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
3990 </listitem>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3991
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3992 </list>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3993
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3994 <para>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3995 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
3996 <literal>ngx_http_request_t</literal>.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3997 Main configuration of a request never changes.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
3998 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
3999 server for a request.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4000 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
4001 rewrite or internal redirect.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4002 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
4003 </para>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4004
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4005 <programlisting>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4006 static ngx_int_t
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4007 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
4008 {
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4009 ngx_http_foo_loc_conf_t *flcf;
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4010
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4011 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
4012
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4013 ...
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4014 }
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4015 </programlisting>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4016
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4017 </section>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4018
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4019
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4020 <section name="Phases" id="http_phases">
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 <para>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4023 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
4024 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
4025 Most phases allow installing handlers.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4026 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
4027 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
4028 at a specific request processing stage.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4029 Following is the list of nginx HTTP phases.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4030 </para>
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 <list type="bullet">
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4033
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 <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
4036 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
4037 installs its handler at this phase.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4038 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
4039 </listitem>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4040
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4041 <listitem>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4042 <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
4043 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
4044 The
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4045 <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
4046 installs its handler at this phase
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4047 </listitem>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4048
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4049 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
4050 <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
4051 location based on request URI.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4052 This phase does not allow installing any handlers.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4053 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
4054 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
4055 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
4056 location configuration.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4057 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
4058 </listitem>
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 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
4061 <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
4062 <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
4063 chosen at the prevous phase
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4064 </listitem>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4065
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4066 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
4067 <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
4068 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
4069 The redirect is done by going back to
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4070 <literal>NGX_HTTP_FIND_CONFIG_PHASE</literal>.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4071 No handlers are allowed at this phase
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4072 </listitem>
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 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
4075 <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
4076 types of handlers, not associated with access check.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4077 Standard nginx modules
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4078 <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
4079 </link> and
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4080 <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
4081 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
4082 </listitem>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4083
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4084 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
4085 <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
4086 for the request.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4087 Standard nginx modules such as
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4088 <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
4089 <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
4090 </link> register their handlers at this phase.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4091 If configured so by the
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4092 <link doc="../http/ngx_http_core_module.xml" id="satisfy"/> directive, only one
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4093 of access phase handlers may allow access to the request in order to confinue
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4094 processing
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4095 </listitem>
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 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
4098 <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
4099 <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
4100 case.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4101 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
4102 request is finalized.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4103 No handlers are supported at this phase
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4104 </listitem>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4105
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4106 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
4107 <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
4108 <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
4109 No handlers are allowed at this phase
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4110 </listitem>
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 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
4113 <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
4114 is supposed to be generated.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4115 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
4116 example
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4117 <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
4118 <literal>ngx_http_static_module</literal>.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4119 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
4120 the output.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4121 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
4122 If the
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4123 <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
4124 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
4125 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
4126 </listitem>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4127
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4128 <listitem>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4129 <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
4130 Currently, only the
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4131 <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
4132 registers its handler
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4133 at this stage for access logging.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4134 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
4135 before freeing the request
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4136 </listitem>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4137
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4138 </list>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4139
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4140 <para>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4141 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
4142 </para>
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 <programlisting>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4145 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
4146 NULL, /* preconfiguration */
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4147 ngx_http_foo_init, /* postconfiguration */
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4148
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4149 NULL, /* create main configuration */
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4150 NULL, /* init main configuration */
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 NULL, /* create server configuration */
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4153 NULL, /* merge server configuration */
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4154
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4155 NULL, /* create location configuration */
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4156 NULL /* merge location configuration */
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4157 };
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4158
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4159
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4160 static ngx_int_t
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4161 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
4162 {
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4163 ngx_str_t *ua;
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4164
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4165 ua = r->headers_in->user_agent;
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4166
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4167 if (ua == NULL) {
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4168 return NGX_DECLINED;
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4169 }
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4170
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4171 /* reject requests with "User-Agent: foo" */
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4172 if (ua->value.len == 3 &amp;&amp; ngx_strncmp(ua->value.data, "foo", 3) == 0) {
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4173 return NGX_HTTP_FORBIDDEN;
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4174 }
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4175
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4176 return NGX_DECLINED;
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4177 }
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4178
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4179
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4180 static ngx_int_t
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4181 ngx_http_foo_init(ngx_conf_t *cf)
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4182 {
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4183 ngx_http_handler_pt *h;
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4184 ngx_http_core_main_conf_t *cmcf;
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4185
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4186 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
4187
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4188 h = ngx_array_push(&amp;cmcf->phases[NGX_HTTP_PREACCESS_PHASE].handlers);
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4189 if (h == NULL) {
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4190 return NGX_ERROR;
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4191 }
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4192
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4193 *h = ngx_http_foo_handler;
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4194
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4195 return NGX_OK;
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4196 }
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4197 </programlisting>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4198
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4199 <para>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4200 Phase handlers are expected to return specific codes:
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4201 </para>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4202
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4203 <list type="bullet">
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4204
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4205 <listitem>
1935
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
4206 <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
4207 </listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
4208
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
4209 <listitem>
be7490a66d1b Style: use long dash with non-breaking space where appropriate.
Vladimir Homutov <vl@nginx.com>
parents: 1932
diff changeset
4210 <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
4211 phase.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4212 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
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_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
4217 future event.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4218 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
4219 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
4220 <literal>ngx_http_core_run_phases()</literal>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4221 </listitem>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4222
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4223 <listitem>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4224 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
4225 finalization code, in particular, HTTP response code.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4226 The request is finalized with the code provided
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4227 </listitem>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4228
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4229 </list>
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 <para>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4232 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
4233 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
4234 is considered a finalization code.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4235 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
4236 finalization code.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4237 At access phase, in
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4238 <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
4239 mode, returning a code other
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4240 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
4241 a denial.
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4242 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
4243 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
4244 </para>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4245
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4246 </section>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
4247
1939
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
4248
1959
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4249 <section name="Variables" id="http_variables">
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4250
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4251 <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
4252
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4253 <para>
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4254 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
4255 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
4256 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
4257 to configuration.
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4258 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
4259 <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
4260 <programlisting>
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4261 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
4262 ngx_int_t index;
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4263
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4264 index = ngx_http_get_variable_index(cf, &amp;name);
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4265 </programlisting>
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4266 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
4267 <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
4268 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
4269 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
4270 future use.
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4271 </para>
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4272
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4273 <para>
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4274 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
4275 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
4276 All functions that evaluate variables return
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4277 <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
4278 the variable value:
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4279 <programlisting>
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4280 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
4281
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4282 typedef struct {
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4283 unsigned len:28;
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4284
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4285 unsigned valid:1;
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4286 unsigned no_cacheable:1;
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4287 unsigned not_found:1;
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4288 unsigned escape:1;
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4289
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4290 u_char *data;
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4291 } ngx_variable_value_t;
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4292 </programlisting>
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4293 where:
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4294 <list type="bullet">
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4295
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4296 <listitem>
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4297 <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
4298 </listitem>
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4299
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4300 <listitem>
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4301 <literal>data</literal> — value itself
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4302 </listitem>
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4303
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4304 <listitem>
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4305 <literal>valid</literal> — value is valid
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4306 </listitem>
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4307
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4308 <listitem>
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4309 <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
4310 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
4311 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
4312 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
4313 </listitem>
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4314
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4315 <listitem>
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4316 <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
4317 </listitem>
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4318
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4319 <listitem>
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4320 <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
4321 values that require escaping on output
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4322 </listitem>
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4323
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4324 </list>
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4325 </para>
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4326
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4327 <para>
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4328 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
4329 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
4330 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
4331 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
4332 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
4333 identifying it.
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4334 Example of typical usage:
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4335 <programlisting>
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4336 ngx_http_variable_value_t *v;
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4337
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4338 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
4339
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4340 if (v == NULL || v->not_found) {
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4341 /* 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
4342 return NGX_ERROR;
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4343 }
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4344
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4345 /* some meaningful value is found */
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4346 </programlisting>
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4347 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
4348 <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
4349 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
4350 non-cacheable variables.
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4351 </para>
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4352
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4353 <para>
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4354 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
4355 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
4356 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
4357 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
4358 used in such cases.
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4359 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
4360 <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
4361 </para>
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4362
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4363 </section>
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4364
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4365
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4366 <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
4367
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4368 <para>
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4369 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
4370 is used.
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4371 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
4372 flags that control its behaviour:
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4373
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4374 <list type="bullet">
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4375 <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
4376 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
4377 no conflict will happen.
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4378 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
4379 <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
4380 </listitem>
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4381
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4382 <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
4383 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
4384 </listitem>
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4385
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4386 <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
4387 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
4388 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
4389 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
4390 </listitem>
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4391
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4392 <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
4393 variable is a prefix.
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4394 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
4395 variable.
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4396 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
4397 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
4398 of specific argument.
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
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4401 </list>
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 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
4404 <literal>ngx_http_variable_t</literal>:
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4405 <programlisting>
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4406 struct ngx_http_variable_s {
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4407 ngx_str_t name;
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4408 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
4409 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
4410 uintptr_t data;
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4411 ngx_uint_t flags;
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4412 ngx_uint_t index;
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4413 };
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4414 </programlisting>
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4415
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4416 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
4417 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
4418 <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
4419 <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
4420 the variable.
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4421 </para>
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4422
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4423 <para>
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4424 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
4425 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
4426 into configuration:
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4427 <programlisting>
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4428 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
4429
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4430 { 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
4431
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4432 { 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
4433 };
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4434
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4435 static ngx_int_t
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4436 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
4437 {
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4438 ngx_http_variable_t *var, *v;
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4439
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4440 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
4441 var = ngx_http_add_variable(cf, &amp;v->name, v->flags);
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4442 if (var == NULL) {
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4443 return NGX_ERROR;
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4444 }
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4445
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4446 var->get_handler = v->get_handler;
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4447 var->data = v->data;
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4448 }
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4449
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4450 return NGX_OK;
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4451 }
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4452 </programlisting>
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4453 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
4454 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
4455 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
4456 </para>
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4457
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4458 <para>
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4459 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
4460 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
4461 <programlisting>
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4462 static ngx_int_t
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4463 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
4464 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
4465 {
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4466 u_char *p;
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 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
4469 if (p == NULL) {
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4470 return NGX_ERROR;
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4471 }
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4472
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4473 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
4474 v->valid = 1;
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4475 v->no_cacheable = 0;
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4476 v->not_found = 0;
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4477 v->data = p;
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4478
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4479 return NGX_OK;
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4480 }
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4481 </programlisting>
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4482 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
4483 (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
4484 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
4485 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
4486 </para>
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4487
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4488 <para>
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4489 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
4490 referred by the variable.
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4491 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
4492 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
4493 <programlisting>
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 { 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
4496 ngx_http_variable_request_get_size,
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4497 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
4498 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
4499 ...
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4500
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4501 static void
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4502 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
4503 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
4504 {
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4505 ssize_t s, *sp;
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4506 ngx_str_t val;
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4507
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4508 val.len = v->len;
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4509 val.data = v->data;
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4510
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4511 s = ngx_parse_size(&amp;val);
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4512
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4513 if (s == NGX_ERROR) {
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4514 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
4515 "invalid size \"%V\"", &amp;val);
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4516 return;
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4517 }
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 sp = (ssize_t *) ((char *) r + data);
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4520
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4521 *sp = s;
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4522
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4523 return;
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4524 }
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4525 </programlisting>
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4526
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4527 </para>
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4528
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4529 </section>
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4530
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4531 </section>
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
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4534 <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
4535
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4536 <para>
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4537 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
4538 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
4539 </para>
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 <para>
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4542 The complex value description in
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4543 <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
4544 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
4545 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
4546
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4547 <programlisting>
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4548 ngx_str_t *value;
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4549 ngx_http_complex_value_t cv;
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4550 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
4551
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4552 value = cf->args->elts; /* directive arguments */
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4553
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4554 ngx_memzero(&amp;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
4555
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4556 ccv.cf = cf;
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4557 ccv.value = &amp;value[1];
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4558 ccv.complex_value = &amp;cv;
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4559 ccv.zero = 1;
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4560 ccv.conf_prefix = 1;
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4561
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4562 if (ngx_http_compile_complex_value(&amp;ccv) != NGX_OK) {
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4563 return NGX_CONF_ERROR;
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4564 }
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4565 </programlisting>
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4566
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4567 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
4568 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
4569
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4570 <list type="bullet">
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4571
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4572 <listitem>
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4573 <literal>cf</literal> — configuration pointer
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4574 </listitem>
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 <listitem>
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4577 <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
4578 </listitem>
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4579
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4580 <listitem>
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4581 <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
4582 </listitem>
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 <listitem>
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4585 <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
4586 </listitem>
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4587
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4588 <listitem>
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4589 <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
4590 (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
4591 </listitem>
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4592
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4593 <listitem>
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4594 <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
4595 (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
4596 </listitem>
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 </list>
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4599 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
4600 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
4601 dealing with filenames.
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4602 </para>
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 <para>
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4605 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
4606 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
4607 in the expression.
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4608 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
4609 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
4610 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
4611 </para>
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4612
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4613 <para>
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4614 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
4615 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
4616 declaration.
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4617 </para>
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4618
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4619 <para>
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4620 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
4621 <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
4622 <programlisting>
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4623 ngx_str_t res;
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4624
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4625 if (ngx_http_complex_value(r, &amp;cv, &amp;res) != NGX_OK) {
d0aebb2337ec Added the "Variables" section to the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1958
diff changeset
4626 return NGX_ERROR;
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 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
4630 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
4631 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
4632 </para>
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
1967
ef27e3ef0c46 The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1960
diff changeset
4637 <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
4638
ef27e3ef0c46 The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1960
diff changeset
4639 <para>
ef27e3ef0c46 The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1960
diff changeset
4640 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
4641 <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
4642 structure.
ef27e3ef0c46 The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1960
diff changeset
4643 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
4644 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
4645 <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
4646 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
4647 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
4648 request.
ef27e3ef0c46 The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1960
diff changeset
4649 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
4650 <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
4651 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
4652 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
4653 <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
4654 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
4655 configured for the server.
ef27e3ef0c46 The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1960
diff changeset
4656 The
ef27e3ef0c46 The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1960
diff changeset
4657 <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
4658 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
4659 <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
4660 <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
4661 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
4662 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
4663 </para>
ef27e3ef0c46 The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1960
diff changeset
4664
ef27e3ef0c46 The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1960
diff changeset
4665 <para>
ef27e3ef0c46 The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1960
diff changeset
4666 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
4667 calling one of the functions
ef27e3ef0c46 The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1960
diff changeset
4668 <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
4669 <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
4670 </para>
ef27e3ef0c46 The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1960
diff changeset
4671
ef27e3ef0c46 The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1960
diff changeset
4672 <para>
ef27e3ef0c46 The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1960
diff changeset
4673 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
4674 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
4675 <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
4676 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
4677 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
4678 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
4679 </para>
ef27e3ef0c46 The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1960
diff changeset
4680
ef27e3ef0c46 The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1960
diff changeset
4681 <para>
ef27e3ef0c46 The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1960
diff changeset
4682 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
4683 arguments.
ef27e3ef0c46 The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1960
diff changeset
4684 </para>
ef27e3ef0c46 The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1960
diff changeset
4685
ef27e3ef0c46 The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1960
diff changeset
4686 <programlisting>
ef27e3ef0c46 The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1960
diff changeset
4687 ngx_int_t
ef27e3ef0c46 The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1960
diff changeset
4688 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
4689 {
ef27e3ef0c46 The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1960
diff changeset
4690 ngx_str_t uri, args;
ef27e3ef0c46 The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1960
diff changeset
4691
ef27e3ef0c46 The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1960
diff changeset
4692 ngx_str_set(&amp;uri, "/foo");
ef27e3ef0c46 The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1960
diff changeset
4693 ngx_str_set(&amp;args, "bar=1");
ef27e3ef0c46 The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1960
diff changeset
4694
ef27e3ef0c46 The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1960
diff changeset
4695 return ngx_http_internal_redirect(r, &amp;uri, &amp;args);
ef27e3ef0c46 The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1960
diff changeset
4696 }
ef27e3ef0c46 The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1960
diff changeset
4697 </programlisting>
ef27e3ef0c46 The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1960
diff changeset
4698
ef27e3ef0c46 The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1960
diff changeset
4699 <para>
ef27e3ef0c46 The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1960
diff changeset
4700 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
4701 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
4702 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
4703 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
4704 <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
4705 </para>
ef27e3ef0c46 The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1960
diff changeset
4706
ef27e3ef0c46 The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1960
diff changeset
4707 <para>
ef27e3ef0c46 The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1960
diff changeset
4708 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
4709 </para>
ef27e3ef0c46 The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1960
diff changeset
4710
ef27e3ef0c46 The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1960
diff changeset
4711 <programlisting>
ef27e3ef0c46 The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1960
diff changeset
4712 ngx_int_t
ef27e3ef0c46 The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1960
diff changeset
4713 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
4714 {
ef27e3ef0c46 The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1960
diff changeset
4715 ngx_str_t name;
ef27e3ef0c46 The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1960
diff changeset
4716
ef27e3ef0c46 The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1960
diff changeset
4717 ngx_str_set(&amp;name, "foo");
ef27e3ef0c46 The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1960
diff changeset
4718
ef27e3ef0c46 The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1960
diff changeset
4719 return ngx_http_named_location(r, &amp;name);
ef27e3ef0c46 The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1960
diff changeset
4720 }
ef27e3ef0c46 The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1960
diff changeset
4721 </programlisting>
ef27e3ef0c46 The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1960
diff changeset
4722
ef27e3ef0c46 The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1960
diff changeset
4723 <para>
ef27e3ef0c46 The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1960
diff changeset
4724 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
4725 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
4726 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
4727 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
4728 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
4729 erased by both redirect functions.
ef27e3ef0c46 The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1960
diff changeset
4730 </para>
ef27e3ef0c46 The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1960
diff changeset
4731
ef27e3ef0c46 The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1960
diff changeset
4732 <para>
ef27e3ef0c46 The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1960
diff changeset
4733 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
4734 <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
4735 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
4736 </para>
ef27e3ef0c46 The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1960
diff changeset
4737
ef27e3ef0c46 The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1960
diff changeset
4738 </section>
ef27e3ef0c46 The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1960
diff changeset
4739
ef27e3ef0c46 The HTTP request redirection section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1960
diff changeset
4740
1960
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4741 <section name="Response" id="http_response">
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4742
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4743 <para>
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4744 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
4745 the optional response body.
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4746 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
4747 written to the client socket.
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4748 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
4749 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
4750 </para>
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4751
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4752
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4753 <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
4754
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4755 <para>
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4756 Output header is sent by the function
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4757 <literal>ngx_http_send_header(r)</literal>.
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4758 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
4759 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
4760 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
4761 <literal>r->headers_out</literal>.
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4762 If the response status suggests that a response body follows the header,
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4763 <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
4764 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
4765 unknown.
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4766 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
4767 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
4768 appended.
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4769 </para>
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4770
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4771 <programlisting>
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4772 static ngx_int_t
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4773 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
4774 {
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4775 ngx_int_t rc;
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4776 ngx_table_elt_t *h;
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4777
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4778 /* send header */
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4779
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4780 r->headers_out.status = NGX_HTTP_OK;
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4781 r->headers_out.content_length_n = 3;
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4782
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4783 /* X-Foo: foo */
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4784
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4785 h = ngx_list_push(&amp;r->headers_out.headers);
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4786 if (h == NULL) {
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4787 return NGX_ERROR;
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4788 }
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4789
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4790 h->hash = 1;
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4791 ngx_str_set(&amp;h->key, "X-Foo");
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4792 ngx_str_set(&amp;h->value, "foo");
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4793
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4794 rc = ngx_http_send_header(r);
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4795
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4796 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
4797 return rc;
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4798 }
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4799
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4800 /* send body */
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4801
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4802 ...
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4803 }
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4804 </programlisting>
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4805
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4806 </section>
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4807
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4808
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4809 <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
4810
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4811 <para>
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4812 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
4813 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
4814 <literal>ngx_http_top_header_filter</literal>.
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4815 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
4816 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
4817 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
4818 <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
4819 <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
4820 </para>
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4821
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4822 <para>
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4823 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
4824 <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
4825 time.
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4826 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
4827 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
4828 </para>
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4829
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4830 <para>
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4831 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
4832 "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
4833 </para>
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4834
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4835 <programlisting>
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4836 #include &lt;ngx_config.h&gt;
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4837 #include &lt;ngx_core.h&gt;
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4838 #include &lt;ngx_http.h&gt;
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4839
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4840
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4841 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
4842 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
4843
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4844
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4845 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
4846 NULL, /* preconfiguration */
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4847 ngx_http_foo_header_filter_init, /* postconfiguration */
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4848
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4849 NULL, /* create main configuration */
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4850 NULL, /* init main configuration */
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4851
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4852 NULL, /* create server configuration */
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4853 NULL, /* merge server configuration */
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4854
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4855 NULL, /* create location configuration */
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4856 NULL /* merge location configuration */
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4857 };
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4858
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4859
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4860 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
4861 NGX_MODULE_V1,
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4862 &amp;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
4863 NULL, /* module directives */
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4864 NGX_HTTP_MODULE, /* module type */
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4865 NULL, /* init master */
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4866 NULL, /* init module */
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4867 NULL, /* init process */
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4868 NULL, /* init thread */
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4869 NULL, /* exit thread */
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4870 NULL, /* exit process */
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4871 NULL, /* exit master */
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4872 NGX_MODULE_V1_PADDING
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4873 };
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4874
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4875
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4876 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
4877
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4878
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4879 static ngx_int_t
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4880 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
4881 {
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4882 ngx_table_elt_t *h;
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4883
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4884 /*
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4885 * 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
4886 * to every HTTP 200 response
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4887 */
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4888
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4889 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
4890 return ngx_http_next_header_filter(r);
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4891 }
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4892
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4893 h = ngx_list_push(&amp;r->headers_out.headers);
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4894 if (h == NULL) {
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4895 return NGX_ERROR;
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4896 }
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4897
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4898 h->hash = 1;
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4899 ngx_str_set(&amp;h->key, "X-Foo");
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4900 ngx_str_set(&amp;h->value, "foo");
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4901
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4902 return ngx_http_next_header_filter(r);
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4903 }
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4904
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4905
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4906 static ngx_int_t
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4907 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
4908 {
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4909 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
4910 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
4911
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4912 return NGX_OK;
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4913 }
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4914 </programlisting>
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4915
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4916 </section>
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4917
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4918 </section>
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4919
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4920
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4921 <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
4922
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4923 <para>
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4924 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
4925 <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
4926 The function can be called multiple times.
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4927 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
4928 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
4929 </para>
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4930
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4931 <para>
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4932 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
4933 In order for the example to work not only as a main request but as a subrequest
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4934 as well, the <literal>last_in_chain_flag</literal> is set in the last buffer
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4935 of the output.
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4936 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
4937 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
4938 </para>
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4939
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4940 <programlisting>
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4941 static ngx_int_t
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4942 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
4943 {
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4944 ngx_int_t rc;
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4945 ngx_buf_t *b;
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4946 ngx_chain_t out;
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4947
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4948 /* send header */
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4949
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4950 r->headers_out.status = NGX_HTTP_OK;
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4951 r->headers_out.content_length_n = 3;
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4952
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4953 rc = ngx_http_send_header(r);
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4954
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4955 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
4956 return rc;
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4957 }
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4958
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4959 /* send body */
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4960
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4961 b = ngx_calloc_buf(r->pool);
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4962 if (b == NULL) {
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4963 return NGX_ERROR;
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4964 }
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4965
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4966 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
4967 b->last_in_chain = 1;
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4968
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4969 b->memory = 1;
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4970
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4971 b->pos = (u_char *) "foo";
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4972 b->last = b->pos + 3;
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4973
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4974 out.buf = b;
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4975 out.next = NULL;
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4976
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4977 return ngx_http_output_filter(r, &amp;out);
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4978 }
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4979 </programlisting>
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4980
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4981 </section>
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4982
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4983
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4984 <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
4985
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4986 <para>
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4987 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
4988 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
4989 <literal>ngx_http_top_body_filter</literal>.
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4990 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
4991 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
4992 </para>
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4993
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4994 <para>
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4995 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
4996 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
4997 the next handler.
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
4998 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
4999 incoming chain belong to the caller.
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5000 They should never be reused or changed.
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5001 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
5002 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
5003 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
5004 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
5005 </para>
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5006
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5007 <para>
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5008 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
5009 body bytes.
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5010 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
5011 used in the access log.
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5012 </para>
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5013
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5014 <programlisting>
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5015 #include &lt;ngx_config.h&gt;
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5016 #include &lt;ngx_core.h&gt;
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5017 #include &lt;ngx_http.h&gt;
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5018
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5019
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5020 typedef struct {
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5021 off_t count;
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5022 } ngx_http_counter_filter_ctx_t;
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5023
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5024
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5025 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
5026 ngx_chain_t *in);
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5027 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
5028 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
5029 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
5030 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
5031
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5032
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5033 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
5034 ngx_http_counter_add_variables, /* preconfiguration */
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5035 ngx_http_counter_filter_init, /* postconfiguration */
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5036
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5037 NULL, /* create main configuration */
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5038 NULL, /* init main configuration */
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5039
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5040 NULL, /* create server configuration */
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5041 NULL, /* merge server configuration */
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5042
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5043 NULL, /* create location configuration */
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5044 NULL /* merge location configuration */
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5045 };
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5046
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5047
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5048 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
5049 NGX_MODULE_V1,
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5050 &amp;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
5051 NULL, /* module directives */
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5052 NGX_HTTP_MODULE, /* module type */
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5053 NULL, /* init master */
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5054 NULL, /* init module */
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5055 NULL, /* init process */
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5056 NULL, /* init thread */
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5057 NULL, /* exit thread */
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5058 NULL, /* exit process */
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5059 NULL, /* exit master */
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5060 NGX_MODULE_V1_PADDING
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5061 };
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5062
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5063
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5064 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
5065
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5066 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
5067
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5068
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5069 static ngx_int_t
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5070 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
5071 {
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5072 ngx_chain_t *cl;
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5073 ngx_http_counter_filter_ctx_t *ctx;
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5074
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5075 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
5076 if (ctx == NULL) {
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5077 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
5078 if (ctx == NULL) {
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5079 return NGX_ERROR;
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5080 }
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5081
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5082 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
5083 }
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5084
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5085 for (cl = in; cl; cl = cl->next) {
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5086 ctx->count += ngx_buf_size(cl->buf);
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5087 }
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5088
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5089 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
5090 }
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5091
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5092
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5093 static ngx_int_t
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5094 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
5095 uintptr_t data)
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5096 {
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5097 u_char *p;
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5098 ngx_http_counter_filter_ctx_t *ctx;
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5099
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5100 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
5101 if (ctx == NULL) {
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5102 v->not_found = 1;
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5103 return NGX_OK;
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5104 }
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5105
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5106 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
5107 if (p == NULL) {
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5108 return NGX_ERROR;
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5109 }
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5110
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5111 v->data = p;
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5112 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
5113 v->valid = 1;
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5114 v->no_cacheable = 0;
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5115 v->not_found = 0;
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5116
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5117 return NGX_OK;
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5118 }
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5119
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5120
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5121 static ngx_int_t
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5122 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
5123 {
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5124 ngx_http_variable_t *var;
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5125
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5126 var = ngx_http_add_variable(cf, &amp;ngx_http_counter_name, 0);
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5127 if (var == NULL) {
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5128 return NGX_ERROR;
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5129 }
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5130
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5131 var->get_handler = ngx_http_counter_variable;
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5132
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5133 return NGX_OK;
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5134 }
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5135
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5136
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5137 static ngx_int_t
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5138 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
5139 {
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5140 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
5141 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
5142
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5143 return NGX_OK;
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5144 }
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5145 </programlisting>
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5146
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5147 </section>
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5148
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5149
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5150 <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
5151
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5152 <para>
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5153 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
5154 filters order.
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5155 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
5156 modules.
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5157 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
5158 other filters.
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5159 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
5160 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
5161 registered.
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5162 </para>
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5163
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5164 <para>
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5165 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
5166 modules is provided by nginx.
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5167 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
5168 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
5169 module's configuration.
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5170 </para>
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5171
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5172 <para>
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5173 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
5174 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
5175 </para>
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5176
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5177 <programlisting>
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5178 ngx_module_type=HTTP_AUX_FILTER
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5179 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
5180 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
5181
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5182 . auto/module
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5183 </programlisting>
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5184
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5185 </section>
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5186
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5187
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5188 <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
5189
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5190 <para>
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5191 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
5192 allocated buffers.
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5193 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
5194 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
5195 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
5196 These buffers can be reused.
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5197 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
5198 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
5199 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
5200 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
5201 (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
5202 <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
5203 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
5204 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
5205 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
5206 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
5207 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
5208 <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
5209 this.
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5210 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
5211 <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
5212 <literal>busy</literal> to <literal>free</literal>.
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5213 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
5214 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
5215 </para>
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5216
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5217 <para>
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5218 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
5219 incoming buffer.
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5220 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
5221 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
5222 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
5223 beyond the scope of this section.
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5224 </para>
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5225
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5226 <programlisting>
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5227 typedef struct {
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5228 ngx_chain_t *free;
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5229 ngx_chain_t *busy;
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5230 } ngx_http_foo_filter_ctx_t;
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5231
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5232
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5233 ngx_int_t
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5234 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
5235 {
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5236 ngx_int_t rc;
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5237 ngx_buf_t *b;
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5238 ngx_chain_t *cl, *tl, *out, **ll;
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5239 ngx_http_foo_filter_ctx_t *ctx;
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5240
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5241 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
5242 if (ctx == NULL) {
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5243 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
5244 if (ctx == NULL) {
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5245 return NGX_ERROR;
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5246 }
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5247
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5248 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
5249 }
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5250
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5251 /* 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
5252
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5253 ll = &amp;out;
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5254
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5255 for (cl = in; cl; cl = cl->next) {
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5256
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5257 /* 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
5258
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5259 tl = ngx_chain_get_free_buf(r->pool, &amp;ctx->free);
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5260 if (tl == NULL) {
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5261 return NGX_ERROR;
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5262 }
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5263
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5264 b = tl->buf;
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5265 b->tag = (ngx_buf_tag_t) &amp;ngx_http_foo_filter_module;
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5266 b->memory = 1;
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5267 b->pos = (u_char *) "foo";
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5268 b->last = b->pos + 3;
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5269
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5270 *ll = tl;
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5271 ll = &amp;tl->next;
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5272
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5273 /* append the next incoming buffer */
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5274
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5275 tl = ngx_alloc_chain_link(r->pool);
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5276 if (tl == NULL) {
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5277 return NGX_ERROR;
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5278 }
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5279
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5280 tl->buf = cl->buf;
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5281 *ll = tl;
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5282 ll = &amp;tl->next;
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5283 }
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5284
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5285 *ll = NULL;
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5286
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5287 /* send the new chain */
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5288
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5289 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
5290
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5291 /* update "busy" and "free" chains for reuse */
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5292
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5293 ngx_chain_update_chains(r->pool, &amp;ctx->free, &amp;ctx->busy, &amp;out,
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5294 (ngx_buf_tag_t) &amp;ngx_http_foo_filter_module);
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5295
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5296 return rc;
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5297 }
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5298 </programlisting>
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5299
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5300 </section>
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5301
9550ea66abdd HTTP response section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1959
diff changeset
5302
1939
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5303 <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
5304
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5305 <para>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5306 The
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5307 <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
5308 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
5309 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
5310 such as HTTP or FastCGI.
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5311 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
5312 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
5313 </para>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5314
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5315 <para>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5316 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
5317 <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
5318 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
5319 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
5320 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
5321 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
5322 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
5323 </para>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5324
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5325 <para>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5326 The
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5327 <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
5328 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
5329 <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
5330 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
5331 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
5332 for example,
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5333 <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
5334 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
5335 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
5336 <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
5337 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
5338 <programlisting>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5339 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
5340 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
5341 void **srv_conf;
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5342
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5343 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
5344
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5345 ngx_uint_t flags;
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5346 ngx_str_t host;
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5347 u_char *file_name;
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5348 ngx_uint_t line;
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5349 in_port_t port;
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5350 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
5351
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5352 #if (NGX_HTTP_UPSTREAM_ZONE)
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5353 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
5354 #endif
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5355 };
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5356 </programlisting>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5357
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5358 <list type="bullet">
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5359
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5360 <listitem>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5361 <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
5362 </listitem>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5363
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5364 <listitem>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5365 <literal>servers</literal> — array of
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5366 <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
5367 <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
5368 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
5369 </listitem>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5370
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5371 <listitem>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5372 <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
5373 (configured as parameters of
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5374 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
5375 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
5376
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5377 <list type="bullet">
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5378
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5379 <listitem>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5380 <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
5381 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
5382 <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
5383 (FastCGI, SCGI, etc.)
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5384 </listitem>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5385
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5386 <listitem>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5387 <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
5388 is supported
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5389 </listitem>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5390
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5391 <listitem>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5392 <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
5393 is supported
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5394 </listitem>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5395
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5396 <listitem>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5397 <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
5398 “<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
5399 </listitem>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5400
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5401 <listitem>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5402 <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
5403 is supported
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5404 </listitem>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5405
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5406 <listitem>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5407 <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
5408 is supported
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5409 </listitem>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5410
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5411 <listitem>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5412 <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
5413 is supported
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5414 </listitem>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5415
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5416 </list>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5417
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5418 </listitem>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5419
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5420 <listitem>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5421 <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
5422 </listitem>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5423
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5424 <listitem>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5425 <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
5426 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
5427 </listitem>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5428
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5429 <listitem>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5430 <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
5431 upstreams
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5432 </listitem>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5433
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5434 <listitem>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5435 <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
5436 </listitem>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5437
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5438 <listitem>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5439 <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
5440 initializing upstream configuration:
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5441
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5442 <programlisting>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5443 typedef struct {
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5444 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
5445 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
5446 void *data;
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5447 } ngx_http_upstream_peer_t;
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5448 </programlisting>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5449 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
5450 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
5451 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
5452 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
5453 <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
5454
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5455 <list type="bullet">
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5456 <listitem>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5457 <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
5458 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
5459 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
5460 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
5461 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
5462 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
5463 </listitem>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5464
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5465 <listitem>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5466 <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
5467 <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
5468 <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
5469 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
5470 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
5471 deal with server selection.
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5472 </listitem>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5473 </list>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5474
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5475 </listitem>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5476 </list>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5477 </para>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5478
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5479 <para>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5480 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
5481 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
5482 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
5483 <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
5484 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
5485 <programlisting>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5486 struct ngx_peer_connection_s {
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5487 [...]
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5488
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5489 struct sockaddr *sockaddr;
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5490 socklen_t socklen;
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5491 ngx_str_t *name;
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5492
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5493 ngx_uint_t tries;
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5494
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5495 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
5496 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
5497 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
5498 void *data;
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5499
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5500 #if (NGX_SSL || NGX_COMPAT)
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5501 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
5502 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
5503 #endif
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5504
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5505 [..]
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5506 };
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5507 </programlisting>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5508
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5509 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
5510
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5511 <list type="bullet">
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5512 <listitem>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5513 <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
5514 <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
5515 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
5516 </listitem>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5517
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5518 <listitem>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5519 <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
5520 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
5521 configuration.
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5522 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
5523 (see below)
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5524 </listitem>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5525
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5526 <listitem>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5527 <literal>tries</literal> — allowed
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5528 <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
5529 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
5530 </listitem>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5531
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5532 <listitem>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5533 <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
5534 <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
5535 - 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
5536 </listitem>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5537
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5538 </list>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5539
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5540 </para>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5541
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5542 <para>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5543 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
5544 <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
5545 <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
5546 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
5547 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
5548 </para>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5549
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5550 <para>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5551
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5552 <list type="bullet">
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5553 <listitem>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5554 <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
5555 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
5556 its address.
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5557 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
5558 <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
5559 <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
5560 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
5561
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5562 <list type="bullet">
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5563
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5564 <listitem>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5565 <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
5566 </listitem>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5567
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5568 <listitem>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5569 <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
5570 </listitem>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5571
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5572 <listitem>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5573 <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
5574 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
5575 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
5576 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
5577 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
5578 </listitem>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5579
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5580 <listitem>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5581 <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
5582 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
5583 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
5584 </listitem>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5585
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5586 <!--
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5587 <listitem>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5588 <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
5589 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
5590 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
5591 </listitem>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5592 -->
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5593
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5594 </list>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5595
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5596 </listitem>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5597
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5598 <listitem>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5599 <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
5600 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
5601 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
5602 completion.
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5603 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
5604 <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
5605 <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
5606 <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
5607 (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
5608 <literal>NGX_PEER_KEEPALIVE</literal>.
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5609 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
5610 </listitem>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5611
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5612 <listitem>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5613 <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
5614 in the OSS version.
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5615 </listitem>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5616
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5617 <listitem>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5618 <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
5619 <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
5620 — 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
5621 servers.
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5622 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
5623 </listitem>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5624
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5625 </list>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5626
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5627 </para>
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5628
1929
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
5629 </section>
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
5630
1939
c1b0169e8f54 Added the "Load balancing" section of the development guide.
Vladimir Homutov <vl@nginx.com>
parents: 1936
diff changeset
5631 </section>
1929
7f290929b32d HTTP section of the development guide.
Roman Arutyunyan <arut@nginx.com>
parents: 1928
diff changeset
5632
1899
b86cfece30c3 Added development guide.
Vladimir Homutov <vl@nginx.com>
parents:
diff changeset
5633 </article>