模块 ngx_http_rewrite_module
| 如果 return rewrite rewrite_log 设置为 Internal Implementation uninitialized_variable_warn 则Directives 会中断 |
这ngx_http_rewrite_modulemodule 用于
使用 PCRE 正则表达式更改请求 URI,返回重定向,以及
有条件地选择 Configurations。
break、if、return、rewrite 和 set Directives是 按以下顺序处理:
- 在服务器级别指定的此模块的Directives 按顺序执行;
- 反复:
Directives
| 语法: |
break; |
|---|---|
| 默认值: | — |
| 上下文: |
server,location,if |
停止处理当前的ngx_http_rewrite_moduleDirectives。
如果在 location 内指定了Directives,则 在此位置继续对请求进行进一步处理。
例:
if ($slow) {
limit_rate 10k;
break;
}
| 语法: |
if ( |
|---|---|
| 默认值: | — |
| 上下文: |
server,location |
指定的condition被评估。
如果为 true,则在大括号内指定的 this module Directives为
执行,并将请求分配在if命令。
Configuration 中的ifDirectives是
继承自之前的配置级别。
条件可以是以下任何一项:
- 变量名称;如果变量的值为空字符串,则为 false
或 ”
0”;在 1.0.1 版本之前,任何以 “ 开头的字符串
0” 被视为 false 值。 - 使用
“” 和 ”
=!=“ 运算符; - 使用
“”(用于区分大小写的匹配)和
“”(用于不区分大小写的匹配)运算符。
正则表达式可以包含可用于的捕获
稍后在
~~*$1..$9变量。 负运算符 ”!~“ 和 ”!~*” 也可用。 如果正则表达式包含 “” 或 “” 字符,则整个表达式应括起来 在单引号或双引号中。}; - 使用 ”
-f“ 和 “!-f“ 运算符; - 使用 ”
-d“ 和 “!-d“ 运算符; - 检查文件、目录或符号链接是否存在
“
-e“ 和 ”!-e“ 运算符; - 检查带有 ”
-x” 和”!-x“ 运算符。
例子:
if ($http_user_agent ~ MSIE) {
rewrite ^(.*)$ /msie/$1 break;
}
if ($http_cookie ~* "id=([^;]+)(?:;|$)") {
set $id $1;
}
if ($request_method = POST) {
return 405;
}
if ($slow) {
limit_rate 10k;
}
if ($invalid_referer) {
return 403;
}
的$invalid_refererembedded 变量由 valid_referers Directives设置。
| 语法: |
return return return |
|---|---|
| 默认值: | — |
| 上下文: |
server,location,if |
停止处理并返回指定的code给客户。
非标准代码 444 关闭连接而不发送
响应标头。
从版本 0.8.42 开始,可以指定
重定向 URL(适用于代码 301、302、303、307 和 308)
或响应正文text(对于其他代码)。
响应正文文本和重定向 URL 可以包含变量。
作为特殊情况,可以将重定向 URL 指定为 URI
local 的 URL 添加到此服务器,在这种情况下,完整的重定向 URL
根据请求方案 ($scheme) 以及 server_name_in_redirect 和 port_in_redirect Directives。
此外,一个URL用于代码为 302 的临时重定向
可以指定为唯一参数。
此类参数应以 ”http://”,
“https://“或 ”$scheme“ 字符串。
一个URL可以包含变量。
在 0.7.51 版本之前,只能返回以下代码: 204、400、402 — 406、408、410、411、413、416 和 500 — 504。
代码 307 直到版本 1.1.16 和 1.0.13 才被视为重定向。
代码 308 直到 1.13.0 版才被视为重定向。
另请参见 error_page Directives。
| 语法: |
rewrite
|
|---|---|
| 默认值: | — |
| 上下文: |
server,location,if |
如果指定的正则表达式与请求 URI 匹配,则 URI 会更改
如replacement字符串。
这rewriteDirectives按顺序执行
按它们在配置文件中的显示顺序。
可以使用 flags 终止对Directives的进一步处理。
如果替换字符串以 “ 开头http://”,
“https://“或 ”$scheme”,
处理停止,重定向将返回给客户端。
可选的flagparameter 可以是以下之一:
last- 停止处理当前的
ngx_http_rewrite_moduleDirectives和启动 搜索与更改的 URI 匹配的新位置; break- 停止处理当前的
ngx_http_rewrite_moduleDirectives 与 break Directives一样; redirect- 返回带有 302 代码的临时重定向;
如果替换字符串不以
“
http://”, “https://”, 或 ”$scheme”; permanent- 返回带有 301 代码的永久重定向。
完整的重定向 URL 是根据
请求方案 ($scheme) 以及 server_name_in_redirect 和 port_in_redirect Directives。
例:
server {
...
rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 last;
rewrite ^(/download/.*)/audio/(.*)\..*$ $1/mp3/$2.ra last;
return 403;
...
}
但是,如果将这些Directives放在 ”/download/”
location、lastflag 应替换为break,否则 nginx 将进行 10 个循环,并且
返回 500 错误:
location /download/ {
rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 break;
rewrite ^(/download/.*)/audio/(.*)\..*$ $1/mp3/$2.ra break;
return 403;
}
如果replacementstring 包含新的请求参数,
前面的请求参数会附加在它们之后。
如果不希望这样做,请在替换的末尾放置一个问号
string 避免附加它们,例如:
rewrite ^/users/(.*)$ /show?user=$1? last;
如果正则表达式包含 “”
或 “” 字符,则整个表达式应括起来
在单引号或双引号中。};
| 语法: |
rewrite_log |
|---|---|
| 默认值: |
rewrite_log off; |
| 上下文: |
http,server,location,if |
启用或禁用ngx_http_rewrite_modulemodule Directives处理结果
进入error_log
这notice水平。
| 语法: |
set |
|---|---|
| 默认值: | — |
| 上下文: |
server,location,if |
将value对于指定的variable.
这value可以包含文本、变量及其组合。
| 语法: |
uninitialized_variable_warn |
|---|---|
| 默认值: |
uninitialized_variable_warn on; |
| 上下文: |
http,server,location,if |
控制是否记录有关未初始化变量的警告。
internal实施
这ngx_http_rewrite_modulemodule Directives
在配置阶段编译为internalDirectives
,这些内容在请求处理期间进行解释。
resolver 是一个简单的虚拟堆栈机器。
例如,Directives
location /download/ {
if ($forbidden) {
return 403;
}
if ($slow) {
limit_rate 10k;
}
rewrite ^/(download/.*)/media/(.*)\..*$ /$1/mp3/$2.mp3 break;
}
将翻译成以下说明:
variable $forbidden
check against zero
return 403
end of code
variable $slow
check against zero
match of regular expression
copy "/"
copy $1
copy "/mp3/"
copy $2
copy ".mp3"
end of regular expression
end of code
请注意,上面没有 limit_rate Directives的说明,因为它与ngx_http_rewrite_module模块。
为 if 块创建单独的配置。
如果条件成立,则为请求分配此配置
哪里limit_rate等于 10k。
Directives
rewrite ^/(download/.*)/media/(.*)\..*$ /$1/mp3/$2.mp3 break;
如果正则表达式中的第一个斜杠 放在括号内:
rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 break;
相应的说明将如下所示:
match of regular expression copy $1 copy "/mp3/" copy $2 copy ".mp3" end of regular expression end of code