tomcat通过nginx配置代理后IP地址获取处理

在配置代理的location下进入如下几句配置加入:

proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr; #获取真实ip
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;

我的配置如下图:


然后需要在tomcat的服务器上对conf下的server.xml进行配置;

找到<Host>节点,在Host里面中的默认的上面加入如下配置:

<Valve className="org.apache.catalina.valves.RemoteIpValve" remoteIpHeader="X-Real-IP" />

<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" pattern="%h %l %u %t "%r" %s %b" prefix="localhost_access_log" suffix=".txt"/>

然后重启tomcat,最后再通过nginx访问,IP已能正常得到。

推荐 收藏
评论(0) 阅读(259)
刷新评论 返回顶部
Lottie最新博客