模块 ngx_http_rewrite_module

如果
return
rewrite
rewrite_log 设置为
Internal Implementation uninitialized_variable_warn

Directives
会中断

该模块用于 使用 PCRE 正则表达式更改请求 URI,返回重定向,以及 有条件地选择 Configurations。ngx_http_rewrite_module

breakifreturnrewriteset Directives是 按以下顺序处理:

  • 服务器级别指定的此模块的Directives 按顺序执行;
  • 反复:
    • 根据请求 URI 搜索位置;
    • 在 found location 内指定的此模块的Directives 按顺序执行;
    • 如果请求 URI 被重写,则会重复循环, 但不超过 10 次

Directives

语法: break;
默认值:
上下文: server, ,locationif

停止处理当前Directives集。ngx_http_rewrite_module

如果在 location 内指定了Directives,则 在此位置继续对请求进行进一步处理。

例:

if ($slow) {
    limit_rate 10k;
    break;
}

语法: if (condition) { ... }
默认值:
上下文: server,location

将评估指定的 The Specified。 如果为 true,则在大括号内指定的 this module Directives为 执行,并在Directives内为请求分配配置。 Directives内的配置是 继承自之前的配置级别。conditionifif

条件可以是以下任何一项:

  • 变量名称;如果变量的值为空字符串,则为 false 或 “”;0
    在 1.0.1 版本之前,任何以 “” 开头的字符串 被视为 false 值。0
  • 使用 “” 和 “” 运算符;=!=
  • 使用 “”(用于区分大小写的匹配)和 “”(用于不区分大小写的匹配)运算符。 正则表达式可以包含可用于的捕获 以后在 .. 中重复使用变量。 否定运算符 “” 和 “” 也可用。 如果正则表达式包含 “” 或 “” 字符,则整个表达式应括起来 在单引号或双引号中。~~*$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;
}

嵌入变量的值由 valid_referers Directives设置。$invalid_referer

语法: return code [text];
return code URL;
return URL;
默认值:
上下文: server, ,locationif

停止处理并将指定的返回给客户端。 非标准代码 444 关闭连接而不发送 响应标头。code

从版本 0.8.42 开始,可以指定 重定向 URL(适用于代码 301、302、303、307 和 308) 或响应正文(对于其他代码)。 响应正文文本和重定向 URL 可以包含变量。 作为特殊情况,可以将重定向 URL 指定为 URI local 的 URL 添加到此服务器,在这种情况下,完整的重定向 URL 根据请求方案 () 以及 server_name_in_redirectport_in_redirect Directives形成。text$scheme

此外,a 用于代码为 302 的临时重定向 可以指定为唯一参数。 此类参数应以 “”开头, “” 或 “” 字符串。 A 可以包含变量。URLhttp://https://$schemeURL

在 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 regex replacement [flag];
默认值:
上下文: server, ,locationif

如果指定的正则表达式与请求 URI 匹配,则 URI 会更改 如字符串中指定的那样。 Directives按顺序执行 按它们在配置文件中的显示顺序。 可以使用 flags 终止对Directives的进一步处理。 如果替换字符串以 “” 开头, “” 或 “”, 处理停止,重定向将返回给客户端。replacementrewritehttp://https://$scheme

可选参数可以是以下之一:flag

last
停止处理当前Directives集并启动 搜索与更改的 URI 匹配的新位置;ngx_http_rewrite_module
break
停止处理当前Directives集 与 break Directives一样;ngx_http_rewrite_module
redirect
返回带有 302 代码的临时重定向; 如果替换字符串不以 “”, “”, 或 “”;http://https://$scheme
permanent
返回带有 301 代码的永久重定向。

完整的重定向 URL 是根据 request scheme () 以及 server_name_in_redirectport_in_redirect Directives。$scheme

例:

server {
    ...
    rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 last;
    rewrite ^(/download/.*)/audio/(.*)\..*$ $1/mp3/$2.ra  last;
    return  403;
    ...
}

但是,如果将这些Directives放在 “” location,该标志应该被 替换为 ,否则 nginx 将进行 10 个循环,并且 返回 500 错误:/download/lastbreak

location /download/ {
    rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 break;
    rewrite ^(/download/.*)/audio/(.*)\..*$ $1/mp3/$2.ra  break;
    return  403;
}

如果字符串包含新的请求参数, 前面的请求参数会附加在它们之后。 如果不希望这样做,请在替换的末尾放置一个问号 string 避免附加它们,例如:replacement

rewrite ^/users/(.*)$ /show?user=$1? last;

如果正则表达式包含 “” 或 “” 字符,则整个表达式应括起来 在单引号或双引号中。};

语法: rewrite_log on | off;
默认值:
rewrite_log off;
上下文: http, , ,serverlocationif

启用或禁用模块Directives处理结果的日志记录 进入error_log 级别。ngx_http_rewrite_modulenotice

语法: set $variable value;
默认值:
上下文: server, ,locationif

为指定的 . 可以包含文本、变量及其组合。valuevariablevalue

语法: uninitialized_variable_warn on | off;
默认值:
uninitialized_variable_warn on;
上下文: http, , ,serverlocationif

控制是否记录有关未初始化变量的警告。

internal实施

module Directives 在配置阶段编译为internalDirectives ,这些请求在请求处理期间进行解释。 resolver 是一个简单的虚拟堆栈机器。ngx_http_rewrite_module

例如,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的说明,因为它与模块无关。 为 if 块创建单独的配置。 如果条件成立,则为请求分配此配置 其中等于 10k。ngx_http_rewrite_modulelimit_rate

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