nginx 反向代理设置中的proxy_redirect

来源:互联网 发布:上传淘宝的照片不清晰 编辑:程序博客网 时间:2024/06/09 21:06
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 

原创粉丝点击