nginx配置中一个不起眼字符“/“

编程入门 行业动态 更新时间:2024-10-24 22:22:26

nginx配置中一个<a href=https://www.elefans.com/category/jswz/34/1767245.html style=不起眼字符“/“"/>

nginx配置中一个不起眼字符“/“

文章目录

  • nginx配置中一个不起眼字符"/"
    • 1.问题
    • 2.`location`
    • 3.`proxy_pass`
    • 4.总结
    • 5.案例总结

nginx配置中一个不起眼字符"/"

1.问题

[!DANGER]

nginx在配置proxy_pass代理转接和location时,多加少加“/”字符会产生完全不一样的效果。

2.location

[!TIP]

nginx每个location都是一个匹配目录,nginx的策略是:访问请求来时,会对访问地址进行解析,从上到下逐个匹配,匹配上就执行对应location大括号中的策略,并根据策略对请求作出相应。

# 进行精准匹配
location /dw/ { # /dw/proxy_pass http://127.0.0.1:8080; 
}# 进行模糊匹配location /dw { # /dw*proxy_pass http://127.0.0.1:8080; 
}

3.proxy_pass

# 情况1
location /dw/ { # http://127.0.0.1:8080/index.htmlproxy_pass http://127.0.0.1:8080/; 
}# 情况2
location /dw/ { # http://127.0.0.1:8080/dw/index.htmlproxy_pass http://127.0.0.1:8080; 
}# 情况3
location /dw/ { # http://127.0.0.1:8080/testindex.htmlproxy_pass http://127.0.0.1:8080/test; 
}# 情况4
location /dw/ { # http://127.0.0.1:8080/test/index.htmlproxy_pass http://127.0.0.1:8080/test/; 
}

4.总结

[!DANGER]

location目录后加"/",只能匹配目录,不加“/”不仅可以匹配目录还对目录进行模糊匹配。而proxy_pass无论加不加“/”,代理跳转地址都直接拼接。

5.案例总结

server { listen 80; server_name localhost; # http://localhost/wddd01/xxx -> http://localhost:8080/wddd01/xxxlocation /wddd01/ { proxy_pass http://localhost:8080; }# http://localhost/wddd02/xxx -> http://localhost:8080/xxx location /wddd02/ { proxy_pass http://localhost:8080/; }# http://localhost/wddd03/xxx -> http://localhost:8080/wddd03*/xxx location /wddd03 { proxy_pass http://localhost:8080; }# http://localhost/wddd04/xxx -> http://localhost:8080//xxx,请注意这里的双斜线,好好分析一下。location /wddd04 { proxy_pass http://localhost:8080/; }# http://localhost/wddd05/xxx -> http://localhost:8080/hahaxxx,请注意这里的haha和xxx之间没有斜杠,分析一下原因。location /wddd05/ { proxy_pass http://localhost:8080/haha; }# http://localhost/api6/xxx -> http://localhost:8080/haha/xxx location /wddd06/ { proxy_pass http://localhost:8080/haha/; }# http://localhost/wddd07/xxx -> http://localhost:8080/haha/xxx location /wddd07 { proxy_pass http://localhost:8080/haha; } # http://localhost/wddd08/xxx -> http://localhost:8080/haha//xxx,请注意这里的双斜杠。location /wddd08 { proxy_pass http://localhost:8080/haha/; }
}

更多推荐

nginx配置中一个不起眼字符“/“

本文发布于:2024-02-11 18:14:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1682517.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不起眼   字符   nginx

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!