[nginx]proxy_pass&rewrite知識點


While passing request nginx replaces URI part which corresponds to location with one indicated in proxy_pass directive. But there are two exceptions from this rule when it is not possible to determine what to replace:

if the location is given by regular expression;
if inside proxied location URI is changed by rewrite directive, and this configuration will be used to process request (break):
location  /name/ {
  rewrite      /name/([^/] +)  /users?name=$1  break;
  proxy_pass   http://127.0.0.1;
}
For these cases of URI it is transferred without the mapping.
注意:
兩種情況下不進行代理轉換。
1.location 標段使用正則表達式
2.執行代理動作的location段內使用了rewrite指令對location URI進行了修改,並且使用rewrite后的配置發出請求(break)。

 


A special case is using variables in the proxy_pass statement: The requested URL is not used and you are fully responsible to construct the target URL yourself.

This means, the following is not what you want for rewriting into a zope virtual host monster, as it will proxy always to the same URL (within one server specification):

另一種特例是在proxy_pass語句中使用變量:請求的url並沒有被使用,你必須自己構造目標url

location / {
  proxy_pass   http://127.0.0.1:8080/VirtualHostBase/https/$server_name:443/some/path/VirtualHostRoot;
}
Instead use a combination of rewrite and proxy_pass:上面的語句要寫成下面的:

location / {
  rewrite ^(.*)$ /VirtualHostBase/https/$server_name:443/some/path/VirtualHostRoot$1 break;
  proxy_pass   http://127.0.0.1:8080;
}

 

rewrite的permanent和redirect重寫后的url會在url欄顯示,在數據包的層面,添加了location http header
這塊東西還是有不少不清楚的地方,繼續研究。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM