[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並無被使用,你必須本身構造目標urlnginx

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
這塊東西仍是有很多不清楚的地方,繼續研究。
相關文章
相關標籤/搜索