天天看點

nginx proxy_pass 包含路徑問題

nginx proxy_pass 是可以包含路徑的,同時路徑如果包含/ 以及不包含/的效果是不一樣的

參考

  • 包含路徑
location /api/v1/ {
          proxy_pass http://127.0.0.1/myapi/;
}      

請求之後就變成了: ​

​http://localhost/api/v1/demoapp = > 127.0.0.1/myapi/demoapp​

  • 特殊場景

    對于正則處理的,會忽略包含了的url,其實nginx 會提示你不能包含,我們需要使用的是服務位址以及端口

location  ~* /api/v1/ {
          proxy_pass http://backend/myapi/;
}      

會有錯誤提示

2022/08/31 12:56:07 [emerg] 1#1: "proxy_pass" cannot have URI part in location given by regular expression, or inside named location, or inside "if" statement, or inside "limit_except" block in /usr/local/openresty/nginx/conf/nginx.conf:128      

參考源碼處理

nginx proxy_pass 包含路徑問題

說明

參考資料

繼續閱讀