location 的斜杠问题比较好理解,不带斜杠的是模糊匹配。例如:
可以匹配 /doc/index.html,也可以匹配 /docs/index.html。
只能匹配 /doc/index.html,不能匹配 /docs/index.html。
对于 proxy_pass 的斜杠问题,得结合 location 来讲。
proxy_pass http://127.0.0.1:8080;
}
这种 IP、端口后面没有 /,是不带 URI 的方式,nginx 会保留 location 中的路径。所以,访问 http://127.0.0.1/docs/,实际上访问的是 http://127.0.0.1:8080/docs/。
proxy_pass http://127.0.0.1:8080/;
}
这种 IP、端口后面有 /,是带 URI 的方式,nginx 将会使用别名的方式来对 URL 进行替换。所以,访问 http://127.0.0.1/docs/,实际上访问的是 http://127.0.0.1:8080/,/docs/ 替换成了 /。
对带 URI 的方式进行扩展:
proxy_pass http://127.0.0.1:8080/docs/;
}
这种同样 IP、端口后面有 /,也是带 URI 的方式。所以,访问 http://127.0.0.1/article/,实际上访问的是 http://127.0.0.1:8080/docs/,/article/ 替换成了 /docs/。
经常出错的也正是这种带 URI 方式的写法,例如:
proxy_pass http://127.0.0.1:8080/docs;
}
当访问 http://127.0.0.1/article/index.html 的时候,本意是想访问 http://127.0.0.1:8080/docs/index.html。但是,/article/ 替换成了 /docs,所以,实际访问的是 http://127.0.0.1:8080/docsindex.html。
Copyright © 2005-2023 by www.ricensoftwares.com.cn All Rights Reserved.