Location 字符匹配详解

1.Location后什么都不带直接指定目录

root@Nginx conf.d\]# vim vhosts.conf server { listen 80; server_name lee.timinglee.org; location /null { return 200 "/null-1"; } } \[root@Nginx conf.d\]# curl lee.timinglee.org/null/ /null-1 \[root@Nginx conf.d\]# curl lee.timinglee.org/NULL/ \ \\404 Not Found\\ \ \\404 Not Found\\ \\nginx/1.28.1\ \ \

2.location 后用 =

root@Nginx conf.d\]# vim vhosts.conf server { listen 80; server_name lee.timinglee.org; location /null { return 200 "null-1"; } location = /null { #精确匹配到此结束 return 200 "null-2"; } location \~ /null { return 200 "null-3"; } } \[root@Nginx conf.d\]# nginx -s reload \[root@Nginx conf.d\]# curl lee.timinglee.org/null null-2

root@Nginx conf.d\]# vim vhosts.conf server { listen 80; server_name lee.timinglee.org; location /null { return 200 "null-1"; } location = /null { return 200 "null-2"; } location \~ /null { return 200 "null-3"; } location \^\~ /lee { return 200 "lee"; } } \[root@Nginx conf.d\]# nginx -s reload lee \[root@Nginx conf.d\]# curl lee.timinglee.org/lee lee \[root@Nginx conf.d\]# curl lee.timinglee.org/test/lee \ \\404 Not Found\\ \ \\404 Not Found\\ \\nginx/1.28.1\ \ \ \[root@Nginx conf.d\]# curl lee.timinglee.org/leeab/test lee

root@Nginx conf.d\]# vim vhosts.conf server { listen 80; server_name lee.timinglee.org; location /null { return 200 "null-1"; } location = /null { return 200 "null-2"; } location \~ /null { return 200 "null-3"; } location \^\~ /lee { return 200 "lee"; } location \~ /timing/ { return 200 "timing"; } } \[root@Nginx conf.d\]# nginx -s reload \[root@Nginx conf.d\]# curl lee.timinglee.org/timinga/ timing \[root@Nginx conf.d\]# curl lee.timinglee.org/timing/ timing \[root@Nginx conf.d\]# curl lee.timinglee.org/a/timing/ timing \[root@Nginx conf.d\]# curl lee.timinglee.org/a/timinga/ timing \[root@Nginx conf.d\]# curl lee.timinglee.org/a/atiming/ \ \\404 Not Found\\ \ \\404 Not Found\\ \\nginx/1.28.1\ \ \

5.location 后用"~*"

root@Nginx conf.d\]# vim vhosts.conf server { listen 80; server_name lee.timinglee.org; location /null { return 200 "null-1"; } location = /null { return 200 "null-2"; } location \~ /null { return 200 "null-3"; } location \^\~ /lee { return 200 "lee"; } location \~ /timing/ { return 200 "timing"; } location \~\* /timinglee { return 200 "timinglee"; } } \[root@Nginx conf.d\]# nginx -s reload \[root@Nginx conf.d\]# curl lee.timinglee.org/Timinglee/ timinglee \[root@Nginx conf.d\]# curl lee.timinglee.org/timinglee/ timinglee \[root@Nginx conf.d\]# curl lee.timinglee.org/timinglee/a timinglee \[root@Nginx conf.d\]# curl lee.timinglee.org/a/timinglee/a timinglee \[root@Nginx conf.d\]# curl lee.timinglee.org/a/atiminglee/a \ \\404 Not Found\\ \ \\404 Not Found\\ \\nginx/1.28.1\ \ \ \[root@Nginx conf.d\]# curl lee.timinglee.org/a/timingleea/a timinglee \[root@Nginx conf.d\]# curl lee.timinglee.org/a/Timinglee/a timinglee

6.location 后用"\"

root@Nginx conf.d\]# vim vhosts.conf server { listen 80; server_name lee.timinglee.org; location /null { return 200 "null-1"; } location = /null { return 200 "null-2"; } location \~ /null { return 200 "null-3"; } location \^\~ /lee { return 200 "lee"; } location \~ /timing/ { return 200 "timing"; } location \~\* /timinglee { return 200 "timinglee"; } location \~\* \\.(img\|php\|jsp)$ { return 200 "app"; } } \[root@Nginx conf.d\]# nginx -s reload \[root@Nginx conf.d\]# curl lee.timinglee.org/test.php app \[root@Nginx conf.d\]# curl lee.timinglee.org/test.jsp app

相关推荐
Gofarlic_oms14 小时前
利用API实现ANSYS许可证管理自动化集成
运维·服务器·开发语言·matlab·自动化·负载均衡
小码哥_常5 小时前
安卓开发秘籍:解锁10大性能优化秘诀
前端
倔强的石头1066 小时前
【Linux指南】基础IO系列(八):实战衔接 —— 给微型 Shell 添加完整重定向功能
linux·运维·服务器
try2find6 小时前
打印ascii码报错问题
java·linux·前端
郑州光合科技余经理6 小时前
同城O2O海外版二次开发实战:从支付网关到配送算法
开发语言·前端·后端·算法·架构·uni-app·php
Ujimatsu7 小时前
虚拟机安装Ubuntu 26.04.x及其常用软件(2026.4)
linux·运维·ubuntu
冰暮流星7 小时前
javascript事件案例-全选框案例
服务器·前端·javascript
Csvn8 小时前
前端性能优化实战指南
前端
Moment8 小时前
2026 年,AI 全栈时代到了,前端简历别再只写前端技术了 🫠🫠🫠
前端·后端·面试
糯米团子7498 小时前
Web Worker
开发语言·前端·javascript