Apache custom-log format with regex expression

apache-2.2loggingmod-jk

We have apache webserver as "entry" for incoming traffic it then delegates to tomcats with mod_jk.

We want to log the HTTP digest username, sample header:

Authorization: Digest username="Mufasa",
                     realm="testrealm@host.com",
                     nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093",
                     ...

Unfortunately using the '%u' doesn't work as described. My guess is as we authenticate on tomcat side (apache delegates calls to tomcat with mod_jk) the own logging facility of apache doesn't know how to access the digest-username (because own auth module is bypassed).

To workaround this: Is there a custom log-format expression with regex matching for, e.g. telling to extract username="(.*?)" from 'Authorization' header and push to logs?

Best Answer

SetEnvIf Authorization username="([^"]+)" digest_username=$1
LogFormat "%h %l %{digest_username}e %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\"" combined_with_user
CustomLog /path/to/log combined_with_user

Haven't tested, so not sure if Apache is going to choke on the quotes in the SetEnvIf (they may need escaped) - but this should be pretty close.

Related Topic