location 的斜杠問題比較好理解,不帶斜杠的是模糊匹配。例如:
location /doc
- 可以匹配 /doc/index.html,也可以匹配 /docs/index.html。
location /doc/? ? ?強烈建議使用這種
- 只能匹配 /doc/index.html,不能匹配 /docs/index.html。
??對于 proxy_pass 的斜杠問題,得結(jié)合 location 來講。?
(1)proxy_pass 不帶URL方式
?這種 IP、端口后面沒有 /,是不帶 URI 的方式,nginx 會保留 location 中的路徑。所以,訪問 http://127.0.0.1/docs/,實際上訪問的是 http://127.0.0.1:8080/docs/。
訪問??http://127.0.0.1/docs/? ----------------------------> 實際訪問? http://127.0.0.1:8080/docs/
location /docs/ {
proxy_pass http://127.0.0.1:8080;
}
location /jenkins/ {
proxy_set_header host $host;
proxy_pass http://192.168.11.128:8080;
}
tomcat日志
192.168.11.128 - - [04/Jun/2023:07:50:59 +0800] "GET /jenkins/index.html HTTP/1.0" 404 648
(2)proxy_pass 帶 URI 的方式
這種 IP、端口后面有 /,是帶 URI 的方式,nginx 將會使用別名的方式來對 URL 進行替換。所以,訪問 http://127.0.0.1/docs/,實際上訪問的是 http://127.0.0.1:8080/,/docs/ 替換成了 /。
訪問 http://127.0.0.1/docs/? ?-------------> 實際訪問 http://127.0.0.1:8080/
location /docs/ {
proxy_pass http://127.0.0.1:8080/;
}
location /article/ {
proxy_set_header host $host;
proxy_pass http://192.168.11.128:8080/;
}
后端tomcat日志
192.168.11.128 - - [04/Jun/2023:08:08:56 +0800] "GET /index.html HTTP/1.0" 404 648
?對帶 URI 的方式進行擴展:
location /article/ {
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/。?
?舉例如下:
location /article/ {
proxy_set_header host $host;
proxy_pass http://192.168.11.128:8080/docs/;
}
192.168.11.128 - - [04/Jun/2023:08:00:26 +0800] "GET /docs/index.html HTTP/1.0" 404 648
?經(jīng)常出錯的也正是這種帶 URI 方式的寫法 !?。。?!
例如:
location /article/ {
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。
例如:
匹配模式 |
分為兩種模式:
使用前綴匹配時,匹配剩余的路徑將透傳到后端。文章來源:http://www.zghlxwxcb.cn/news/detail-707256.html 例如,使用前綴匹配,前端請求路徑定義為/test/,后端請求路徑定義為/test2/,通過/test/AA/CC訪問API,則后端收到的請求url為/test2/AA/CC。文章來源地址http://www.zghlxwxcb.cn/news/detail-707256.html |
到了這里,關(guān)于Nginx 中 location 和 proxy_pass 的斜杠問題的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!