Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
nginx
GitHub Repository: nginx/nginx.org
Path: blob/main/xml/en/docs/http/ngx_http_rewrite_module.xml
1 views
1
<?xml version="1.0"?>
2
3
<!--
4
Copyright (C) Igor Sysoev
5
Copyright (C) Nginx, Inc.
6
-->
7
8
<!DOCTYPE module SYSTEM "../../../../dtd/module.dtd">
9
10
<module name="Module ngx_http_rewrite_module"
11
link="/en/docs/http/ngx_http_rewrite_module.html"
12
lang="en"
13
rev="9">
14
15
<section id="summary">
16
17
<para>
18
The <literal>ngx_http_rewrite_module</literal> module is used to
19
change request URI using PCRE regular expressions, return redirects, and
20
conditionally select configurations.
21
</para>
22
23
<para>
24
The <link id="break"/>, <link id="if"/>, <link id="return"/>,
25
<link id="rewrite"/>, and <link id="set"/> directives are
26
processed in the following order:
27
<list type="bullet">
28
29
<listitem>
30
the directives of this module specified on the
31
<link doc="ngx_http_core_module.xml" id="server"/> level
32
are executed sequentially;
33
</listitem>
34
35
<listitem>
36
repeatedly:
37
<list type="bullet">
38
39
<listitem>
40
a
41
<link doc="ngx_http_core_module.xml" id="location"/>
42
is searched based on a request URI;
43
</listitem>
44
45
<listitem>
46
the directives of this module specified inside the found location
47
are executed sequentially;
48
</listitem>
49
50
<listitem>
51
the loop is repeated if a request URI was <link id="rewrite">rewritten</link>,
52
but not more than
53
<link doc="ngx_http_core_module.xml" id="internal">10 times</link>.
54
</listitem>
55
56
</list>
57
</listitem>
58
59
</list>
60
</para>
61
62
</section>
63
64
65
<section id="directives" name="Directives">
66
67
<directive name="break">
68
<syntax/>
69
<default/>
70
<context>server</context>
71
<context>location</context>
72
<context>if</context>
73
74
<para>
75
Stops processing the current set of
76
<literal>ngx_http_rewrite_module</literal> directives.
77
</para>
78
79
<para>
80
If a directive is specified inside the
81
<link doc="ngx_http_core_module.xml" id="location"/>,
82
further processing of the request continues in this location.
83
</para>
84
85
<para>
86
Example:
87
<example>
88
if ($slow) {
89
limit_rate 10k;
90
break;
91
}
92
</example>
93
</para>
94
95
</directive>
96
97
98
<directive name="if">
99
<syntax block="yes">(<value>condition</value>)</syntax>
100
<default/>
101
<context>server</context>
102
<context>location</context>
103
104
<para>
105
The specified <value>condition</value> is evaluated.
106
If true, this module directives specified inside the braces are
107
executed, and the request is assigned the configuration inside the
108
<literal>if</literal> directive.
109
Configurations inside the <literal>if</literal> directives are
110
inherited from the previous configuration level.
111
</para>
112
113
<para>
114
A condition may be any of the following:
115
<list type="bullet">
116
117
<listitem>
118
a variable name; false if the value of a variable is an empty string
119
or “<literal>0</literal>”;
120
<note>
121
Before version 1.0.1, any string starting with “<literal>0</literal>
122
was considered a false value.
123
</note>
124
</listitem>
125
126
<listitem>
127
comparison of a variable with a string using the
128
<literal>=</literal>” and “<literal>!=</literal>” operators;
129
</listitem>
130
131
<listitem>
132
matching of a variable against a regular expression using the
133
<literal>~</literal>” (for case-sensitive matching) and
134
<literal>~*</literal>” (for case-insensitive matching) operators.
135
Regular expressions can contain captures that are made available for
136
later reuse in the <var>$1</var>..<var>$9</var> variables.
137
Negative operators “<literal>!~</literal>” and “<literal>!~*</literal>
138
are also available.
139
If a regular expression includes the “<literal>}</literal>
140
or “<literal>;</literal>” characters, the whole expressions should be enclosed
141
in single or double quotes.
142
</listitem>
143
144
<listitem>
145
checking of a file existence with the “<literal>-f</literal>” and
146
<literal>!-f</literal>” operators;
147
</listitem>
148
149
<listitem>
150
checking of a directory existence with the “<literal>-d</literal>” and
151
<literal>!-d</literal>” operators;
152
</listitem>
153
154
<listitem>
155
checking of a file, directory, or symbolic link existence with the
156
<literal>-e</literal>” and “<literal>!-e</literal>” operators;
157
</listitem>
158
159
<listitem>
160
checking for an executable file with the “<literal>-x</literal>
161
and “<literal>!-x</literal>” operators.
162
</listitem>
163
164
</list>
165
</para>
166
167
<para>
168
Examples:
169
<example>
170
if ($http_user_agent ~ MSIE) {
171
rewrite ^(.*)$ /msie/$1 break;
172
}
173
174
if ($http_cookie ~* "id=([^;]+)(?:;|$)") {
175
set $id $1;
176
}
177
178
if ($request_method = POST) {
179
return 405;
180
}
181
182
if ($slow) {
183
limit_rate 10k;
184
}
185
186
if ($invalid_referer) {
187
return 403;
188
}
189
</example>
190
<note>
191
A value of the <var>$invalid_referer</var> embedded variable is set by the
192
<link doc="ngx_http_referer_module.xml" id="valid_referers"/> directive.
193
</note>
194
</para>
195
196
</directive>
197
198
199
<directive name="return">
200
<syntax><value>code</value> [<value>text</value>]</syntax>
201
<syntax><value>code</value> <value>URL</value></syntax>
202
<syntax><value>URL</value></syntax>
203
<default/>
204
<context>server</context>
205
<context>location</context>
206
<context>if</context>
207
208
<para>
209
Stops processing and returns the specified <value>code</value> to a client.
210
The non-standard code 444 closes a connection without sending
211
a response header.
212
</para>
213
214
<para>
215
Starting from version 0.8.42, it is possible to specify
216
either a redirect URL (for codes 301, 302, 303, 307, and 308)
217
or the response body <value>text</value> (for other codes).
218
A response body text and redirect URL can contain variables.
219
As a special case, a redirect URL can be specified as a URI
220
local to this server, in which case the full redirect URL
221
is formed according to the request scheme (<var>$scheme</var>) and the
222
<link doc="ngx_http_core_module.xml" id="server_name_in_redirect"/> and
223
<link doc="ngx_http_core_module.xml" id="port_in_redirect"/> directives.
224
</para>
225
226
<para>
227
In addition, a <value>URL</value> for temporary redirect with the code 302
228
can be specified as the sole parameter.
229
Such a parameter should start with the “<literal>http://</literal>”,
230
<literal>https://</literal>”, or “<literal>$scheme</literal>” string.
231
A <value>URL</value> can contain variables.
232
</para>
233
234
<para>
235
<note>
236
Only the following codes could be returned before version 0.7.51:
237
204, 400, 402 — 406, 408, 410, 411, 413, 416, and 500 — 504.
238
</note>
239
240
<note>
241
The code 307 was not treated as a redirect until versions 1.1.16 and 1.0.13.
242
</note>
243
244
<note>
245
The code 308 was not treated as a redirect until version 1.13.0.
246
</note>
247
</para>
248
249
<para>
250
See also the <link doc="ngx_http_core_module.xml" id="error_page"/> directive.
251
</para>
252
253
</directive>
254
255
256
<directive name="rewrite">
257
<syntax>
258
<value>regex</value>
259
<value>replacement</value>
260
[<value>flag</value>]</syntax>
261
<default/>
262
<context>server</context>
263
<context>location</context>
264
<context>if</context>
265
266
<para>
267
If the specified regular expression matches a request URI, URI is changed
268
as specified in the <value>replacement</value> string.
269
The <literal>rewrite</literal> directives are executed sequentially
270
in order of their appearance in the configuration file.
271
It is possible to terminate further processing of the directives using flags.
272
If a replacement string starts with “<literal>http://</literal>”,
273
<literal>https://</literal>”, or “<literal>$scheme</literal>”,
274
the processing stops and the redirect is returned to a client.
275
</para>
276
277
<para>
278
An optional <value>flag</value> parameter can be one of:
279
<list type="tag">
280
281
<tag-name><literal>last</literal></tag-name>
282
<tag-desc>
283
stops processing the current set of
284
<literal>ngx_http_rewrite_module</literal> directives and starts
285
a search for a new location matching the changed URI;
286
</tag-desc>
287
288
<tag-name><literal>break</literal></tag-name>
289
<tag-desc>
290
stops processing the current set of
291
<literal>ngx_http_rewrite_module</literal> directives
292
as with the <link id="break"/> directive;
293
</tag-desc>
294
295
<tag-name><literal>redirect</literal></tag-name>
296
<tag-desc>
297
returns a temporary redirect with the 302 code;
298
used if a replacement string does not start with
299
<literal>http://</literal>”, “<literal>https://</literal>”,
300
or “<literal>$scheme</literal>”;
301
</tag-desc>
302
303
<tag-name><literal>permanent</literal></tag-name>
304
<tag-desc>
305
returns a permanent redirect with the 301 code.
306
</tag-desc>
307
308
</list>
309
The full redirect URL is formed according to the
310
request scheme (<var>$scheme</var>) and the
311
<link doc="ngx_http_core_module.xml" id="server_name_in_redirect"/> and
312
<link doc="ngx_http_core_module.xml" id="port_in_redirect"/> directives.
313
</para>
314
315
<para>
316
Example:
317
<example>
318
server {
319
...
320
rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 last;
321
rewrite ^(/download/.*)/audio/(.*)\..*$ $1/mp3/$2.ra last;
322
return 403;
323
...
324
}
325
</example>
326
</para>
327
328
<para>
329
But if these directives are put inside the “<literal>/download/</literal>
330
location, the <literal>last</literal> flag should be replaced by
331
<literal>break</literal>, or otherwise nginx will make 10 cycles and
332
return the 500 error:
333
<example>
334
location /download/ {
335
rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 break;
336
rewrite ^(/download/.*)/audio/(.*)\..*$ $1/mp3/$2.ra break;
337
return 403;
338
}
339
</example>
340
</para>
341
342
<para>
343
If a <value>replacement</value> string includes the new request arguments,
344
the previous request arguments are appended after them.
345
If this is undesired, putting a question mark at the end of a replacement
346
string avoids having them appended, for example:
347
<example>
348
rewrite ^/users/(.*)$ /show?user=$1? last;
349
</example>
350
</para>
351
352
<para>
353
If a regular expression includes the “<literal>}</literal>
354
or “<literal>;</literal>” characters, the whole expressions should be enclosed
355
in single or double quotes.
356
</para>
357
358
</directive>
359
360
361
<directive name="rewrite_log">
362
<syntax><literal>on</literal> | <literal>off</literal></syntax>
363
<default>off</default>
364
<context>http</context>
365
<context>server</context>
366
<context>location</context>
367
<context>if</context>
368
369
<para>
370
Enables or disables logging of <literal>ngx_http_rewrite_module</literal>
371
module directives processing results
372
into the <link doc="../ngx_core_module.xml" id="error_log"/> at
373
the <literal>notice</literal> level.
374
</para>
375
376
</directive>
377
378
379
<directive name="set">
380
<syntax><value>$variable</value> <value>value</value></syntax>
381
<default/>
382
<context>server</context>
383
<context>location</context>
384
<context>if</context>
385
386
<para>
387
Sets a <value>value</value> for the specified <value>variable</value>.
388
The <value>value</value> can contain text, variables, and their combination.
389
</para>
390
391
</directive>
392
393
394
<directive name="uninitialized_variable_warn">
395
<syntax><literal>on</literal> | <literal>off</literal></syntax>
396
<default>on</default>
397
<context>http</context>
398
<context>server</context>
399
<context>location</context>
400
<context>if</context>
401
402
<para>
403
Controls whether warnings about uninitialized variables are logged.
404
</para>
405
406
</directive>
407
408
</section>
409
410
411
<section id="internals" name="Internal Implementation">
412
413
<para>
414
The <literal>ngx_http_rewrite_module</literal> module directives
415
are compiled at the configuration stage into internal instructions
416
that are interpreted during request processing.
417
An interpreter is a simple virtual stack machine.
418
</para>
419
420
<para>
421
For example, the directives
422
<example>
423
location /download/ {
424
if ($forbidden) {
425
return 403;
426
}
427
428
if ($slow) {
429
limit_rate 10k;
430
}
431
432
rewrite ^/(download/.*)/media/(.*)\..*$ /$1/mp3/$2.mp3 break;
433
}
434
</example>
435
will be translated into these instructions:
436
<example>
437
variable $forbidden
438
check against zero
439
return 403
440
end of code
441
variable $slow
442
check against zero
443
match of regular expression
444
copy "/"
445
copy $1
446
copy "/mp3/"
447
copy $2
448
copy ".mp3"
449
end of regular expression
450
end of code
451
</example>
452
</para>
453
454
<para>
455
Note that there are no instructions for the
456
<link doc="ngx_http_core_module.xml" id="limit_rate"/>
457
directive above as it is unrelated to the
458
<literal>ngx_http_rewrite_module</literal> module.
459
A separate configuration is created for the <link id="if"/> block.
460
If the condition holds true, a request is assigned this configuration
461
where <literal>limit_rate</literal> equals to 10k.
462
</para>
463
464
<para>
465
The directive
466
<example>
467
rewrite ^/(download/.*)/media/(.*)\..*$ /$1/mp3/$2.mp3 break;
468
</example>
469
can be made smaller by one instruction if the first slash in the regular expression
470
is put inside the parentheses:
471
<example>
472
rewrite ^(<emphasis>/</emphasis>download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 break;
473
</example>
474
The corresponding instructions will then look like this:
475
<example>
476
match of regular expression
477
copy $1
478
copy "/mp3/"
479
copy $2
480
copy ".mp3"
481
end of regular expression
482
end of code
483
</example>
484
</para>
485
486
</section>
487
488
</module>
489
490