Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I'm configuring nginx as a reverse proxy in front of Tomcat 7. I added the following lines to nginx configuration:

set_real_ip_from 127.0.0.1;
...
location / {
    proxy_pass http://tomcat;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

But still, I see in Tomcat logs the following picture:

127.0.0.1 - - [10/Jun/2013:11:25:48 +0600] "GET /app/welcome;jsessionid=6C1B02376C5F748C509B28FC7CE416C9 HTTP/1.0" 200 10571
127.0.0.1 - - [10/Jun/2013:11:25:48 +0600] "GET /app/welcome;jsessionid=0BBE0174C1F0E94FDF49610144E809D3 HTTP/1.0" 200 10571
127.0.0.1 - - [10/Jun/2013:11:25:48 +0600] "GET /app/welcome;jsessionid=AD48005AD453F3A0BE46F1AC978F145D HTTP/1.0" 200 10571

Is there any way to force Tomcat to use X-Real-IP header (and write it to log files) without modifying web app?

share|improve this question

2 Answers 2

I was searching for the same thing and found information leading me to the following solution from researching around the net.

In your tomcat server.xml you need to edit the logging valve pattern to get the values from the incoming header.

In your

and change the pattern to:

pattern="Remote User[ %{X-Forwarded-For}i %l %u %t ] Request[ "%r" ] Status Code[ %s ] Bytes[ %b ] Referer[ "%{Referer}i" ] Agent[ "%{User-agent}i" ] "

My full access log value looks like the following:

      <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="access_log." suffix=".txt"
               pattern="Remote User[ %{X-Forwarded-For}i %l %u %t ] Request[ &quot;%r&quot; ] 
               Status Code[ %s ] Bytes[ %b ] Referer[ &quot;%{Referer}i&quot; ] 
               Agent[ &quot;%{User-agent}i&quot; ] " />

This is accompanied by the Nginx config of:

location / {
    proxy_pass http://localhost:8080;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_intercept_errors on;
}

Additional information of log valve patterns in Tomcat can be found at: Apache 7: The Valve Component

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.