nginx配置中proxy_redirect的作用(轉)
來自http://wntest.ustc.edu.cn/mine/wordpress/?p=400
及 http://hi.baidu.com/171892549/blog/item/0ec6aaef22acbb1dfdfa3ca7.html
NGINX的proxy_redirect功能比較強大,其作用是對發送給客戶端的URL進行修改。以例子說明:
server {
listen 80;
server_name test.abc.com;
location / {
proxy_pass http://10.10.10.1:9080;
}
}這段配置一般情況下都正常,但偶爾會出錯, 錯誤在什么地方呢? 抓包發現服務器給客戶端的跳轉指令里加了端口號,如 Location: http://test.abc.com:9080/abc.html 。因為nginx服務器偵聽的是80端口,所以這樣的URL給了客戶端,必然會出錯.針對這種情況, 加一條proxy_redirect指令: proxy_redirect http://test.abc.com:9080/ / ,把所有“http://test.abc.com:9080/”的內容替換成“/”再發給客戶端,就解決了。
server {
listen 80;
server_name test.abc.com;
proxy_redirect http://test.abc.com:9080/ /;
location / {
proxy_pass http://10.10.10.1:9080;
}
}
http://nginx.179401.cn/
聖地啊 加紅 加粗~!!
出處:http://nginx.179401.cn/StandardHTTPModules/HTTPProxy.html
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/ /;
利用這個指令可以為被代理服務器發出的相對重定向增加主機名:
及 http://hi.baidu.com/171892549/blog/item/0ec6aaef22acbb1dfdfa3ca7.html
NGINX的proxy_redirect功能比較強大,其作用是對發送給客戶端的URL進行修改。以例子說明:
server {
listen 80;
server_name test.abc.com;
location / {
proxy_pass http://10.10.10.1:9080;
}
}這段配置一般情況下都正常,但偶爾會出錯, 錯誤在什么地方呢? 抓包發現服務器給客戶端的跳轉指令里加了端口號,如 Location: http://test.abc.com:9080/abc.html 。因為nginx服務器偵聽的是80端口,所以這樣的URL給了客戶端,必然會出錯.針對這種情況, 加一條proxy_redirect指令: proxy_redirect http://test.abc.com:9080/ / ,把所有“http://test.abc.com:9080/”的內容替換成“/”再發給客戶端,就解決了。
server {
listen 80;
server_name test.abc.com;
proxy_redirect http://test.abc.com:9080/ /;
location / {
proxy_pass http://10.10.10.1:9080;
}
}
http://nginx.179401.cn/
聖地啊 加紅 加粗~!!
出處:http://nginx.179401.cn/StandardHTTPModules/HTTPProxy.html
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/ /;
利用這個指令可以為被代理服務器發出的相對重定向增加主機名: