Nginx配置Https访问,tomcat无法正确获取schema的问题

本文介绍如何使用Nginx配置HTTPS访问并作为Tomcat的反向代理,解决redirect后的schema变为HTTP的问题及request.getSchema()始终返回HTTP的问题。通过配置Nginx的proxy_set_header及Tomcat的RemoteIpValve组件实现正确的协议转发。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Nginx配置Https访问,反向代理tomcat,发现两个问题:

(1)redirect之后的schema全变成了http。

(2)request.getSchema()全部返回http。

对于(1)解决办法:参考:http://blog.youkuaiyun.com/mr_smile2014/article/details/51701878

server {  
    listen 80 default_server;  
  
    location / {  
        proxy_pass http://127.0.0.1:8080;  
        proxy_redirect http:// $scheme://;  
    }  
}  

对于(2)的解决办法:参考:http://feitianbenyue.iteye.com/blog/2056357

nginx上配置:

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上配置:

<Engine >
	<Valve className="org.apache.catalina.valves.RemoteIpValve"  
	remoteIpHeader="X-Forwarded-For"  
	protocolHeader="X-Forwarded-Proto"  
	protocolHeaderHttpsValue="https"/> 
</Engine >

重启,over!


关于 RemoteIpValve,有兴趣的同学可以阅读下 :http://tomcat.apache.org/tomcat-6.0-doc/api/org/apache/catalina/valves/RemoteIpValve.html
Tomcat port of mod_remoteip, this valve replaces the apparent client remote IP address and hostname for the request with the IP address list presented by a proxy or a load balancer via a request headers (e.g. "X-Forwarded-For").   
Another feature of this valve is to replace the apparent scheme (http/https) and server port with the scheme presented by a proxy or a load balancer via a request header (e.g. "X-Forwarded-Proto").  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值