13

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?

Simon
  • 636
  • 1
  • 5
  • 13
  • 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

1 Answers1

14

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.

KLaalo
  • 3
  • 2
Simon
  • 636
  • 1
  • 5
  • 13