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;
