**Nginx location 各参数跳转经常迷糊,做了测试记录一下。 20240828_1.png

指定静态资源路径用的关键字root,还可以用alias,那么root和alias的区别是什么? 用root属性指定的值是要加入到最终路径中的,匹配条件会拼接到路径中 用alias属性指定的值不需要加入到最终路径中

location /html/ {
 root /usr/share/nginx/;
}

Root指定路径,将把条件拼凑进来, 访问http://192.168.2.101/html/ 其实跳转到/usr/share/nginx/html/下面的index.html文件 20240828_2.png

Alias:如果用alias,那么通过浏览器进行请求的时候,alias也是指定到/usr/share/nginx/html路径下,但是会匹配默认的index.html,但是此时就不能使用”=”来进行精确匹配,alias指定的路径结尾要加”/” 访问http://192.168.2.101/jump/1.txt其实看日志访问的路径是"/usr/share/nginx/html/1.txt"

通过^~方式实现匹配: 例如:下面配置文件有两条规则,分别匹配url以字母a开头,但是长度不同如下: 20240828_3.png

root@192.168.2.101 /etc/nginx/conf.d]$curl -I 192.168.2.101/a/
HTTP/1.1 111 
Server: nginx/1.20.1
[root@192.168.2.101 /etc/nginx/conf.d]$curl -I 192.168.2.101/a/b/
HTTP/1.1 222 
Server: nginx/1.20.1
root@192.168.2.101 /etc/nginx/conf.d]$curl -I 192.168.2.101/a/b/c/
HTTP/1.1 222 
Server: nginx/1.20.1
[root@192.168.2.101 /etc/nginx/conf.d]$curl -I 192.168.2.101/a/b/c/d/
HTTP/1.1 333 
Server: nginx/1.20.1
[root@192.168.2.101 /etc/nginx/conf.d]$curl -I 192.168.2.101/a/b/c/d/1/as/e
HTTP/1.1 333 
Server: nginx/1.20.1

两条规则同时被匹配成功,但是第二条规则比较长,因此第二条规则优先被匹配,如果^~匹配成功了,那么表示阻断正则表达式,不再进行正则匹配。 通过”~”方式实现匹配: 上图中的匹配规则都是通过”^~”方式来实现的,那么在匹配最长规则的时候又分为两种情况: 第一种:最长规则通过^~来实现匹配(上图中的最长规则就是通过^~实现) 第二种:最长的规则不是通过^~实现匹配,而是通过普通匹配来实现 当最长规则是通过普通匹配的时候,将会继续正则匹配, 只要有一个正则成功,则使用这个正则的location,停止继续匹配,返回结果 20240828_4.png

添加一条普通匹配,测试如下:

[root@192.168.2.101 /etc/nginx/conf.d]$curl -I 192.168.2.101/a/b/
HTTP/1.1 222 
Server: nginx/1.20.1
[root@192.168.2.101 /etc/nginx/conf.d]$curl -I 192.168.2.101/a/b/c
HTTP/1.1 222 
Server: nginx/1.20.1
[root@192.168.2.101 /etc/nginx/conf.d]$curl -I 192.168.2.101/a/b/c/d/
HTTP/1.1 333 
Server: nginx/1.20.1
[root@192.168.2.101 /etc/nginx/conf.d]$curl -I 192.168.2.101/a/b/c/d/1/s/a/3/1/21
HTTP/1.1 333 
Server: nginx/1.20.1

请求/a/b/的时候,首先会到达第二条规则(最长规则),由于第二条规则为普通匹配(不是^~匹配),因此会继续去匹配正则,最终返回222;

通过"~"方式实现匹配: ” ~ ” 表示不区分大小写的正则匹配 例如:通过url请求/a/b/或者/A/B/,查看返回状态码,如图: 20240828_5.png

[root@192.168.2.101 /etc/nginx/conf.d]$curl -I 192.168.2.101/a/b/c
HTTP/1.1 666 
Server: nginx/1.20.1
[root@192.168.2.101 /etc/nginx/conf.d]$curl -I 192.168.2.101/A/b/
HTTP/1.1 666 
Server: nginx/1.20.1
[root@192.168.2.101 /etc/nginx/conf.d]$curl -I 192.168.2.101/a/b/
HTTP/1.1 666 
Server: nginx/1.20.1

常用的匹配图片后缀,返回指定信息: 20240828_6.png

[root@192.168.2.101 /etc/nginx/conf.d]$curl -I 192.168.2.101/1.jpg
HTTP/1.1 777 
[root@192.168.2.101 /etc/nginx/conf.d]$curl -I 192.168.2.101/1.JPG
HTTP/1.1 777 
[root@192.168.2.101 /etc/nginx/conf.d]$curl -I 192.168.2.101/1.png
HTTP/1.1 777 

1.都没有”/”

 location  /aaa {
proxy_pass http://127.0.0.1:8021;
}

注:8021 root /data/nginx/web/;

20240828_7.png

说明:8021端口为本机另外一个站点,如果匹配路径和proxy_pass后都没有”/”,那么此时访问http://192.168.2.101/aaa,默认将请求到http://192.168.2.101:8021/aaa/index.html的内容,定义内容为:aaa page!,通过IP请求如图: 20240828_8.png 20240828_9.png

2.proxy_pass,proxy_pass最后面没有斜杠,匹配路径有斜杠(/aaa/) 20240828_10.png 说明:proxy_pass最后面没有斜杠”/”,此时通过浏览器请求http://192.168.2.101/aaa/, 那么实际访问的地址就是http://192.168.2.101:8021/aaa/,会将匹配路径/aaa一起加过去。 3. proxy_pass最后面有斜杠 “/”,匹配路径也有斜杠(/aaa/), 20240828_11.png 说明:proxy_pass最后面有斜杠”/”,此时通过浏览器请求http://192.168.2.101/aaa/, 那么实际访问的地址就是http://192.168.2.101:8021,会将/aaa抛弃的,如图: 20240828_12.png 20240828_13.png

4.proxy_pass后面还有其他路径但是最后没有 “/”, 匹配路径也有斜杠(/aaa/)  20240828_14.png 说明,此时通过浏览器访问http://192.168.2.101/aaa/index.html,实际请求的是http://192.168.2.101:8021/bbbindex.html(注意位置是默认8021web root路径下,/data/nginx/web/bbbindex.html 不是bbb路径下, open() "/data/nginx/web/bbbindex.html" failed (2: No such file or directory), client: 127.0.0.1, server: 192.168.2.101, request: "GET /bbbindex.html HTTP/1.0", host: "127.0.0.1:8021"

如果proxy_pass的路径为/bbb/ccc,

 location  /aaa/ {
proxy_pass http://127.0.0.1:8021/bbb/ccc;
}

那么实际请求的就是bbb路径下的cccindex.html, "/data/nginx/web/bbb/cccindex.html" open() "/data/nginx/web/bbb/cccindex.html" failed (2: No such file or directory), client: 127.0.0.1, server: 192.168.2.101, request: "GET /bbb/cccindex.html HTTP/1.0", host: "127.0.0.1:8021"

) 5. proxy_pass后面还有其他路径但是最后有 “/”, 匹配路径也有斜杠(/aaa/) , 20240828_15.png 说明:此时通过浏览器访问http://192.168.2.101/aaa/index.html, 实际访问的是http://192.168.2.101:8021/bbb/index.html 20240828_16.png20240828_17.png

GET /bbb/index.html HTTP/1.0" 200 11 参考:https://www.modb.pro/db/391597**