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/

<html>

<head><title>404 Not Found</title></head>

<body>

<center><h1>404 Not Found</h1></center>

<hr><center>nginx/1.28.1</center>

</body>

</html>

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

3.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";

}

}

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

<html>

<head><title>404 Not Found</title></head>

<body>

<center><h1>404 Not Found</h1></center>

<hr><center>nginx/1.28.1</center>

</body>

</html>

root@Nginx conf.d# curl lee.timinglee.org/leeab/test

lee

4.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";

}

}

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/

<html>

<head><title>404 Not Found</title></head>

<body>

<center><h1>404 Not Found</h1></center>

<hr><center>nginx/1.28.1</center>

</body>

</html>

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

<html>

<head><title>404 Not Found</title></head>

<body>

<center><h1>404 Not Found</h1></center>

<hr><center>nginx/1.28.1</center>

</body>

</html>

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

相关推荐
Full Stack Developme2 小时前
JVM 与 Linux 交互的核心原理
linux·运维·jvm
HackTwoHub3 小时前
最新Nessus2026.6.8版本主机漏洞扫描/探测工具Windows/Linux
linux·运维·服务器·安全·web安全·网络安全·安全架构
qq_163135753 小时前
Linux 【04-mkdir命令超详细教程】
linux
qq_163135753 小时前
Linux 【08-mv命令超详细教程】
linux
QWEDDRFTG3 小时前
C13/C19怎么选?服务器电源线电流与接口选型技巧
服务器
原则猫4 小时前
HOOKS 背后机制
前端
码语智行4 小时前
首页导航跳转功能深度解析-系统内和系统外
前端
阿猫的故乡5 小时前
Vue过渡动画从入门到装X:淡入淡出、滑动、列表动画、第三方库全搞定
前端·javascript·vue.js
QWEDDRFTG5 小时前
服务器电源线怎么选?接口、电流、线径、认证一次讲清
服务器
IManiy5 小时前
总结之Vibe Coding前端骨架
前端