濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > Nginx之proxy_redirect使用詳解

Nginx之proxy_redirect使用詳解

熱門(mén)標(biāo)簽:云會(huì)外呼系統(tǒng) 外呼系統(tǒng)的經(jīng)營(yíng)范圍 申請(qǐng)400電話(huà)價(jià)格多少 安陽(yáng)ai電銷(xiāo)機(jī)器人軟件 涪陵商都400電話(huà)開(kāi)通辦理 柳州市機(jī)器人外呼系統(tǒng)報(bào)價(jià) 廈門(mén)營(yíng)銷(xiāo)外呼系統(tǒng)平臺(tái) 智能電話(huà)機(jī)器人坐席 外呼系統(tǒng)不彈窗

今天在做nginx反向代理apache的時(shí)候出了一點(diǎn)點(diǎn)問(wèn)題,原來(lái)后端apache用的端口是8080通過(guò)反向代理后,使用wireshark抓包發(fā)現(xiàn)location頭域數(shù)值為http://192.168.1.154:8080/wuman/  如果把這個(gè)返回給客戶(hù)端肯定是不可以的,看起來(lái)別扭而且還暴露了apache的具體信息

所以在這里用到了nginx的proxy_redirect指定修改被代理服務(wù)器返回的響應(yīng)頭中的location頭域跟refresh頭域數(shù)值

以下是截取nginx的一小段配置文檔

server {
 listen 80;
 server_name www.boke.com;
 location / {
  proxy_pass http://192.168.1.154:8080;
  proxy_redirect off;
 }
 }

此時(shí)我們通過(guò)curl查看結(jié)果得出

[root@localhost nginx]# curl -I http://www.boke.com/wuman
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Thu, 24 Dec 2015 12:02:00 GMT
Content-Type: text/html; charset=iso-8859-1
Connection: keep-alive
Location: http://192.168.1.154:8080/wuman/

這里location為帶有后端服務(wù)器實(shí)際地址跟端口的響應(yīng)頭信息這樣在實(shí)際線(xiàn)上是不允許的所以這里我們打算通過(guò)proxy_redirect將被代理服務(wù)器的響應(yīng)頭中的location字段進(jìn)行修改后返回給客戶(hù)端

server {
 listen 80;
 server_name www.boke.com;
 location / {
  proxy_pass http://192.168.1.154:8080;
  proxy_redirect http://192.168.1.154:8080/wuman/ http://www.boke.com/wuman/;
 }

server {
 listen 80;
 server_name www.boke.com;
 location / {
  proxy_pass http://192.168.1.154:8080;
  proxy_redirect ~^http://192.168.1.154:8080(.*) http://www.boke.com$1;
 }

則curl查看返回結(jié)果

[root@localhost nginx]# curl -I http://www.boke.com/wuman
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Thu, 24 Dec 2015 12:08:34 GMT
Content-Type: text/html; charset=iso-8859-1
Connection: keep-alive
Location: http://www.boke.com/wuman/

此時(shí)查看location已經(jīng)變成了我們想要的結(jié)果了。 此時(shí)通過(guò)replacement 301重定向到了我們新的頁(yè)面

出處:

proxy_redirect

語(yǔ)法:proxy_redirect [ default|off|redirect replacement ] 

默認(rèn)值:proxy_redirect default 

使用字段:http, server, location 

如果需要修改從被代理服務(wù)器傳來(lái)的應(yīng)答頭中的"Location"和"Refresh"字段,可以用這個(gè)指令設(shè)置。

假設(shè)被代理服務(wù)器返回Location字段為: http://localhost:8000/two/some/uri/

這個(gè)指令: 

proxy_redirect http://localhost:8000/two/ http://frontend/one/;

將Location字段重寫(xiě)為http://frontend/one/some/uri/。

在代替的字段中可以不寫(xiě)服務(wù)器名:

proxy_redirect http://localhost:8000/two/ /;

這樣就使用服務(wù)器的基本名稱(chēng)和端口,即使它來(lái)自非80端口。

如果使用“default”參數(shù),將根據(jù)location和proxy_pass參數(shù)的設(shè)置來(lái)決定。

例如下列兩個(gè)配置等效:

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/;

這個(gè)指令有時(shí)可以重復(fù):

proxy_redirect default;
proxy_redirect http://localhost:8000//; proxy_redirect ; /;

參數(shù)off將在這個(gè)字段中禁止所有的proxy_redirect指令:

proxy_redirect off;
proxy_redirect default;
proxy_redirect http://localhost:8000/ /; proxy_redirect ; /;

利用這個(gè)指令可以為被代理服務(wù)器發(fā)出的相對(duì)重定向增加主機(jī)名:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

標(biāo)簽:南充 福州 綏化 晉城 巴中 蕪湖 撫順 孝感

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Nginx之proxy_redirect使用詳解》,本文關(guān)鍵詞  Nginx,之,proxy,redirect,使用,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Nginx之proxy_redirect使用詳解》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于Nginx之proxy_redirect使用詳解的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    阜宁县| 深水埗区| 平谷区| 曲靖市| 余干县| 时尚| 黄大仙区| 郸城县| 泽普县| 张家港市| 靖西县| 德庆县| 芦溪县| 七台河市| 峡江县| 宜昌市| 贺州市| 乐平市| 福海县| 新沂市| 隆德县| 西青区| 南木林县| 永和县| 青州市| 同心县| 铁力市| 贡觉县| 霞浦县| 万宁市| 即墨市| 崇州市| 长葛市| 崇明县| 亳州市| 潮州市| 佛坪县| 石阡县| 随州市| 永嘉县| 祁东县|