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