nginx location匹配顺序

例子来源以下地址

https://github.com/trimstray/nginx-admins-handbook#introduction

假设配置如下

server {

 listen           80;
 server_name      xyz.com www.xyz.com;

 location ~ ^/(media|static)/ {
  root            /var/www/xyz.com/static;
  expires         10d;
 }

 location ~* ^/(media2|static2) {
  root            /var/www/xyz.com/static2;
  expires         20d;
 }

 location /static3 {
  root            /var/www/xyz.com/static3;
 }

 location ^~ /static4 {
  root            /var/www/xyz.com/static4;
 }

 location = /api {
  proxy_pass      http://127.0.0.1:8080;
 }

 location / {
  proxy_pass      http://127.0.0.1:8080;
 }

 location /backend {
  proxy_pass      http://127.0.0.1:8080;
 }

 location ~ logo.xcf$ {
  root            /var/www/logo;
  expires         48h;
 }

 location ~* .(png|ico|gif|xcf)$ {
  root            /var/www/img;
  expires         24h;
 }

 location ~ logo.ico$ {
  root            /var/www/logo;
  expires         96h;
 }

 location ~ logo.jpg$ {
  root            /var/www/logo;
  expires         48h;
 }

}

匹配规则如下

请求URL 相匹配的location 最终匹配
/ 1) prefix match for / /
/css 1) prefix match for / /
/api 1) exact match for /api /api
/api/ 1) prefix match for / /
/backend 1) prefix match for /2) prefix match for /backend /backend
/static 1) prefix match for / /
/static/header.png 1) prefix match for /2) case sensitive regex match for ^/(media|static)/ ^/(media|static)/
/static/logo.jpg 1) prefix match for /2) case sensitive regex match for ^/(media|static)/ ^/(media|static)/
/media2 1) prefix match for /2) case insensitive regex match for ^/(media2|static2) ^/(media2|static2)
/media2/ 1) prefix match for /2) case insensitive regex match for ^/(media2|static2) ^/(media2|static2)
/static2/logo.jpg 1) prefix match for /2) case insensitive regex match for ^/(media2|static2) ^/(media2|static2)
/static2/logo.png 1) prefix match for /2) case insensitive regex match for ^/(media2|static2) ^/(media2|static2)
/static3/logo.jpg 1) prefix match for /static32) prefix match for /3) case sensitive regex match for logo.jpg$ logo.jpg$
/static3/logo.png 1) prefix match for /static32) prefix match for /3) case insensitive regex match for .(png|ico|gif|xcf)$ .(png|ico|gif|xcf)$
/static4/logo.jpg 1) priority prefix match for /static42) prefix match for / /static4
/static4/logo.png 1) priority prefix match for /static42) prefix match for / /static4
/static5/logo.jpg 1) prefix match for /2) case sensitive regex match for logo.jpg$ logo.jpg$
/static5/logo.png 1) prefix match for /2) case insensitive regex match for .(png|ico|gif|xcf)$ .(png|ico|gif|xcf)$
/static5/logo.xcf 1) prefix match for /2) case sensitive regex match for logo.xcf$ logo.xcf$
/static5/logo.ico 1) prefix match for /2) case insensitive regex match for .(png|ico|gif|xcf)$ .(png|ico|gif|xcf)$

匹配顺序说明

nginx根据uri进行最优匹配

    1. 基于前缀的NGINX位置匹配(没有正则表达式)。每个位置都将根据请求URI进行检查
    1. NGINX搜索精确的匹配。如果=修饰符与请求URI完全匹配,则立即选择此特定位置块
    1. 如果没有找到精确的位置块(即没有相应的=修饰符),NGINX将继续使用非精确的前缀。它从这个URI的最长匹配前缀位置开始,方法如下:
    • 3.1 如果最长匹配前缀位置有^~修饰符,NGINX将立即停止搜索并选择该位置。
    • 3.2 假设最长匹配前缀位置不使用^~修饰符,匹配将被临时存储,并继续执行。
    1. 一旦选择并存储了最长匹配前缀位置,NGINX就会继续计算区分大小写和不敏感的正则表达式位置。第一个适合URI的正则表达式位置将立即被选中来处理请求
    1. 如果没有找到匹配请求URI的正则表达式位置,则选择先前存储的前缀位置来服务请求

你可能感兴趣的:(nginx location匹配顺序)