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

相关推荐
七歌杜金房12 小时前
我终于又有了自己的 Linux 电脑
linux·debian·mac
kyriewen12 小时前
Anthropic 估值逼近万亿美元,Claude Sonnet 5 + Claude Science 一天两连发
前端·ai编程·claude
小徐_233314 小时前
Wot UI 2.2.0 发布:Button 新增 subtle,VideoPreview 预览体验继续增强
前端·微信小程序·uni-app
天蓝色的鱼鱼16 小时前
关于 CSS 你可能不知道的属性,但关键时刻很有用
前端·css
泯泷17 小时前
第 2 篇:设计第一套字节码:Opcode、Instruction 与 Constant Pool
前端·javascript·安全
妙码生花17 小时前
从 PHP 到 AI + Golang,程序员自救转型手记(十五):优化细节、网络请求封装
前端·后端·ai编程
泯泷17 小时前
第 1 篇:从 1 + 2 开始:亲手写出第一台 JSVM
前端·javascript·安全
团团崽_七分甜17 小时前
Spring Boot 核心知识点总结
前端
lichenyang45317 小时前
从一个按钮开始,理解 ASCF 框架到底在做什么
前端