How to log Client IP and X-Forwarded-For IP in tomcat access log

前端 未结 3 2057
北恋
北恋 2021-01-02 23:53

How to log Client IP and X-Forwarded-For IP in tomcat access log.

I am using %{X-Forwarded-For}i and it logs the actual client address if I access through load bala

3条回答
  •  梦谈多话
    2021-01-03 00:16

    
    
    
    

    You can add these two Valve definition into context.xml. So you can see remote ip in both case. Through load balancer or direct.

    org.apache.catalina.valves.RemoteIpValve detects "x-forwarded-for" header. If there is "x-forwarded-for" in header puts its value into "org.apache.tomcat.remoteAddr" request attribute. If there is no "x-forwarded-for" in header puts client ip addr into "org.apache.tomcat.remoteAddr" request attribute.

    AccessLogValve config just logs "org.apache.tomcat.remoteAddr" request attribute which holds correct remote ip.

提交回复
热议问题