Path: blob/main/xml/en/docs/http/ngx_http_rewrite_module.xml
1 views
<?xml version="1.0"?>12<!--3Copyright (C) Igor Sysoev4Copyright (C) Nginx, Inc.5-->67<!DOCTYPE module SYSTEM "../../../../dtd/module.dtd">89<module name="Module ngx_http_rewrite_module"10link="/en/docs/http/ngx_http_rewrite_module.html"11lang="en"12rev="9">1314<section id="summary">1516<para>17The <literal>ngx_http_rewrite_module</literal> module is used to18change request URI using PCRE regular expressions, return redirects, and19conditionally select configurations.20</para>2122<para>23The <link id="break"/>, <link id="if"/>, <link id="return"/>,24<link id="rewrite"/>, and <link id="set"/> directives are25processed in the following order:26<list type="bullet">2728<listitem>29the directives of this module specified on the30<link doc="ngx_http_core_module.xml" id="server"/> level31are executed sequentially;32</listitem>3334<listitem>35repeatedly:36<list type="bullet">3738<listitem>39a40<link doc="ngx_http_core_module.xml" id="location"/>41is searched based on a request URI;42</listitem>4344<listitem>45the directives of this module specified inside the found location46are executed sequentially;47</listitem>4849<listitem>50the loop is repeated if a request URI was <link id="rewrite">rewritten</link>,51but not more than52<link doc="ngx_http_core_module.xml" id="internal">10 times</link>.53</listitem>5455</list>56</listitem>5758</list>59</para>6061</section>626364<section id="directives" name="Directives">6566<directive name="break">67<syntax/>68<default/>69<context>server</context>70<context>location</context>71<context>if</context>7273<para>74Stops processing the current set of75<literal>ngx_http_rewrite_module</literal> directives.76</para>7778<para>79If a directive is specified inside the80<link doc="ngx_http_core_module.xml" id="location"/>,81further processing of the request continues in this location.82</para>8384<para>85Example:86<example>87if ($slow) {88limit_rate 10k;89break;90}91</example>92</para>9394</directive>959697<directive name="if">98<syntax block="yes">(<value>condition</value>)</syntax>99<default/>100<context>server</context>101<context>location</context>102103<para>104The specified <value>condition</value> is evaluated.105If true, this module directives specified inside the braces are106executed, and the request is assigned the configuration inside the107<literal>if</literal> directive.108Configurations inside the <literal>if</literal> directives are109inherited from the previous configuration level.110</para>111112<para>113A condition may be any of the following:114<list type="bullet">115116<listitem>117a variable name; false if the value of a variable is an empty string118or “<literal>0</literal>”;119<note>120Before version 1.0.1, any string starting with “<literal>0</literal>”121was considered a false value.122</note>123</listitem>124125<listitem>126comparison of a variable with a string using the127“<literal>=</literal>” and “<literal>!=</literal>” operators;128</listitem>129130<listitem>131matching of a variable against a regular expression using the132“<literal>~</literal>” (for case-sensitive matching) and133“<literal>~*</literal>” (for case-insensitive matching) operators.134Regular expressions can contain captures that are made available for135later reuse in the <var>$1</var>..<var>$9</var> variables.136Negative operators “<literal>!~</literal>” and “<literal>!~*</literal>”137are also available.138If a regular expression includes the “<literal>}</literal>”139or “<literal>;</literal>” characters, the whole expressions should be enclosed140in single or double quotes.141</listitem>142143<listitem>144checking of a file existence with the “<literal>-f</literal>” and145“<literal>!-f</literal>” operators;146</listitem>147148<listitem>149checking of a directory existence with the “<literal>-d</literal>” and150“<literal>!-d</literal>” operators;151</listitem>152153<listitem>154checking of a file, directory, or symbolic link existence with the155“<literal>-e</literal>” and “<literal>!-e</literal>” operators;156</listitem>157158<listitem>159checking for an executable file with the “<literal>-x</literal>”160and “<literal>!-x</literal>” operators.161</listitem>162163</list>164</para>165166<para>167Examples:168<example>169if ($http_user_agent ~ MSIE) {170rewrite ^(.*)$ /msie/$1 break;171}172173if ($http_cookie ~* "id=([^;]+)(?:;|$)") {174set $id $1;175}176177if ($request_method = POST) {178return 405;179}180181if ($slow) {182limit_rate 10k;183}184185if ($invalid_referer) {186return 403;187}188</example>189<note>190A value of the <var>$invalid_referer</var> embedded variable is set by the191<link doc="ngx_http_referer_module.xml" id="valid_referers"/> directive.192</note>193</para>194195</directive>196197198<directive name="return">199<syntax><value>code</value> [<value>text</value>]</syntax>200<syntax><value>code</value> <value>URL</value></syntax>201<syntax><value>URL</value></syntax>202<default/>203<context>server</context>204<context>location</context>205<context>if</context>206207<para>208Stops processing and returns the specified <value>code</value> to a client.209The non-standard code 444 closes a connection without sending210a response header.211</para>212213<para>214Starting from version 0.8.42, it is possible to specify215either a redirect URL (for codes 301, 302, 303, 307, and 308)216or the response body <value>text</value> (for other codes).217A response body text and redirect URL can contain variables.218As a special case, a redirect URL can be specified as a URI219local to this server, in which case the full redirect URL220is formed according to the request scheme (<var>$scheme</var>) and the221<link doc="ngx_http_core_module.xml" id="server_name_in_redirect"/> and222<link doc="ngx_http_core_module.xml" id="port_in_redirect"/> directives.223</para>224225<para>226In addition, a <value>URL</value> for temporary redirect with the code 302227can be specified as the sole parameter.228Such a parameter should start with the “<literal>http://</literal>”,229“<literal>https://</literal>”, or “<literal>$scheme</literal>” string.230A <value>URL</value> can contain variables.231</para>232233<para>234<note>235Only the following codes could be returned before version 0.7.51:236204, 400, 402 — 406, 408, 410, 411, 413, 416, and 500 — 504.237</note>238239<note>240The code 307 was not treated as a redirect until versions 1.1.16 and 1.0.13.241</note>242243<note>244The code 308 was not treated as a redirect until version 1.13.0.245</note>246</para>247248<para>249See also the <link doc="ngx_http_core_module.xml" id="error_page"/> directive.250</para>251252</directive>253254255<directive name="rewrite">256<syntax>257<value>regex</value>258<value>replacement</value>259[<value>flag</value>]</syntax>260<default/>261<context>server</context>262<context>location</context>263<context>if</context>264265<para>266If the specified regular expression matches a request URI, URI is changed267as specified in the <value>replacement</value> string.268The <literal>rewrite</literal> directives are executed sequentially269in order of their appearance in the configuration file.270It is possible to terminate further processing of the directives using flags.271If a replacement string starts with “<literal>http://</literal>”,272“<literal>https://</literal>”, or “<literal>$scheme</literal>”,273the processing stops and the redirect is returned to a client.274</para>275276<para>277An optional <value>flag</value> parameter can be one of:278<list type="tag">279280<tag-name><literal>last</literal></tag-name>281<tag-desc>282stops processing the current set of283<literal>ngx_http_rewrite_module</literal> directives and starts284a search for a new location matching the changed URI;285</tag-desc>286287<tag-name><literal>break</literal></tag-name>288<tag-desc>289stops processing the current set of290<literal>ngx_http_rewrite_module</literal> directives291as with the <link id="break"/> directive;292</tag-desc>293294<tag-name><literal>redirect</literal></tag-name>295<tag-desc>296returns a temporary redirect with the 302 code;297used if a replacement string does not start with298“<literal>http://</literal>”, “<literal>https://</literal>”,299or “<literal>$scheme</literal>”;300</tag-desc>301302<tag-name><literal>permanent</literal></tag-name>303<tag-desc>304returns a permanent redirect with the 301 code.305</tag-desc>306307</list>308The full redirect URL is formed according to the309request scheme (<var>$scheme</var>) and the310<link doc="ngx_http_core_module.xml" id="server_name_in_redirect"/> and311<link doc="ngx_http_core_module.xml" id="port_in_redirect"/> directives.312</para>313314<para>315Example:316<example>317server {318...319rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 last;320rewrite ^(/download/.*)/audio/(.*)\..*$ $1/mp3/$2.ra last;321return 403;322...323}324</example>325</para>326327<para>328But if these directives are put inside the “<literal>/download/</literal>”329location, the <literal>last</literal> flag should be replaced by330<literal>break</literal>, or otherwise nginx will make 10 cycles and331return the 500 error:332<example>333location /download/ {334rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 break;335rewrite ^(/download/.*)/audio/(.*)\..*$ $1/mp3/$2.ra break;336return 403;337}338</example>339</para>340341<para>342If a <value>replacement</value> string includes the new request arguments,343the previous request arguments are appended after them.344If this is undesired, putting a question mark at the end of a replacement345string avoids having them appended, for example:346<example>347rewrite ^/users/(.*)$ /show?user=$1? last;348</example>349</para>350351<para>352If a regular expression includes the “<literal>}</literal>”353or “<literal>;</literal>” characters, the whole expressions should be enclosed354in single or double quotes.355</para>356357</directive>358359360<directive name="rewrite_log">361<syntax><literal>on</literal> | <literal>off</literal></syntax>362<default>off</default>363<context>http</context>364<context>server</context>365<context>location</context>366<context>if</context>367368<para>369Enables or disables logging of <literal>ngx_http_rewrite_module</literal>370module directives processing results371into the <link doc="../ngx_core_module.xml" id="error_log"/> at372the <literal>notice</literal> level.373</para>374375</directive>376377378<directive name="set">379<syntax><value>$variable</value> <value>value</value></syntax>380<default/>381<context>server</context>382<context>location</context>383<context>if</context>384385<para>386Sets a <value>value</value> for the specified <value>variable</value>.387The <value>value</value> can contain text, variables, and their combination.388</para>389390</directive>391392393<directive name="uninitialized_variable_warn">394<syntax><literal>on</literal> | <literal>off</literal></syntax>395<default>on</default>396<context>http</context>397<context>server</context>398<context>location</context>399<context>if</context>400401<para>402Controls whether warnings about uninitialized variables are logged.403</para>404405</directive>406407</section>408409410<section id="internals" name="Internal Implementation">411412<para>413The <literal>ngx_http_rewrite_module</literal> module directives414are compiled at the configuration stage into internal instructions415that are interpreted during request processing.416An interpreter is a simple virtual stack machine.417</para>418419<para>420For example, the directives421<example>422location /download/ {423if ($forbidden) {424return 403;425}426427if ($slow) {428limit_rate 10k;429}430431rewrite ^/(download/.*)/media/(.*)\..*$ /$1/mp3/$2.mp3 break;432}433</example>434will be translated into these instructions:435<example>436variable $forbidden437check against zero438return 403439end of code440variable $slow441check against zero442match of regular expression443copy "/"444copy $1445copy "/mp3/"446copy $2447copy ".mp3"448end of regular expression449end of code450</example>451</para>452453<para>454Note that there are no instructions for the455<link doc="ngx_http_core_module.xml" id="limit_rate"/>456directive above as it is unrelated to the457<literal>ngx_http_rewrite_module</literal> module.458A separate configuration is created for the <link id="if"/> block.459If the condition holds true, a request is assigned this configuration460where <literal>limit_rate</literal> equals to 10k.461</para>462463<para>464The directive465<example>466rewrite ^/(download/.*)/media/(.*)\..*$ /$1/mp3/$2.mp3 break;467</example>468can be made smaller by one instruction if the first slash in the regular expression469is put inside the parentheses:470<example>471rewrite ^(<emphasis>/</emphasis>download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 break;472</example>473The corresponding instructions will then look like this:474<example>475match of regular expression476copy $1477copy "/mp3/"478copy $2479copy ".mp3"480end of regular expression481end of code482</example>483</para>484485</section>486487</module>488489490