Search code examples
apacheipmasking

Apply a mask to IP with Logformat


I need to apply a mask to IP in the log of Apache.

For example, I have this log : 192.168.234.111 - - [18/Oct/2013:16:29:40 +0200] "GET ........"

And I want to save that : 192.168.234.xxx - - [18/Oct/2013:16:29:40 +0200] "GET ........"

To do the first log, I'm using log format like this.

LogFormat "%h %l %u %t" combined-syslog2
CustomLog /var/log/toto combined-syslog2

To have the second log, I can pipe a perl/shell post script like that :

CustomLog |/usr/local/shl/apache_syslog2

But I'm not happy with this solution. Is it possible to do that with Apache ?

Thanks.

Eric


Solution

  • Here's the way to do it in Apache:

    • Use a RewriteCondition which matches all IP addresses
    • Store the partial IP address in a backreference
    • Use a RewriteRule to store the partial IP plus xxx in an environment variable (e.g. VARNAME)
    • Reference the environment variable in the LogFormat declaration via %{VARNAME}e

    References