Loading... ### 问题描述: 使用阿里云SLB启用https,发现后端的Tomcat并不能获取到正确的协议与客户端访问的真实IP,Tomcat的配置方式参照nginx下面的配置方法,详见[《HTTPS环境下Nginx反向代理Tomcat的正确配置方法》][1]这篇文章。 ### 原因分析 由于阿里云SLB使用的IP地址都是100.x.x.xk开头的,这段IP均为保留IP,并不在Tomcat的可信代理IP列表中,Tomcat官方的[这篇][2]文档中有如下描述 >Regular expression (using java.util.regex) that a proxy's IP address must match to be considered an internal proxy. Internal proxies that appear in the remoteIpHeader will be trusted and will not appear in the proxiesHeader value. If not specified the default value of `10\.\d{1,3}\.\d{1,3}\.\d{1,3}|192\.168\.\d{1,3}\.\d{1,3}|169\.254\.\d{1,3}\.\d{1,3}|127\.\d{1,3}\.\d{1,3}\.\d{1,3}|172\.1[6-9]{1}\.\d{1,3}\.\d{1,3}|172\.2[0-9]{1}\.\d{1,3}\.\d{1,3}|172\.3[0-1]{1}\.\d{1,3}\.\d{1,3}|0:0:0:0:0:0:0:1` will be used. 也就是说,Tomcat默认只信任如下代理的IP,其他IP做代理,需要添加到`internalProxies`才行。 ``` 10.0.0.0/8 127.0.0.0/8 172.16.0.0/12 169.254.0.0/16 192.168.0.0/16 0:0:0:0:0:0:0:1(ipv6) ``` ### 解决方案 - 在阿里云SLB上勾选如下选项,如图: ![89.jpeg][3] - 在ECS中的tomcat配置文件中`<Engine>`元素下新增如下配置 ``` <Valve className="org.apache.catalina.valves.RemoteIpValve" remoteIpHeader="X-Forwarded-For" protocolHeader="X-Forwarded-Proto" protocolHeaderHttpsValue="https" internalProxies="100\.\d{1,3}\.\d{1,3}\.\d{1,3}" /> ``` 完整配置文件如下 ``` <?xml version="1.0" encoding="UTF-8"?> <Server port="8005" shutdown="SHUTDOWN"> <Listener className="org.apache.catalina.startup.VersionLoggerListener" /> <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" /> <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" /> <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" /> <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" /> <GlobalNamingResources> <Resource name="UserDatabase" auth="Container" type="org.apache.catalina.UserDatabase" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" pathname="conf/tomcat-users.xml" /> </GlobalNamingResources> <Service name="Catalina"> <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /> <Engine name="Catalina" defaultHost="localhost"> <Valve className="org.apache.catalina.valves.RemoteIpValve" remoteIpHeader="X-Forwarded-For" protocolHeader="X-Forwarded-Proto" protocolHeaderHttpsValue="https" internalProxies="100\.\d{1,3}\.\d{1,3}\.\d{1,3}" /> <Realm className="org.apache.catalina.realm.LockOutRealm"> <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/> </Realm> <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true"> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log" suffix=".txt" pattern="%h %l %u %t "%r" %s %b" /> </Host> </Engine> </Service> </Server> ``` [1]: https://blog.90.vc/archives/44.html [2]: https://tomcat.apache.org/tomcat-8.0-doc/config/valve.html#Remote_Host_Filter [3]: https://feng-bj.oss-cn-beijing.aliyuncs.com/usr/uploads/2019/06/733768865.jpeg 最后修改:2019 年 06 月 21 日 © 允许规范转载 打赏 赞赏作者 支付宝微信 赞 如果觉得我的文章对你有用,请随意赞赏
此处评论已关闭