用nginx 代理https后,应用redirect https变成http,导致网站无法访问

浏览器到nginx是https,nginx到应用服务器变成http,
应用服务器,再做302 redirect的时候,返回的redirect 地址就变成http的地址;

原因
原因是spring mvc的servlet的secheme取值,request.getScheme()
是取请求里的一个scheme值,所有这个值在nginx代理时要设置成https
其中: request.getScheme() return http but not https.

解决
配置:proxy_redirect http:// $scheme://;

server {
    listen 80 default_server;
    location / {
        proxy_pass http://127.0.0.1:8080;
        proxy_redirect http:// $scheme://; #该指令会将后端响应header location内容中的http://替换成用户端协议https://。
    }
}

其他参考
在这里插入图片描述
可能是因为springmvc底层使用的是 httpservletresponse.sendredirect,并没有使用response.isSecure进,所以最后发送的请求重定向的协议是http://

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐