location 匹配规则

规则 匹配
= 严格匹配。如果请求匹配这个 location,那么将停止搜索并立即处理此请求
~ 区分大小写匹配(可用正则表达式)
~* 不区分大小写匹配(可用正则表达式)
!~ 区分大小写不匹配
!~* 不区分大小写不匹配
^~ 前缀匹配
@ “@” 定义一个命名的location,使用在内部定向时
/ 通用匹配,任何请求都会匹配到

location 原文

A location can either be defined by a prefix string, or by a regular expression. Regular expressions are specified with the preceding “~*” modifier (for case-insensitive matching), or the “~” modifier (for case-sensitive matching). To find location matching a given request, nginx first checks locations defined using the prefix strings (prefix locations). Among them, the location with the longest matching prefix is selected and remembered. Then regular expressions are checked, in the order of their appearance in the configuration file. The search of regular expressions terminates on the first match, and the corresponding configuration is used. If no match with a regular expression is found then the configuration of the prefix location remembered earlier is used.
If the longest matching prefix location has the “^~” modifier then regular expressions are not checked.
Also, using the “=” modifier it is possible to define an exact match of URI and location. If an exact match is found, the search terminates. For example, if a “/” request happens frequently, defining “location = /” will speed up the processing of these requests, as search terminates right after the first comparison. Such a location cannot obviously contain nested locations.

location 的定义分为两种:

  • 前缀字符串(prefix string)
  • 正则表达式(regular expression),具体为前面带 ~* 和 ~ 修饰符的

而匹配 location 的顺序为:

  1. 检查使用前缀字符串的 locations,在使用前缀字符串的 locations 中选择最长匹配的,并将结果进行储存。
  2. 如果符合带有 = 修饰符的 URI,则立刻停止匹配
  3. 如果符合带有 ^~ 修饰符的 URI,则也立刻停止匹配。
  4. 如果不满足2和3,使用1中记住的最长匹配前缀字符串location,按照定义文件的顺序,检查正则表达式,匹配到就停止。
  5. 当正则表达式匹配不到的时候,使用之前储存的前缀字符串

location的两种语法

约定: []表示里面的参数能省略, <>表示里面的参数不能省略.

第一种语法分为3个部分, 分别是: location关键字+@name别名(name是自己取的名字)+如何处理, 这个语法很简单, 就是做内部跳转, 这里不讨论了.

location @name { … }

第二种语法分为4个部分, 分别是: location关键字 + 匹配方式符号(可省略)+匹配规则+如何处理, 这个最复杂也是最常用, 我们只讨论这个.

location [ = | ~ | ~* | ^~ ] uri { … }

普通匹配和正则匹配

[ = | ~ | ~* | ^~ ]分为两种匹配模式, 分别是普通匹配和正则匹配.

普通匹配概述

  • = : 这代表精准匹配全路径, 命中它后直接返回, 不再进行后续匹配, 优先级最高.

  • ^~ : 这代表精准匹配开头, 命中开头后直接返回, 不再进行后续匹配, 优先级第二.

  • 无匹配方式符号 : 这代表通用性匹配, 命中后还会继续后续匹配, 最后选取路径最长的匹配, 并储存起来, 优先级第四.

正则匹配概述

  • ~: 这是区分大小写的正则匹配, 命中后则不进行后续匹配, 立即返回, 优先级第三.

  • *~: 不区分大小写的正则匹配, 命中后则不进行后续匹配, 立即返回, 优先级第三.

注:正则匹配中 ~ 和 * ~ 优先级一样, 它们按照从上到下的顺序进行匹配, 最先命中的立即返回, 后续的不会进行匹配, 所以精细的正则匹配规则往前放, 通用的正则匹配规则往后放.

location 匹配顺序

  • “=” 精准匹配,如果匹配成功,则停止其他匹配
  • 普通字符串指令匹配,优先级是从长到短(匹配字符越多,则选择该匹配结果)。匹配成功的location如果使用^~,则停止其他匹配(正则匹配)
  • 正则表达式指令匹配,按照配置文件里的顺序(从上到下),成功就停止其他匹配,如果正则匹配成功,使用该结果;否则使用普通字符串匹配结果

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

即:

(精确匹配)> (最长字符串匹配,但完全匹配) >(非正则匹配)>(正则匹配)>(最长字符串匹配,不完全匹配)>(location通配)

location 匹配案例

server {listen 9876;location = / {  return 200 "精确匹配/";}location ~* /ma.*ch {return 200 "正则匹配/ma.*ch";}location ~ /mat.*ch {return 200 "正则匹配/match.*";}location = /test {return 200 "精确匹配/test";}location ^~ /test/ {return 200 "前缀匹配/test";}location /test/hello/world {return 200 "完全匹配/test/hello/world";}location /test/hello {return 200 "部分匹配/test/hello";}location ~ /test/he*o {return 200 "正则匹配/test/he*o";}location /document {return 200 "/document";}location ~ /docu* {return 200 "~/docu";}location / {  return 200 "通配/";}}
curl localhost:9876/test
精确匹配/test
curl localhost:9876/test/hello/world
完全匹配/test/hello/world
curl localhost:9876/test/
前缀匹配/test
curl localhost:9876/test/hell
前缀匹配/test
curl localhost:9876/test/hello/aaa
部分匹配/test/hello
curl localhost:9876/test/heao
前缀匹配/test
curl localhost:9876/match
正则匹配/ma.*ch
curl localhost:9876/test/hello/world/ss
完全匹配/test/hello/world
curl localhost:9876/document
~/docu

上面例子中最后一个需要好好理解

【Nginx学习系列】location匹配规则相关推荐

  1. nginx配置中location匹配规则详解

    女主宣言 nginx作为一款性能优异的反向代理服务器,可以用于静态代理.负载均衡.限流等多种场景.那么,要灵活的使用nginx,必须清楚nginx配置文件的使用.本文作者对nginx的http块中的l ...

  2. Nginx—核心配置location匹配规则说明

    2019独角兽企业重金招聘Python工程师标准>>> location介绍 location指令是Nginx中最核心的一项配置,根据预先定义的URL匹配规则来接收用户发送的请求,根 ...

  3. nginx之location匹配规则

    Nginx之location 匹配规则详解 有些童鞋的误区 1. location 的匹配顺序是"先匹配正则,再匹配普通". 矫正: location 的匹配顺序其实是" ...

  4. Nginx之location 匹配规则详解

    Nginx 的语法形式是: location [=|~|~*|^~|@] /uri/ { - } ,意思是可以以" = "或" ~* "或" ~ &q ...

  5. location 匹配规则 (NGINX)

    转:https://moonbingbing.gitbooks.io/openresty-best-practices/ngx/nginx_local_pcre.html location 匹配规则 ...

  6. nginx中location匹配规则与proxy_pass代理转发

    最近使用nginx在服务器上配置,在做路径匹配时上遇到细节上的东西,在此做记录,安装请转 windows下安装使用nginx教程 一.location匹配规则 1.前缀匹配:不带符号 server { ...

  7. Nginx正则表达式与location匹配简介

    提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录 一.nginx rewrite概述 1.概述 2. 跳转场景​ 3. 跳转实现 4. rewrite实际场景 二.nginx ...

  8. Nginx篇之路由匹配规则以及配置url转发

    一.前言 一般我们经常在访问网站时,通常会遇到输入某个页面的网址时,出现路由的转发,重定向等.可能访问的是一个网址,出来的时候就显示的是另外的地址. 这种情况下,通常属于nginx的页面跳转. 二.N ...

  9. ngix 常用配置之 location 匹配规则

    大家好,我是 17. 今天和大家详细聊聊 nginx 的 location 匹配规则 location 匹配规则 @ 匹配规则在后面的 try_files 中有举例 location 按如下优先级匹配 ...

最新文章

  1. 红包规则_“科普闯关100%夺红包”游戏规则升级了!速速来看!
  2. linux信号机制 - 用户堆栈和内核堆栈的变化【转】
  3. [Eclipse]GEF入门系列(七、XYLayout和展开/折叠功能)
  4. 浅谈HASH算法与CSDN密码泄漏事件
  5. 强烈推荐的TensorFlow、Pytorch和Keras的样例资源(深度学习初学者必须收藏)
  6. C语言通过数组指针获取长度
  7. angular 居中_Angular 的模块间通信
  8. Java 经典习题-初学
  9. 凸二次规划解法(旋转法)——补充代码
  10. SpringBoot使用银联支付
  11. 一文掌握机器学习算法工程师技术栈
  12. switch好玩吗_外媒评Switch平台最佳游戏 好玩的基本都在这了
  13. 新站如何解决收录问题
  14. 【课程】03 Richards方程数值解
  15. Vue:运行项目时报错:Module not found: Error: Can't resolve 'sass-loader' in
  16. JDK11占比第一?
  17. 抖音seo矩阵系统,抖音矩阵系统源码怎么搭建?
  18. hive表信息查询:查看表结构、表操作、建表语句
  19. 基于ssm的药房药店药品管理系统
  20. Flink 检测一段时间内的温度连续上升输出报警

热门文章

  1. springboot@Valid注解用法详解
  2. samba服务器配置文件
  3. 计算机应用财会,按计算机应用分类,计算机在财务管理方面的应用属于()。
  4. 腾讯位置服务2021年中秋节国庆节假期服务公告
  5. input元素的oninput事件和onchange事件
  6. python自动化弹框_Selenium2+python自动化16-alert\confirm\prompt
  7. Es terms include 聚合过滤
  8. 基因分子生物学~遗传信息传递与遗传密码关键
  9. 给我未来的孩子--(转载)
  10. 基于PLC的混凝土搅拌站控制系统软件设计