nginx location proxy_pass 后面的url 加与不加/的区别

location 拦截特定的路径时 location /test test后面带/和不带/有什么区别呢
proxy_pass
如果test后不带/
表示会拦截 如/test /test01 /testxxxxx 这一类请求,只要是以test开头的请求都会被拦截

如果test后带/
表示只会拦截/test 或者/test/xxxx等相关请求

这里我们分4种情况讨论

这里我们请求的网站为:fffmo.com/static/a.html

整个配置文件是

server{
port  80,
server name  fffmo.com/

location /static{
proxy_pass  fffmo.com
}

location /static{
proxy_pass  fffmo.com/
}

location /static/{
proxy_pass  fffmo.com
}

location /static/{
proxy_pass  fffmo.com/
}

我们分开来讲:

location后没有/ 转发网站没有/

#fffmo.com->server name
# :80 ---------> port
#/statc ------->location
#/a.html ------>proxy_pass 

location /static{
proxy_pass  fffmo.com
}

最后网址经过nginx转向到的网址是 fffmo.com/static/a.html

location后没有/ 转发网站有/

#fffmo.com---->server name
# :80 ------------> port
#/statc ---------->location
#/a.html --------->proxy_pass 

location /static{
proxy_pass  fffmo.com/
}

最后网址经过nginx转向到的网址是 fffmo.com/a.html

location后有/ 转发网站没有/

#fffmo.com-->server name
# :80 ------------> port
#/statc/ ---------->location
#a.html --------->proxy_pass 

location /static/{
proxy_pass  fffmo.com
}

最后网址经过nginx转向到的网址是 fffmo.com/static/a.html

location后有/ 转发网站有/

#fffmo.com-->server name
# :80 ------------> port
#/statc/ ---------->location(path1)
#a.html --------->proxy_pass (path2)

location /static/{
proxy_pass  fffmo.com/
}

最后网址经过nginx转向到的网址是 fffmo.com/a.html

总结:
从这四种我们可以的看出,当nginx里面匹配时可以把端口后的参数分为path1+path2(其中我在上方标注的location属于path1,proxy_pass属于path2)

proxy_pass
里面是ip:port+/时nginx最后匹配的网址是 proxy_pass的内容加上path2
里面是ip:port时nginx最后匹配的网址是 proxy_pass的内容加上path1+path2

随机文章