Mercurial > hg > nginx-site
comparison xml/en/docs/http/ngx_http_rewrite_module.xml @ 966:95c3c3bbf1ce
Text review.
author | Egor Nikitin <yegor.nikitin@gmail.com> |
---|---|
date | Wed, 14 Aug 2013 12:03:41 +0400 |
parents | 9b549f369a36 |
children | cb63563024eb |
comparison
equal
deleted
inserted
replaced
965:fadccc156188 | 966:95c3c3bbf1ce |
---|---|
13 rev="4"> | 13 rev="4"> |
14 | 14 |
15 <section id="summary"> | 15 <section id="summary"> |
16 | 16 |
17 <para> | 17 <para> |
18 The <literal>ngx_http_rewrite_module</literal> module allows to | 18 The <literal>ngx_http_rewrite_module</literal> module is used to |
19 change URIs using regular expressions, return redirects, and | 19 change URIs using regular expressions, return redirects, and |
20 conditionally select configurations. | 20 conditionally select configurations. |
21 </para> | 21 </para> |
22 | 22 |
23 <para> | 23 <para> |
34 a location for a request is searched; | 34 a location for a request is searched; |
35 </listitem> | 35 </listitem> |
36 | 36 |
37 <listitem> | 37 <listitem> |
38 the directives of this module specified in the selected | 38 the directives of this module specified in the selected |
39 <link doc="ngx_http_core_module.xml" id="location"/> are processed, | 39 <link doc="ngx_http_core_module.xml" id="location"/> are processed. |
40 and if they changed a URI, a new location is searched for the new URI. | 40 If they have been changing a URI, a new location is searched for the new URI. |
41 This cycle may be repeated up to | 41 This cycle may be repeated up to |
42 <link doc="ngx_http_core_module.xml" id="internal">10 times</link> | 42 <link doc="ngx_http_core_module.xml" id="internal">10 times</link> |
43 after which the error <http-status code="500" text="Internal Server Error"/> | 43 after which the <http-status code="500" text="Internal Server Error"/> error |
44 is returned. | 44 is returned. |
45 </listitem> | 45 </listitem> |
46 | 46 |
47 </list> | 47 </list> |
48 </para> | 48 </para> |
83 <context>server</context> | 83 <context>server</context> |
84 <context>location</context> | 84 <context>location</context> |
85 | 85 |
86 <para> | 86 <para> |
87 The specified <value>condition</value> is evaluated. | 87 The specified <value>condition</value> is evaluated. |
88 If true, the directives of this module specified inside the braces are | 88 If true, this module directives specified inside the braces are |
89 executed, and a request is assigned the configuration inside the | 89 executed, and the request is assigned the configuration inside the |
90 <literal>if</literal> directive. | 90 <literal>if</literal> directive. |
91 Configurations inside the <literal>if</literal> directives are | 91 Configurations inside the <literal>if</literal> directives are |
92 inherited from the previous configuration level. | 92 inherited from the previous configuration level. |
93 </para> | 93 </para> |
94 | 94 |
104 was considered a false value. | 104 was considered a false value. |
105 </note> | 105 </note> |
106 </listitem> | 106 </listitem> |
107 | 107 |
108 <listitem> | 108 <listitem> |
109 comparing a variable with a string using the | 109 comparison of a variable with a string using the |
110 “<literal>=</literal>” and “<literal>!=</literal>” operators; | 110 “<literal>=</literal>” and “<literal>!=</literal>” operators; |
111 </listitem> | 111 </listitem> |
112 | 112 |
113 <listitem> | 113 <listitem> |
114 matching a variable against a regular expression using the | 114 matching of a variable against a regular expression using the |
115 “<literal>~</literal>” (for case-sensitive matching) and | 115 “<literal>~</literal>” (for case-sensitive matching) and |
116 “<literal>~*</literal>” (for case-insensitive matching) operators. | 116 “<literal>~*</literal>” (for case-insensitive matching) operators. |
117 Regular expressions can contain captures that are made available for | 117 Regular expressions can contain captures that are made available for |
118 later reuse in the <var>$1</var>..<var>$9</var> variables. | 118 later reuse in the <var>$1</var>..<var>$9</var> variables. |
119 Negative operators “<literal>!~</literal>” and “<literal>!~*</literal>” | 119 Negative operators “<literal>!~</literal>” and “<literal>!~*</literal>” |
120 are also available. | 120 are also available. |
121 If a regular expression includes the characters “<literal>}</literal>” | 121 If a regular expression includes the “<literal>}</literal>” |
122 or “<literal>;</literal>”, the whole expression should be enclosed | 122 or “<literal>;</literal>” characters, the whole expressions should be enclosed |
123 in single or double quotes. | 123 in single or double quotes. |
124 </listitem> | 124 </listitem> |
125 | 125 |
126 <listitem> | 126 <listitem> |
127 checking a file existence with the “<literal>-f</literal>” and | 127 checking of a file existence with the “<literal>-f</literal>” and |
128 “<literal>!-f</literal>” operators; | 128 “<literal>!-f</literal>” operators; |
129 </listitem> | 129 </listitem> |
130 | 130 |
131 <listitem> | 131 <listitem> |
132 checking a directory existence with the “<literal>-d</literal>” and | 132 checking of a directory existence with the “<literal>-d</literal>” and |
133 “<literal>!-d</literal>” operators; | 133 “<literal>!-d</literal>” operators; |
134 </listitem> | 134 </listitem> |
135 | 135 |
136 <listitem> | 136 <listitem> |
137 checking a file, directory, or symbolic link existence with the | 137 checking of a file, directory, or symbolic link existence with the |
138 “<literal>-e</literal>” and “<literal>!-e</literal>” operators; | 138 “<literal>-e</literal>” and “<literal>!-e</literal>” operators; |
139 </listitem> | 139 </listitem> |
140 | 140 |
141 <listitem> | 141 <listitem> |
142 checking for an executable file with operators “<literal>-x</literal>” | 142 checking for an executable file with the “<literal>-x</literal>” |
143 and “<literal>!-x</literal>” operators. | 143 and “<literal>!-x</literal>” operators. |
144 </listitem> | 144 </listitem> |
145 | 145 |
146 </list> | 146 </list> |
147 </para> | 147 </para> |
168 if ($invalid_referer) { | 168 if ($invalid_referer) { |
169 return 403; | 169 return 403; |
170 } | 170 } |
171 </example> | 171 </example> |
172 <note> | 172 <note> |
173 A value of the embedded variable <var>$invalid_referer</var> is set by the | 173 A value of the <var>$invalid_referer</var> embedded variable is set by the |
174 <link doc="ngx_http_referer_module.xml" id="valid_referers"/> directive. | 174 <link doc="ngx_http_referer_module.xml" id="valid_referers"/> directive. |
175 </note> | 175 </note> |
176 </para> | 176 </para> |
177 | 177 |
178 </directive> | 178 </directive> |
193 a response header. | 193 a response header. |
194 </para> | 194 </para> |
195 | 195 |
196 <para> | 196 <para> |
197 Starting from version 0.8.42, it is possible to specify | 197 Starting from version 0.8.42, it is possible to specify |
198 either the URL of redirect (for codes 301, 302, 303, and 307), | 198 either a redirect URL (for codes 301, 302, 303, and 307), |
199 or the <value>text</value> of a response body (for other codes). | 199 or the response body <value>text</value> (for other codes). |
200 A response body text or URL of redirect can contain variables. | 200 A response body text and redirect URL can contain variables. |
201 As a special case, a URL of redirect can be specified as a URI | 201 As a special case, a redirect URL can be specified as a URI |
202 local to this server, in which case the full URL of redirect | 202 local to this server, in which case the full redirect URL |
203 is formed according to the request scheme (<var>$scheme</var>) and the | 203 is formed according to the request scheme (<var>$scheme</var>) and the |
204 <link doc="ngx_http_core_module.xml" id="server_name_in_redirect"/> and | 204 <link doc="ngx_http_core_module.xml" id="server_name_in_redirect"/> and |
205 <link doc="ngx_http_core_module.xml" id="port_in_redirect"/> directives. | 205 <link doc="ngx_http_core_module.xml" id="port_in_redirect"/> directives. |
206 </para> | 206 </para> |
207 | 207 |
208 <para> | 208 <para> |
209 In addition, a <value>URL</value> for temporary redirect with the code 302 | 209 In addition, a <value>URL</value> for temporary redirect with the code 302 |
210 can be specified as the sole parameter. | 210 can be specified as the sole parameter. |
211 Such a parameter should start with the string “<literal>http://</literal>”, | 211 Such a parameter should start with the “<literal>http://</literal>”, |
212 “<literal>https://</literal>”, or “<literal>$scheme</literal>”. | 212 “<literal>https://</literal>”, or “<literal>$scheme</literal>” string. |
213 A <value>URL</value> can contain variables. | 213 A <value>URL</value> can contain variables. |
214 </para> | 214 </para> |
215 | 215 |
216 <para> | 216 <para> |
217 <note> | 217 <note> |
240 <para> | 240 <para> |
241 If the specified regular expression matches a URI, the URI is changed | 241 If the specified regular expression matches a URI, the URI is changed |
242 as specified in the <value>replacement</value> string. | 242 as specified in the <value>replacement</value> string. |
243 The <literal>rewrite</literal> directives are executed sequentially | 243 The <literal>rewrite</literal> directives are executed sequentially |
244 in order of their appearance in the configuration file. | 244 in order of their appearance in the configuration file. |
245 Flags make it possible to terminate further processing of directives. | 245 It is possible to terminate further processing of the directives using flags. |
246 If a replacement string starts with “<literal>http://</literal>” | 246 If a replacement string starts with “<literal>http://</literal>” |
247 or “<literal>https://</literal>”, the processing stops and a | 247 or “<literal>https://</literal>”, the processing stops and the |
248 redirect is returned to a client. | 248 redirect is returned to a client. |
249 </para> | 249 </para> |
250 | 250 |
251 <para> | 251 <para> |
252 An optional <value>flag</value> parameter can be one of: | 252 An optional <value>flag</value> parameter can be one of: |
253 <list type="tag"> | 253 <list type="tag"> |
254 | 254 |
255 <tag-name><literal>last</literal></tag-name> | 255 <tag-name><literal>last</literal></tag-name> |
256 <tag-desc> | 256 <tag-desc> |
257 stops processing the current set of | 257 stops processing the current set of |
258 <literal>ngx_http_rewrite_module</literal> directives | 258 <literal>ngx_http_rewrite_module</literal> directives and starts |
259 followed by a search for a new location matching the changed URI; | 259 a search for a new location matching the changed URI; |
260 </tag-desc> | 260 </tag-desc> |
261 | 261 |
262 <tag-name><literal>break</literal></tag-name> | 262 <tag-name><literal>break</literal></tag-name> |
263 <tag-desc> | 263 <tag-desc> |
264 stops processing the current set of | 264 stops processing the current set of |
265 <literal>ngx_http_rewrite_module</literal> directives; | 265 <literal>ngx_http_rewrite_module</literal> directives; |
266 </tag-desc> | 266 </tag-desc> |
267 | 267 |
268 <tag-name><literal>redirect</literal></tag-name> | 268 <tag-name><literal>redirect</literal></tag-name> |
269 <tag-desc> | 269 <tag-desc> |
270 returns a temporary redirect with the code 302; | 270 returns a temporary redirect with the 302 code; |
271 used if a replacement string does not start with | 271 used if a replacement string does not start with |
272 “<literal>http://</literal>” or “<literal>https://</literal>”; | 272 “<literal>http://</literal>” or “<literal>https://</literal>”; |
273 </tag-desc> | 273 </tag-desc> |
274 | 274 |
275 <tag-name><literal>permanent</literal></tag-name> | 275 <tag-name><literal>permanent</literal></tag-name> |
276 <tag-desc> | 276 <tag-desc> |
277 returns a permanent redirect with the code 301. | 277 returns a permanent redirect with the 301 code. |
278 </tag-desc> | 278 </tag-desc> |
279 | 279 |
280 </list> | 280 </list> |
281 The full URL of redirects is formed according to the | 281 The full redirect URL is formed according to the |
282 request scheme (<var>$scheme</var>) and the | 282 request scheme (<var>$scheme</var>) and the |
283 <link doc="ngx_http_core_module.xml" id="server_name_in_redirect"/> and | 283 <link doc="ngx_http_core_module.xml" id="server_name_in_redirect"/> and |
284 <link doc="ngx_http_core_module.xml" id="port_in_redirect"/> directives. | 284 <link doc="ngx_http_core_module.xml" id="port_in_redirect"/> directives. |
285 </para> | 285 </para> |
286 | 286 |
298 </para> | 298 </para> |
299 | 299 |
300 <para> | 300 <para> |
301 But if these directives are put inside the “<literal>/download/</literal>” | 301 But if these directives are put inside the “<literal>/download/</literal>” |
302 location, the <literal>last</literal> flag should be replaced by | 302 location, the <literal>last</literal> flag should be replaced by |
303 <literal>break</literal>, otherwise nginx will make 10 cycles and | 303 <literal>break</literal>, or otherwise nginx will make 10 cycles and |
304 return the error 500: | 304 return the 500 error: |
305 <example> | 305 <example> |
306 location /download/ { | 306 location /download/ { |
307 rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 break; | 307 rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 break; |
308 rewrite ^(/download/.*)/audio/(.*)\..*$ $1/mp3/$2.ra break; | 308 rewrite ^(/download/.*)/audio/(.*)\..*$ $1/mp3/$2.ra break; |
309 return 403; | 309 return 403; |
320 rewrite ^/users/(.*)$ /show?user=$1? last; | 320 rewrite ^/users/(.*)$ /show?user=$1? last; |
321 </example> | 321 </example> |
322 </para> | 322 </para> |
323 | 323 |
324 <para> | 324 <para> |
325 If a regular expression includes the characters “<literal>}</literal>” | 325 If a regular expression includes the “<literal>}</literal>” |
326 or “<literal>;</literal>”, the whole expression should be enclosed | 326 or “<literal>;</literal>” characters, the whole expressions should be enclosed |
327 in single or double quotes. | 327 in single or double quotes. |
328 </para> | 328 </para> |
329 | 329 |
330 </directive> | 330 </directive> |
331 | 331 |
382 | 382 |
383 <section id="internals" name="Internal Implementation"> | 383 <section id="internals" name="Internal Implementation"> |
384 | 384 |
385 <para> | 385 <para> |
386 The <literal>ngx_http_rewrite_module</literal> module directives | 386 The <literal>ngx_http_rewrite_module</literal> module directives |
387 are compiled during the configuration stage into internal instructions | 387 are compiled at the configuration stage into internal instructions |
388 that are interpreted during request processing. | 388 that are interpreted during request processing. |
389 An interpreter is a simple virtual stack machine. | 389 An interpreter is a simple virtual stack machine. |
390 </para> | 390 </para> |
391 | 391 |
392 <para> | 392 <para> |