本文介绍了如何在tomcat访问日志中记录客户端IP和X-Forwarded-For IP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在tomcat访问日志中记录客户端IP和X-Forwarded-For IP.

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

我正在使用%{X-Forwarded-For} i,并且如果我通过负载均衡器进行访问,它会记录实际的客户端地址.但是,如果我直接访问tomcat实例,则不会记录实际的客户端地址.在这两种情况下,是否都可以显示实际的客户端IP地址?

I am using %{X-Forwarded-For}i and it logs the actual client address if I access through load balancer. But does not log the actual client address if I directly access the tomcat instance. Is there a way to display the actual client IP address in both the cases?

推荐答案

来自 http://www.techstacks.com/howto/configure-access-logging-in-tomcat.html :

 <Host name="localhost"  appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">

  <!-- Remote IP Valve -->
    <Valve className="org.apache.catalina.valves.RemoteIpValve" />

  <!-- Access log processes all example.
    Documentation at: /docs/config/valve.html -->

  <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
    prefix="localhost_access_log." suffix=".txt"
    pattern="combined" resolveHosts="false"/>
  -->
</Host>
Common Log Format: %{X-Forwarded-For}i %l %u %t "%r" %s %b
Combined Log Format: %{X-Forwarded-For}i %l %u %t %r %s %b %{User-Agent}i %{Referer}i

这篇关于如何在tomcat访问日志中记录客户端IP和X-Forwarded-For IP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 09:43