Logging response headers in Apache reverse proxy without sending them to the client

apache-2.2apache-2.4http-headersreverse-proxy

We use serveral Apache servers as reverse proxy in front of numerous backend servers. The backend servers send a HTTP response header ("Cast") which contains an internal name of the backend server.

In the reverse proxy I would like to log the content of the backend server's response header and prevent the header from being sent to the client.

Logging the header is simple with inserting %{Cast}o in our custom LogFormat configuration.
Also, preventing the header from being sent to the client is easy, by using Header unset Cast

The only problem is that when unsetting the header it cannot be logged anymore.

Is there a way to store the backend's response header in a variable, unset the header and log the variable?

Notes

  • The Apache servers being used as reverse proxies are Apache 2.2 on RHEL 6 and 2.4 on RHEL7
  • Reverse proxy rules use either ProxyPassor RewriteRule ... [P]

Best Answer

In the past I have used a intermediate VirtualHost entry as a work-around for similar situations when certain directives didn't play well together:

Your current configuration may look like:

<VirtualHost *:80>
   ServerName www.example.com
   ProxyPass /app http://app.example.com/app
   ProxyPassReverse /app http://app.example.com/app
</VirtualHost>

Modify that to point to the intermediate virtual host and strip your header from the responses:

<VirtualHost *:80>
   ServerName www.example.com
   ProxyPass /app http://localhost:8000/app
   ProxyPassReverse /app http://localhost:8000/app
   Header unset Cast
</VirtualHost>

and a new virtual host where you can still log your Cast headers:

Listen 127.0.0.1:8000
<VirtualHost 127.0.0.1:8000>
   ServerName localhost
   ProxyPass /app http://app.example.com/app
   ProxyPassReverse /app http://app.example.com/appp
   LogFormat %{X-Forwarded-For}o %{Cast}o ...
</VirtualHost>