nginx路径匹配规则

1.路径配置的分类

在nginx中,一共有4种不同的路径配置方法

= - Exact match
^~ - Preferential match
~ && ~* - Regex match
no modifier - Prefix match

#路径完全一样则匹配 location = path { } #路径开头一样则匹配 location ^~ path{ } #正则匹配,大小写敏感 location ~ path{ } #正则匹配,大小写不敏感 location ~* path{ } #前缀匹配 location path{ }

上面的执行顺序是,优先查看Exact match,若存在,则停止。如不存在,则进入Preferential match。之后在进入Regex match,先看大小写敏感的规则,再看大小写不敏感的规则.最后进入Prefix match .

= --> ^~ --> ~ --> ~* --> no modifier

在每一个同类型的匹配规则中,按照他们出现在配置文件中的先后,一一对比。

2.例子

location /match { return 200 'Prefix match: will match everything that starting with /match'; } location ~* /match[0-9] { return 200 'Case insensitive regex match'; } location ~ /MATCH[0-9] { return 200 'Case sensitive regex match'; } location ^~ /match0 { return 200 'Preferential match'; } location = /match { return 200 'Exact match'; }
/match # => 'Exact match' /match0 # => 'Preferential match' /match1 # => 'Case insensitive regex match' /MATCH1 # => 'Case sensitive regex match' /match-abc # => 'Prefix match: matches everything that starting with /match'

__EOF__

本文作者LinM狂想曲
本文链接https://www.cnblogs.com/Oliver1993/p/14581973.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   LinM狂想曲  阅读(942)  评论(0编辑  收藏  举报
编辑推荐:
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· NetPad:一个.NET开源、跨平台的C#编辑器
· PowerShell开发游戏 · 打蜜蜂
点击右上角即可分享
微信分享提示