Skip to content

匹配优先级

精确匹配 (=)

location = /exactpath {
    # 只有 /exactpath 这个 URI 才会匹配
}

前缀匹配(^~)

location ^~ /images/ {
    # 处理所有以 /images/ 开头的请求,优先匹配,匹配到后,不再检查其他规则,特别是正则规则。
}

正则匹配 (~ 或 ~*)

location ~ \.jpg$ {
    # 匹配所有以 .jpg 结尾的 URI,大小写敏感
}
location ~* \.(gif|jpg|jpeg)$ {
    # 匹配 .gif、.jpg、.jpeg 结尾的 URI,大小写不敏感
}

普通前缀匹配

xml
location /images/ {
    # 处理所有以 /images/ 开头的请求,普通前缀匹配,Nginx 会继续检查其他匹配规则,直到找到最精确的匹配。
}