When using mod_proxy, how can I make Tomcat log the client's IP address?

In your Apache HTTPD config, where you're configuring proxying the request, make sure you add:

ProxyVia On

This will make HTTPD set the "x-forwarded-for" HTTP request header's value to the IP address of the client. This value is passed to Tomcat when HTTPD proxies the request.

Then, where you're configuring the AccessLogValve (usually in your conf/server.xml file), change the default logging pattern to log the "x-forwarded-for" header value, like this:

<Host ...>
    <Valve className="org.apache.catalina.valves.AccessLogValve"
           directory="logs"
           prefix="localhost_access_log."
           suffix=".txt"
           pattern="%{x-forwarded-for}i %l %u %t %r %s %b"
           resolveHosts="false"/>
</Host>

With this configuration, AccessLogValve will log the x-forwarded-for header value that is set by Apache HTTPD's mod_proxy, and the remainder of the logging pattern will be the same as usual.

Comments on this post:
 #
great article
 

Post new comment

The content of this field is kept private and will not be shown publicly.
Tomcat Download

Download Tcat Server - Tomcat Simplified

Diagnose, manage, configure, develop and deploy your Apache Tomcat applications with ease, and break free from bloated legacy JEE app servers. Built on 100% Tomcat, with no changes to the core code, Tcat Server is free for developers, and there is no commitment required. Try it now, risk-free! 
Tomcat - Tcat Download  Apache Tomcat - Tcat Howto
Apache Tomcat - Tcat Screenshot

click thumbnail to enlarge
Free Tomcat Support
Free Tomcat Training - Apache Tomcat

Sign up for our Newsletter!