51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

nginx location 优先级

| 匹配符 | 匹配规则 | 优先级 | |---------|----------------|---------| | = | 精确匹配 | 1 | | ^~ | 以某个字符串开头 | 2 | | ~ | 区分大小写的正则匹配 | 3 | | ~* | 不区分大小写的正则匹配 | 4 | | / | 通用匹配,任何请求都会匹配到 | 5 |

2Nginx常用基础模块(原)-白眉大叔 (baimeidashu.com)

[root@Nginx conf.d]# cat testserver.conf 
server {
    listen 80;
    server_name www.baimei.com;
    location / {
        default_type text/html;
        return 200 "location /";
    }
location =/ {
    default_type text/html;
    return 200 "location =/";
}

location ~ / {
    default_type text/html;
    return 200 "location ~/";
}

# location ^~ / {
#   default_type text/html;
#   return 200 "location ^~";
# }

}


|--------------| | 测试Location效果 |

# 优先级最高符号=
[root@Nginx conf.d]# curl www.baimei.com
location =/

注释掉精确匹配=, 重启Nginx

[root@Nginx ~]# curl www.baimei.com location ~/

注释掉~, 重启Nginx

[root@Nginx ~]# curl www.baimei.com location /


|--------------| | Locaiton应用场景 |

# 通用匹配,任何请求都会匹配到
location / {
    ...
}

严格区分大小写,匹配以.php结尾的都走这个location

location ~ .php$ { ... }

严格区分大小写,匹配以.jsp结尾的都走这个location

location ~ .jsp$ { ... }

不区分大小写匹配,只要用户访问.jpg,gif,png,js,css 都走这条location

location ~* .*.(jpg|gif|png|js|css)$ { ... }

location ~* .(jpg|gif|png|js|css)$ { ... }

不区分大小写匹配

location ~* ".(sql|bak|tgz|tar.gz|.git)$" { ... }

赞(9)
未经允许不得转载:工具盒子 » nginx location 优先级