官网解读-Module ngx_http_rewrite_module

The ngx_http_rewrite_module module is used to change request URI using PCRE regular expressions, return redirects, and conditionally select configurations.

The breakifreturnrewrite, and set directives are processed in the following order:

  • the directives of this module specified on the server level are executed sequentially;
  • repeatedly:
    • location is searched based on a request URI;
    • the directives of this module specified inside the found location are executed sequentially;
    • the loop is repeated if a request URI was rewritten, but not more than 10 times.

 

Directives

Syntax:break;
Default:
Context:serverlocationif

Stops processing the current set of ngx_http_rewrite_module directives.

If a directive is specified inside the location, further processing of the request continues in this location.

Example:

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

 

Syntax:if (condition) { ... }
Default:
Context:serverlocation

The specified condition is evaluated. If true, this module directives specified inside the braces are executed, and the request is assigned the configuration inside the if directive. Configurations inside the if directives are inherited from the previous configuration level.

A condition may be any of the following:

  • a variable name; false if the value of a variable is an empty string or “0”;
    Before version 1.0.1, any string starting with “ 0” was considered a false value.
  • comparison of a variable with a string using the “=” and “!=” operators;
  • matching of a variable against a regular expression using the “~” (for case-sensitive matching) and “~*” (for case-insensitive matching) operators. Regular expressions can contain captures that are made available for later reuse in the $1..$9 variables. Negative operators “!~” and “!~*” are also available. If a regular expression includes the “}” or “;” characters, the whole expressions should be enclosed in single or double quotes.
  • checking of a file existence with the “-f” and “!-f” operators;
  • checking of a directory existence with the “-d” and “!-d” operators;
  • checking of a file, directory, or symbolic link existence with the “-e” and “!-e” operators;
  • checking for an executable file with the “-x” and “!-x” operators.

 

Examples:

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;
}

 

A value of the  $invalid_referer embedded variable is set by the  valid_referers directive.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值