您的位置:首页 > 运维架构 > 反向代理

nginx 反向代理设置中的proxy_redirect

2017-10-22 19:33 288 查看
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 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息