I have a simple apache vhost:

<VirtualHost *:80>
  ServerName hello.local

  ProxyPass / http://localhost:8810/
  ProxyPassReverse / http://localhost:8810/

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?

  • You want to execute this external program during each request? – sciurus Apr 30 '14 at 12:45
  • Yes. Or it could also be a "subrequest": a value returned by a cgi script or something similar. I'm aware of the performance implications. – Simon Apr 30 '14 at 13:03

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:


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

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

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/

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.