详解NGINX访问https跳转到http的解决方法(nginx https代理tomcat redirect问题)

情况说明:
nginx配置https,tomcat正常http接受nginx转发。
nginx 代理https后,(java代码redirect地址)应用redirect https变成http

情况类似:浏览器打开https://www.zzvips.com/aaa.html,然后跳转到http://www.zzvips.com/aaa.html

网站架构:用户--https--->nginx代理---http---->tomcat/nginx+php

nginx代理发给后端的请求是http协议,后端程序跳转获取到的协议是http,返回一个redirect(http header中带Location:http://www.ttlsa.com/aaa.html),浏览器收到location,跳转到了location指定的地方。

 

解决方法
解决方法1:
在nginx代理中增加一个header,标志用户请求是http还是https,后端获取header决定跳转到http/https页面。这个方法需要修改nginx配置和程序,不推荐,但是可以解决问题。

解决方法2(推荐):
nginx代理中配置proxy_redirect(使用proxy_redirect重定向url)

proxy_redirect http:// $scheme://;


以上指令会将后端响应header location内容中的http://替换成用户端协议https://。

NGINX访问https跳转到http的解决了~

 

说明:

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