nginx配置中proxy_redirect的作用(转)
生活随笔
收集整理的這篇文章主要介紹了
nginx配置中proxy_redirect的作用(转)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
來自http://wntest.ustc.edu.cn/mine/wordpress/?p=400?
及 http://hi.baidu.com/171892549/blog/item/0ec6aaef22acbb1dfdfa3ca7.html?
NGINX的proxy_redirect功能比較強大,其作用是對發(fā)送給客戶端的URL進行修改。以例子說明:?
? ?server {?
? ? ? ?listen ? ? ? 80;?
? ? ? ?server_name ?test.abc.com;?
? ? ? ?location / {?
? ? ? ? ? ? proxy_pass http://10.10.10.1:9080;?
? ? ? ?}?
? ?}這段配置一般情況下都正常,但偶爾會出錯, 錯誤在什么地方呢? 抓包發(fā)現(xiàn)服務器給客戶端的跳轉(zhuǎn)指令里加了端口號,如 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/”的內(nèi)容替換成“/”再發(fā)給客戶端,就解決了。?
? ?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”參數(shù),將根據(jù)location和proxy_pass參數(shù)的設置來決定。?
例如下列兩個配置等效:?
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/ ? /;?
參數(shù)off將在這個字段中禁止所有的proxy_redirect指令:?
proxy_redirect ? off; ?proxy_redirect ? default; ?proxy_redirect ? http://localhost:8000/ ? ?/; ?proxy_redirect ? http://www.example.com/ ? /;?
利用這個指令可以為被代理服務器發(fā)出的相對重定向增加主機名:
及 http://hi.baidu.com/171892549/blog/item/0ec6aaef22acbb1dfdfa3ca7.html?
NGINX的proxy_redirect功能比較強大,其作用是對發(fā)送給客戶端的URL進行修改。以例子說明:?
? ?server {?
? ? ? ?listen ? ? ? 80;?
? ? ? ?server_name ?test.abc.com;?
? ? ? ?location / {?
? ? ? ? ? ? proxy_pass http://10.10.10.1:9080;?
? ? ? ?}?
? ?}這段配置一般情況下都正常,但偶爾會出錯, 錯誤在什么地方呢? 抓包發(fā)現(xiàn)服務器給客戶端的跳轉(zhuǎn)指令里加了端口號,如 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/”的內(nèi)容替換成“/”再發(fā)給客戶端,就解決了。?
? ?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”參數(shù),將根據(jù)location和proxy_pass參數(shù)的設置來決定。?
例如下列兩個配置等效:?
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/ ? /;?
參數(shù)off將在這個字段中禁止所有的proxy_redirect指令:?
proxy_redirect ? off; ?proxy_redirect ? default; ?proxy_redirect ? http://localhost:8000/ ? ?/; ?proxy_redirect ? http://www.example.com/ ? /;?
利用這個指令可以為被代理服務器發(fā)出的相對重定向增加主機名:
總結(jié)
以上是生活随笔為你收集整理的nginx配置中proxy_redirect的作用(转)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: nginx 静态文件缓存
- 下一篇: 新版电子发票取消发票章