Homepage de Marianne et Emmanuel
Journee de la non-violence éducative le 30 avril

Apache: get the originating host ip

Apply to: apache and apache2

Aim: get the originating IP of the client host, by default logs contain the IP of the proxy if the client use a proxy. This is a problem:

Warnings:

Solution:

  1. Define two logformat, one with the host IP (%h), one with the value of X-Forwarded-For:
    LogFormat "%h %l %u %t \"%r\" %s %b \"%{Referer}i\" \"%{User-agent}i\"" combined
    LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %s %b \"%{Referer}i\" \"%{User-agent}i\"" combined_forwarded
    
  2. define an environment variable if a proxy is used:
    # Log the originating ip if use a proxy
    SetEnvIfNoCase X-Forwarded-For "." from_proxy=1
    
  3. Use different log depending on "from_proxy"
    CustomLog /var/log/apache2/maretmanu.org-access.log combined env=!from_proxy
    CustomLog /var/log/apache2/maretmanu.org-access.log combined_forwarded env=from_proxy
    
  4. reload your apache