nginx 反向代理設置中的proxy_redirect

Nginx做反向代理,如果在header設置了Host參數,同時如果有協議和二級目錄有不一致的情況的時候,

當後端服務做302、301跳轉的時候,需要用proxy_redirect將後端設置在response header中的Location做轉換.


比如後端應用Java:
LOG.debug("sendRedirect host in header " + req.getHeader("Host")); 
response.sendRedirect("t2"); 


1,瀏覽器通過https + 域名請求後端 http應用
通過nginx的域名訪問:https://www.xxx.com.cn/test/trd
默認配置的情況下:
server {
listen      443;
ssl on;
server_name www.xxx.com.cn;
location /test/ {
proxy_pass http://10.65.192.xx:8080/;
}
}
後端Java執行的情況爲
LOG.debug("sendRedirect host in header " + req.getHeader("Host")); //10.65.192.xx:8080
response.sendRedirect("t2"); //http://10.65.192.xx:8080/t2


不需要設置proxy_redirect,nginx會將http://10.65.192.xx:8080/t2轉成https://www.xxx.com.cn/test/t2


2,如果在header設置了Host參數:proxy_set_header Host $host;
LOG.debug("sendRedirect host in header " + req.getHeader("Host")); //www.xxx.com.cn
response.sendRedirect("t2"); //http://www.xxx.com.cn/t2


nginx需要配置proxy_redirect
server {
listen      443;
ssl on;
server_name www.xxx.com.cn;
location /test/ {
proxy_pass http://10.65.192.xx:8080/;
proxy_redirect http://$host/ https://$host:$server_port/test/;
}

}

3.proxy_redirect 可以支持正則表達式,可以設置多個 proxy_redirect 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章