Path: blob/main/xml/cn/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="ngx_http_rewrite_module模块"10link="/cn/docs/http/ngx_http_rewrite_module.html"11lang="cn"12translator="cfsego"13rev="2">1415<section id="summary">1617<para>18<literal>ngx_http_rewrite_module</literal>模块允许正则替换URI,返回页面重定向,和按条件选择配置。19</para>2021<para>22<literal>ngx_http_rewrite_module</literal>模块指令按以下顺序处理:23<list type="bullet">2425<listitem>26处理在<link doc="ngx_http_core_module.xml" id="server"/>级别中定义的模块指令;27</listitem>2829<listitem>30为请求查找location;31</listitem>3233<listitem>34处理在选中的<link doc="ngx_http_core_module.xml" id="location"/>中定义的模块指令。如果指令改变了URI,按新的URI查找location。这个循环至多重复<link doc="ngx_http_core_module.xml" id="internal">10次</link>,之后nginx返回错误<http-status code="500" text="Internal Server Error"/>。35</listitem>3637</list>38</para>3940</section>414243<section id="directives" name="指令">4445<directive name="break">46<syntax/>47<default/>48<context>server</context>49<context>location</context>50<context>if</context>5152<para>53停止处理当前这一轮的<literal>ngx_http_rewrite_module</literal>指令集。54</para>5556<para>57举例:58<example>59if ($slow) {60limit_rate 10k;61break;62}63</example>64</para>6566</directive>676869<directive name="if">70<syntax block="yes">(<value>condition</value>)</syntax>71<default/>72<context>server</context>73<context>location</context>7475<para>76计算指定的<value>condition</value>的值。如果为真,执行定义在大括号中的rewrite模块指令,并将<literal>if</literal>指令中的配置指定给请求。<literal>if</literal>指令会从上一层配置中继承配置。77</para>7879<para>80条件可以是下列任意一种:81<list type="bullet">8283<listitem>84变量名;如果变量值为空或者是以“<literal>0</literal>”开始的字符串,则条件为假;85</listitem>8687<listitem>88使用“<literal>=</literal>”和“<literal>!=</literal>”运算符比较变量和字符串;89</listitem>9091<listitem>92使用“<literal>~</literal>”(大小写敏感)和“<literal>~*</literal>”(大小写不敏感)运算符匹配变量和正则表达式。正则表达式可以包含匹配组,匹配结果后续可以使用变量<var>$1</var>..<var>$9</var>引用。如果正则表达式中包含字符“<literal>}</literal>”或者“<literal>;</literal>”,整个表达式应该被包含在单引号或双引号的引用中。93</listitem>9495<listitem>96使用“<literal>-f</literal>”和“<literal>!-f</literal>”运算符检查文件是否存在;97</listitem>9899<listitem>100使用“<literal>-d</literal>”和“<literal>!-d</literal>”运算符检查目录是否存在;101</listitem>102103<listitem>104使用“<literal>-e</literal>”和“<literal>!-e</literal>”运算符检查文件、目录或符号链接是否存在;105</listitem>106107<listitem>108使用“<literal>-x</literal>”和“<literal>!-x</literal>”运算符检查可执行文件;109</listitem>110111</list>112</para>113114<para>115举例:116<example>117if ($http_user_agent ~ MSIE) {118rewrite ^(.*)$ /msie/$1 break;119}120121if ($http_cookie ~* "id=([^;]+)(?:;|$)") {122set $id $1;123}124125if ($request_method = POST) {126return 405;127}128129if ($slow) {130limit_rate 10k;131}132133if ($invalid_referer) {134return 403;135}136</example>137<note>138内嵌变量<var>$invalid_referer</var>的值是通过<link doc="ngx_http_referer_module.xml" id="valid_referers"/>指令设置的。139</note>140</para>141142</directive>143144145<directive name="return">146<syntax><value>code</value> [<value>text</value>]</syntax>147<syntax><value>code</value> <value>URL</value></syntax>148<syntax><value>URL</value></syntax>149<default/>150<context>server</context>151<context>location</context>152<context>if</context>153154<para>155停止处理并返回指定<value>code</value>给客户端。返回非标准的状态码444可以直接关闭连接而不返回响应头。156</para>157158<para>159从0.8.42版开始,可以在指令中指定重定向的URL(状态码为301、302、303和307),或者指定响应体<value>文本</value>(状态码为其它值)。响应体文本或重定向URL中可以包含变量。作为一种特殊情况,重定向URL可以简化为当前server的本地URI,那么完整的重定向URL将按照请求协议(<var>$scheme</var>)、<link doc="ngx_http_core_module.xml" id="server_name_in_redirect"/>指令和<link doc="ngx_http_core_module.xml" id="port_in_redirect"/>指令的配置进行补全。160</para>161162<para>163另外,状态码为302的临时重定向使用的<value>URL</value>可以作为指令的唯一参数。该参数应该以“<literal>http://</literal>”、“<literal>https://</literal>”或者“<literal>https://</literal>”开始。<value>URL</value>中可以包含变量。164</para>165166<para>167<note>1680.7.51版本以前只能返回下面状态码:169204、400、402 — 406、408、410、411、413、416 和 500 — 504。170</note>171172<note>173直到1.1.16和1.0.13版,状态码307才被认为是一种重定向。174</note>175</para>176177</directive>178179180<directive name="rewrite">181<syntax>182<value>regex</value>183<value>replacement</value>184[<value>flag</value>]</syntax>185<default/>186<context>server</context>187<context>location</context>188<context>if</context>189190<para>191如果指定的正则表达式能匹配URI,此URI将被<value>replacement</value>参数定义的字符串改写。<literal>rewrite</literal>指令按其在配置文件中出现的顺序执行。flag可以终止后续指令的执行。如果replacement的字符串以“<literal>http://</literal>”或“<literal>https://</literal>”开头,nginx将结束执行过程,并返回给客户端一个重定向。192</para>193194<para>195可选的<value>flag</value>参数可以是其中之一:196<list type="tag">197198<tag-name><literal>last</literal></tag-name>199<tag-desc>200停止执行当前这一轮的<literal>ngx_http_rewrite_module</literal>指令集,然后查找匹配改变后URI的新location;201</tag-desc>202203<tag-name><literal>break</literal></tag-name>204<tag-desc>205停止执行当前这一轮的<literal>ngx_http_rewrite_module</literal>指令集;206</tag-desc>207208<tag-name><literal>redirect</literal></tag-name>209<tag-desc>210在replacement字符串未以“<literal>http://</literal>”或“<literal>https://</literal>”开头时,使用返回状态码为302的临时重定向;211</tag-desc>212213<tag-name><literal>permanent</literal></tag-name>214<tag-desc>215返回状态码为301的永久重定向。216</tag-desc>217218</list>219完整的重定向URL将按照请求协议(<var>$scheme</var>)、<link doc="ngx_http_core_module.xml" id="server_name_in_redirect"/>指令和<link doc="ngx_http_core_module.xml" id="port_in_redirect"/>指令的配置进行补全。220</para>221222<para>223举例:224<example>225server {226...227rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 last;228rewrite ^(/download/.*)/audio/(.*)\..*$ $1/mp3/$2.ra last;229return 403;230...231}232</example>233</para>234235<para>236但是当上述指令写在“<literal>/download/</literal>”的location中时,应使用标志<literal>break</literal>代替<literal>last</literal>,否则nginx会重复10轮循环,然后返回错误500:237<example>238location /download/ {239rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 break;240rewrite ^(/download/.*)/audio/(.*)\..*$ $1/mp3/$2.ra break;241return 403;242}243</example>244</para>245246<para>247如果<value>replacement</value>字符串包括新的请求参数,以往的请求参数会添加到新参数后面。如果不希望这样,在replacement字符串末尾加一个问号“?”,就可以避免,比如:248<example>249rewrite ^/users/(.*)$ /show?user=$1? last;250</example>251</para>252253<para>254如果正则表达式中包含字符“<literal>}</literal>”或者“<literal>;</literal>”,整个表达式应该被包含在单引号或双引号的引用中。255</para>256257</directive>258259260<directive name="rewrite_log">261<syntax><literal>on</literal> | <literal>off</literal></syntax>262<default>off</default>263<context>http</context>264<context>server</context>265<context>location</context>266<context>if</context>267268<para>269开启或者关闭将<literal>ngx_http_rewrite_module</literal>模块指令的处理日志以<literal>notice</literal>级别记录到错误日志中。270</para>271272</directive>273274275<directive name="set">276<syntax><value>variable</value> <value>value</value></syntax>277<default/>278<context>server</context>279<context>location</context>280<context>if</context>281282<para>283为指定变量<value>variable</value>设置变量值<value>value</value>。<value>value</value>可以包含文本、变量或者它们的组合。284</para>285286</directive>287288289<directive name="uninitialized_variable_warn">290<syntax><literal>on</literal> | <literal>off</literal></syntax>291<default>on</default>292<context>http</context>293<context>server</context>294<context>location</context>295<context>if</context>296297<para>298控制是否记录变量未初始化的警告到日志。299</para>300301</directive>302303</section>304305306<section id="internals" name="内部实现">307308<para>309<literal>ngx_http_rewrite_module</literal>模块的指令在解析配置阶段被编译成nginx内部指令。这些内部指令在处理请求时被解释执行。而解释器是一个简单的堆栈机器。310</para>311312<para>313比如,下面指令314<example>315location /download/ {316if ($forbidden) {317return 403;318}319320if ($slow) {321limit_rate 10k;322}323324rewrite ^/(download/.*)/media/(.*)\..*$ /$1/mp3/$2.mp3 break;325}326</example>327将被翻译成下面这些指令:328<example>329variable $forbidden330check against zero331return 403332end of code333variable $slow334check against zero335match of regular expression336copy "/"337copy $1338copy "/mp3/"339copy $2340copy ".mp3"341end of regular expression342end of code343</example>344</para>345346<para>347请注意没有对应上面的<link doc="ngx_http_core_module.xml" id="limit_rate"/>指令的内部指令,因为这个指令与<literal>ngx_http_rewrite_module</literal>模块无关。nginx会为这个<link id="if"/>块单独创建一个配置,包含<literal>limit_rate</literal>等于10k。如果条件为真,nginx将把这个配置指派给请求。348</para>349350<para>351指令352<example>353rewrite ^/(download/.*)/media/(.*)\..*$ /$1/mp3/$2.mp3 break;354</example>355可以通过将正则表达式中的第一个斜线“/”放入圆括号,来实现节约一个内部指令:356<example>357rewrite ^(<emphasis>/</emphasis>download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 break;358</example>359对应的内部指令将会是这样:360<example>361match of regular expression362copy $1363copy "/mp3/"364copy $2365copy ".mp3"366end of regular expression367end of code368</example>369</para>370371</section>372373</module>374375376