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

相关推荐
程序员黑豆13 分钟前
鸿蒙应用开发:6种图片加载方式详解
前端·华为·harmonyos
半个落月15 分钟前
用 React 搭一个 WebGPU 模型加载页:从状态驱动到可复用进度条
前端·react.js
雪隐38 分钟前
个人电脑玩AI-13让5060 Ti给你打工——我用 0.9B 小模型终结了"谁来记会议纪要"这个世纪难题
前端·人工智能·后端
橘子星41 分钟前
我一个前端切图仔,凭什么能在浏览器里跑大模型?
前端·javascript·前端框架
张小姐的猫44 分钟前
【Linux】网络编程 —— HTTP协议(上)
linux·运维·服务器·网络·http·单例模式·策略模式
国科安芯1 小时前
星间光链路:AS32S601型抗辐射MCU在空间激光通信终端控制中的技术实现
服务器·网络·单片机·嵌入式硬件·物联网·安全·信息与通信
70asunflower1 小时前
初学者理解 Web 工作原理(完全教程)
前端
爱勇宝1 小时前
《道德经》第 7 章:真正厉害的领导者,不抢主角
前端·后端·程序员
观远数据1 小时前
Excel到数据资产池:文件数据入湖的治理规范怎么建
前端·javascript·excel
CoderWeen1 小时前
我写了个能一步步点着看的 Dijkstra 可视化项目(Vue3 + Leaflet + Generator)
前端·javascript·vue.js