1. 程式人生 > >nginx 之 proxy_redirect詳解

nginx 之 proxy_redirect詳解

proxy_redirect 語法:proxy_redirect [ default|off|redirect replacement ]  預設值:proxy_redirect default  使用欄位:http, server, location  如果需要修改從被代理伺服器傳來的應答頭中的"Location"和"Refresh"欄位,可以用這個指令設定。 假設被代理伺服器返回Location欄位為: http://localhost:8000/two/some/uri/ 這個指令: proxy_redirect http://localhost:8000/two/ http://frontend/one/; 將Location欄位重寫為http://frontend/one/some/uri/。 在代替的欄位中可以不寫伺服器名: proxy_redirect http://localhost:8000/two/ /; 這樣就使用伺服器的基本名稱和埠,即使它來自非80埠。 如果使用“default”引數,將根據location和proxy_pass引數的設定來決定。 例如下列兩個配置等效: location /one/ { proxy_pass http://upstream:port/two/; proxy_redirect default; }   location /one/ { proxy_pass http://upstream:port/two/; proxy_redirect http://upstream:port/two/ /one/; } 在指令中可以使用一些變數: proxy_redirect http://localhost:8000/ http://$host:$server_port/; 這個指令有時可以重複: proxy_redirect default; proxy_redirect http://localhost:8000/ /; proxy_redirect http://www.example.com/ /; 引數off將在這個欄位中禁止所有的proxy_redirect指令: proxy_redirect off; proxy_redirect default; proxy_redirect http://localhost:8000/ /; proxy_redirect http://www.example.com/ /; 利用這個指令可以為被代理伺服器發出的相對重定向增加主機名: proxy_redirect / /;   將被代理伺服器發出的重定向http協議的location改為https協議:proxy_redirect ~^http://([^:]+)(:\d+)?(.*)$ https://$1$2$3;