Nginx反向代理tomcat时response.sendRedirect问题


原文地址:

http://blog.sina.com.cn/s/blog_40ce02d70102wbzo.html

Nginx https反向代理到tomcat http
用户登录跳转时 response.sendRedirect 总是转到http的地址而不是https
 
实际测试影响到以下函数:
request.getScheme()  获取不到实际的https  
request.isSecure() 总是false  
request.getRemoteAddr()   
request.getRequestURL()   
response.sendRedirect(url) 总是重定向到 http 上
 
解决办法: 
1.修改nginx配置:
proxy_pass      http://127.0.0.1:8080;
    proxy_set_header Host    $host;//非80、443端口使用$host:$server_port
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header REMOTE-HOST $remote_addr;
    proxy_redirect off;
    proxy_set_header  X-Forwarded-Proto  $scheme;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 
2.修改tomcat(conf/server.xml),在Engine节点添加
            remoteIpHeader="X-Forwarded-For"
            protocolHeader="X-Forwarded-Proto"
            protocolHeaderHttpsValue="https"/>
 
 

解决nginx https代理tomcat redirect问题

原文地址:

https://blog.csdn.net/juncke/article/details/52539175

问题描述
http服务器:nginx,10.10.10.95,版本:1.10.1,请求使用协议为https,端口为18080。

服务服务器:tomcat,10.10.10.92,使用协议为http,端口为8080。

问题:当在业务服务器使用sendRedirect时,tomcat响应302给nginx,nginx再响应给浏览器,默认情况下,nginx响应给浏览器的location会将schema换为http,端口为:80。

这就导致浏览器redirect后,访问不到正确资源。


过程举例:

浏览器输入:https://10.10.10.95:18080/redirect_test.do
redirect_test.do中执行sendRedirect("/welcome.do")
浏览器接收到的redirect响应(302)中location为http://10.10.10.95/welcome.do
浏览器跳到http://10.10.10.95/welcome.do,发现访问不了

解决方案
修改nginx配置,关键配置如下:

 

server{
  listen 18080;
  server_name ecsc;
  ssl on;
  ssl_certificate /root/ssl/test.crt;
  ssl_certificate_key /root/ssl/test_nopass.key;
  error_page 497 https://$host:8080;

  access_log /var/log/nginx/access.log main;

 proxy_redirect http:// $scheme://; port_in_redirect on;

  location ~/druid{
  return 404;
}

location /{
  proxy_connect_timeout 300;
  proxy_send_timeout 300;
  proxy_read_timeout 300;
  expires 10d;
  proxy_pass http://console.eayun.com ;
  proxy_set_header Host $host:$server_port;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-For 
  $proxy_add_x_forwarded_for;
  proxy_set_header X-NginX-Proxy true;
}

 


说明:

使用proxy_redirect将location中的协议转换为请求nginx的协议。

使用port_in_redirect on指示nginx使用请求的端口,而不是使用默认端口。

proxy_set_header Host $host:$server_port;很关键,我也不明白为啥,不设置,端口为变为80。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM