nginx location if 的匹配规则

nginx location if 的匹配规则

cation匹配命令

~      #波浪线表示执行一个正则匹配,区分大小写
~*    #表示执行一个正则匹配,不区分大小写
^~    #^~表示普通字符匹配,不是正则匹配。如果该选项匹配,只匹配该选项,不匹配别的选项,一般用来匹配目录
=      #进行普通字符精确匹配
@     #"@" 定义一个命名的 location,使用在内部定向时,例如 error_page, try_files

 

 

参考:https://segmentfault.com/a/1190000002797606

 

 

location 优先级官方文档

 

1. Directives with the = prefix that match the query exactly. If found, searching stops.

2. All remaining directives with conventional strings, longest match first. If this match used the ^~ prefix, searching stops.

3. Regular expressions, in order of definition in the configuration file.

4. If #3 yielded a match, that result is used. Else the match from #2 is used.

 

1. =前缀的指令严格匹配这个查询。如果找到,停止搜索。

2. 所有剩下的常规字符串,最长的匹配。如果这个匹配使用^前缀,搜索停止。

3. 正则表达式,在配置文件中定义的顺序。

4. 如果第3条规则产生匹配的话,结果被使用。否则,如同从第2条规则被使用。

 

顺序 no优先级: (location =) > (location 完整路径) > (location ^~ 路径) > (location ~,~* 正则顺序) > (location 部分起始路径) > (/)

 

例如:

location  = / {   # 只匹配"/".   [ configuration A ]  } location  / {   # 匹配任何请求,因为所有请求都是以"/"开始   # 但是更长字符匹配或者正则表达式匹配会优先匹配   [ configuration B ]  } location ^~ /images/ {   # 匹配任何以 /images/ 开始的请求,并停止匹配 其它location   [ configuration C ]  } location ~* \.(gif|jpg|jpeg)$ {   # 匹配以 gif, jpg, or jpeg结尾的请求.    # 但是所有 /images/ 目录的请求将由 [Configuration C]处理.      [ configuration D ]  }

 

 

 

我的疑问1 : 如果是以下的 

  • /images/1.gif -> 会匹配C还是D呢?  会按顺序匹配到C。因为都是正则所以按顺序匹配到了C

location ~ /images/ {   # 匹配任何以 /images/ 开始的请求,并停止匹配 其它location   [ configuration C ]  } location ~* \.(gif|jpg|jpeg)$ {   # 匹配以 gif, jpg, or jpeg结尾的请求.    # 但是所有 /images/ 目录的请求将由 [Configuration C]处理.      [ configuration D ]  }

 

 

我的疑问2: 如果是以下的。会匹配到D ,因为正则匹配到优先级大于部分起始路径。

location  /images/ {   # 匹配任何以 /images/ 开始的请求,并停止匹配 其它location   [ configuration C ]  } location ~* \.(gif|jpg|jpeg)$ {   # 匹配以 gif, jpg, or jpeg结尾的请求.    # 但是所有 /images/ 目录的请求将由 [Configuration C]处理.      [ configuration D ]  }

 

 

 

 

 

if 条件判断:

参考:

http://tengine.taobao.org/nginx_docs/cn/docs/http/ngx_http_rewrite_module.html#if

 

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

计算指定的condition的值。如果为真,执行定义在大括号中的rewrite模块指令,并将if指令中的配置指定给请求。if指令会从上一层配置中继承配置。

条件可以是下列任意一种:

  • 变量名;如果变量值为空或者是以“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; }

 

案例每个用户的guid存在cookie中要存入nginx日志 如果存在的话:

    set $guid "-";

    if ( $http_cookie ~* "guid=(\S+)(;.*|$)"){

        set $guid $1;

    }

 

内嵌变量$invalid_referer的值是通过valid_referers指令设置的。

 

补充正则表达式知识:

参考:https://regex101.com/

(?:;|$)
(;|$)

两者的区别即 ?:的作用。加上?:在分组中表示不捕捉这个分组,在后面不可以引用 Non-capturing group 

(?:;|$)

Capturing Group 

(;|$)

 

 

 

 

rewrite 模块

 

重写语法:

http://tengine.taobao.org/nginx_docs/cn/docs/http/ngx_http_rewrite_module.html#if

 

语法: rewrite regex replacement [flag];
默认值:
上下文: serverlocationif

如果指定的正则表达式能匹配URI,此URI将被replacement参数定义的字符串改写。rewrite指令按其在配置文件中出现的顺序执行。flag可以终止后续指令的执行。如果replacement的字符串以“http://”或“https://”开头,nginx将结束执行过程,并返回给客户端一个重定向。

可选的flag参数可以是其中之一:

  • last

  • 停止执行当前这一轮的ngx_http_rewrite_module指令集,然后查找匹配改变后URI的新location;

  • break

  • 停止执行当前这一轮的ngx_http_rewrite_module指令集;

  • redirect

  • 在replacement字符串未以“http://”或“https://”开头时,使用返回状态码为302的临时重定向;

  • permanent

  • 返回状态码为301的永久重定向。

 

 

如果URI中含有参数(/app/test.php?id=5),默认情况下参数会被自动附加到替换串上,可以通过在替换串的末尾加上?标记来解决这一问题。
例如:
 

复制代码代码示例:

rewrite ^/test(.*)$ http://www.it.net.cn/home  permanent;
访问http://www.it.net.cn/test?id=5 会跳转到 http://www.it.net.cn/home?id=5
 

例如:如果将类似URL /photo/123456 重定向到 /path/to/photo/12/1234/123456.png
 

复制代码代码示例:

Rewrite "/photo/([0-9]{2})([0-9]{2})([0-9]{2})" /path/to/photo/$1/$1$2/$1$2$3.png ;

 

 

 

set指令。这里的变量名和php的语法差不多。变量名前面定义$代表定义变量(set)或者引用变量。

Syntax: set $variable value;
Default:
Context: serverlocationif

Sets a value for the specified variable. The value can contain text, variables, and their combination.

猜你喜欢

转载自my.oschina.net/u/3367404/blog/2934409