Nginx之proxy_redirect使用詳解
今天在做nginx反向代理apache的時候出了一點(diǎn)點(diǎn)問題,原來后端apache用的端口是8080通過反向代理后,使用wireshark抓包發(fā)現(xiàn)location頭域數(shù)值為http://192.168.1.154:8080/wuman/ 如果把這個返回給客戶端肯定是不可以的,看起來別扭而且還暴露了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; } }
此時我們通過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í)際線上是不允許的所以這里我們打算通過proxy_redirect將被代理服務(wù)器的響應(yīng)頭中的location字段進(jìn)行修改后返回給客戶端
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/
此時查看location已經(jīng)變成了我們想要的結(jié)果了。 此時通過replacement 301重定向到了我們新的頁面
出處:
proxy_redirect
語法:proxy_redirect [ default|off|redirect replacement ]
默認(rèn)值:proxy_redirect default
使用字段:http, server, location
如果需要修改從被代理服務(wù)器傳來的應(yīng)答頭中的"Location"和"Refresh"字段,可以用這個指令設(shè)置。
假設(shè)被代理服務(wù)器返回Location字段為: http://localhost:8000/two/some/uri/
這個指令:
proxy_redirect http://localhost:8000/two/ http://frontend/one/;
將Location字段重寫為http://frontend/one/some/uri/。
在代替的字段中可以不寫服務(wù)器名:
proxy_redirect http://localhost:8000/two/ /;
這樣就使用服務(wù)器的基本名稱和端口,即使它來自非80端口。
如果使用“default”參數(shù),將根據(jù)location和proxy_pass參數(shù)的設(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/;
這個指令有時可以重復(fù):
proxy_redirect default; proxy_redirect http://localhost:8000//; proxy_redirect ; /;
參數(shù)off將在這個字段中禁止所有的proxy_redirect指令:
proxy_redirect off; proxy_redirect default; proxy_redirect http://localhost:8000/ /; proxy_redirect ; /;
利用這個指令可以為被代理服務(wù)器發(fā)出的相對重定向增加主機(jī)名:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
修改Nginx源碼實(shí)現(xiàn)worker進(jìn)程隔離實(shí)現(xiàn)詳解
這篇文章主要為大家介紹了修改Nginx源碼實(shí)現(xiàn)worker進(jìn)程隔離實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10Linux系統(tǒng)下nginx日志每天定時切割的腳本寫法
本篇文章給大家分享使用Linux系統(tǒng)自帶的命令logrotate對Nginx日志進(jìn)行切割的方法,對nginx日志切割腳本感興趣的朋友一起學(xué)習(xí)吧2016-11-11Nginx使用自簽ssl證書實(shí)現(xiàn)https連接的方法
本文主要介紹了Nginx使用自簽ssl證書實(shí)現(xiàn)https連接的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07Nginx訪問本地靜態(tài)資源詳細(xì)步驟(推薦)
Nginx?(engine?x)?是一個高性能的HTTP和反向代理web服務(wù)器,同時也提供了IMAP/POP3/SMTP服務(wù),這篇文章主要介紹了nginx配置訪問本地靜態(tài)資源,需要的朋友可以參考下2022-12-12ConfigMap掛載與Subpath在Nginx容器中的應(yīng)用小結(jié)
configmap可以通過ENV環(huán)境變量和文件兩種方式掛載到容器中,修改configmap后容器中對應(yīng)的ENV環(huán)境變量不會更新,將配置文件nginx.conf以configmap文件的方式掛載到容器中,本文介紹ConfigMap掛載與Subpath在Nginx容器中的應(yīng)用小結(jié),感興趣的朋友一起看看吧2024-03-03