最近做的一个项目, 需要使用 tomcat 集群, 且项目需要使用https协议
但是,明明是https url请求, 但是java里的 request.getScheme() 的输出却是 http
proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme;
配置Tomcat server.xml 的 Engine 模块下配置一个 Valve:
<Valve className="org.apache.catalina.valves.RemoteIpValve"
remoteIpHeader="x-forwarded-for"
remoteIpProxiesHeader="x-forwarded-by"
protocolHeader="x-forwarded-proto"
protocolHeaderHttpsValue="https"/>
配置双方的 X-Forwarded-Proto 就是为了正确地识别实际用户发出的协议是 http 还是 https。
这样以上5项测试就都变为正确的结果了,就像用户在直接访问 Tomcat 一样。
参考资料:
http://www.th7.cn/Program/java/201608/929190.shtml
http://blog.csdn.net/pwq296306654/article/details/51760844
http://feitianbenyue.iteye.com/blog/2056357





