如何在tomcat访问日志中记录客户端IP和X-Forwarded-For IP [英] How to log Client IP and X-Forwarded-For IP in tomcat access log

查看:842
本文介绍了如何在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?

推荐答案

来自

如果您运行的tomcat版本大于6.0.21或tomcat 7,则可以利用新的远程IP Valve.对于访问日志记录,此阀门的好处是,如果在X-Forwarded-For标头中传递了IP地址,它将自动将客户端IP与通过X-Forwarded-For标头传递的IP地址交换.加载它非常容易.只需在AccessLogValve声明之前将org.apache.catalina.valves.RemoteIpValve添加到server.xml中即可.例如:

If you are running a version of tomcat greater than version 6.0.21 or tomcat 7, you can take advantage of the new Remote IP Valve. For access logging, the nice thing about this valve is that it will swap the client IP with an IP address passed with the X-Forwarded-For header—automatically—if an IP address is passed in the X-Forwarded-For header. Loading it is pretty easy. Just add the org.apache.catalina.valves.RemoteIpValve to your server.xml before your AccessLogValve declaration. For example:

 <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>

如果您使用的tomcat 6版本低于6.0.21,而您想存储X-Forwarded-For IP地址,则可以修改AccessLogValve的pattern属性.您需要删除"common"或"combined"模式并将其替换为以下模式之一:

If you are using a version of tomcat 6 older than 6.0.21 and you want to store the X-Forwarded-For IP address instead, then you could modify the pattern property of your AccessLogValve. You'll need to remove the "common" or "combined" pattern and replace it with one of the following patterns:

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

RemoteIP Valve确实处理的主要问题是,您只会在日志中获得X-Forwarded-For地址.如果您直接命中应用服务器,而绕过在请求中插入X-Forwarded-For标头的设备,则不会记录IP地址.您仍然会记录一个请求,只是不知道它来自何处.

The main problem here, that RemoteIP Valve does take care of, is that you'll only get the X-Forwarded-For address in the logs. If you hit the app server directly, bypassing the device that is inserting the X-Forwarded-For header in the request, you won't get an IP address logged. You will still log a request—you just will not know where it came from.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆