nginx-匹配规则

时间:2023-03-08 19:39:52
nginx-匹配规则

location 指令的作用是根据用户请求的URI来执行不同的应用。

locationn使用的语法为

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

....

}

location 语法说明表

location

[=|~|~*|^~]

uri

{....}

指令

匹配标识

匹配的网站地址

匹配URI后要执行的配置段

与~* 的区别

~  匹配内容区分大小写

~* 匹配内容不区分的小写

!~ 取反

^~ 但多个匹配同时存在,优先匹配 ^~匹配的内容;不做正则表达式的检查 (优先处理)

官方配置示例

location = / {

[ configuration A ]

}

location / {

[ configuration B ]

}

location /documents/ {

[ configuration C ]

}

location ^~ /images/ {

[ configuration D ]

}

location ~* \.(gif|jpg|jpeg)$ {

[ configuration E ]

}

"/"请求将匹配配置A,

"/index.html"请求将匹配配置B,

"/documents/document.html"请求将匹配配置C,

"/images/1.gif"请求将匹配配置D,

"/documents/1.jpg"请求将匹配配置E.

顺序

不用URI及特殊字符组合匹配

匹配说明

1

location = / {}

精确匹配 /

2

location ^~ /image/{

匹配常规字符串,不做正则表达式匹配检查

3

location ~* \.(gif|jpg|jpeg)$ {

正则匹配

4

location /documents/ {

匹配常规字符串,如果有正则,则优先匹配正则

5

location / {

所有location 都不能匹配后的默认匹配