Add a custom header to ProxyPass requests

apache-2.2apache-2.4http-headers

I have a simple apache vhost:

<VirtualHost *:80>
  ServerName hello.local

  ProxyPass / http://localhost:8810/
  ProxyPassReverse / http://localhost:8810/
</VirtualHost>

All request to hello.local are proxyed to http://localhost:8810/.
What I'd like to do is add a header to the http request going to http://localhost:8810/ with a value returned by an external command. Something like

Header set MyHeader ${/usr/bin/an_external_program}

Any way to accomplish this?

Best Answer

Ok I got it.

First of all, the script that is executed and that is used to get the value to insert in the header. I created this as /opt/apache/debug.sh:

#!/bin/bash

#this script just loops forever and outputs a random string
#every time it receives something on stdin

while read
do
        cat /dev/urandom|head -c12|base64
done

Apache config:

<VirtualHost *:80>
        ServerName light.nik

        RewriteEngine On

        RewriteMap doheader prg:/opt/apache/debug.sh
        RewriteRule (.*) - [E=customheader:${doheader:},P]

        RequestHeader set customheader %{customheader}e

        ProxyPass / http://localhost:8080/
        ProxyPassReverse / http://localhost:8080/
</VirtualHost>

The backend service running on http://localhost:8080/ receives the customheader with the value from the script.

The Apache documentation about using external program is here.