nginx 反向代理设置中的proxy_redirect

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

免责声明:本网信息来自于互联网,目的在于传递更多信息,并不代表本网赞同其观点。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,并请自行核实相关内容。本站不承担此类作品侵权行为的直接责任及连带责任。如若本网有任何内容侵犯您的权益,请及时联系我们,本站将会在24小时内处理完毕。
相关文章
返回顶部