Apache – Set Header if It Doesn’t Already Exist

apache-2.2http-headersreverse-proxy

I have a proxy that is injecting some headers but I want to modify it so it only sets the headers if they are not already present:

<Location /api>    
    RequestHeader set MY_HEADER "value"

    ProxyPass http://127.0.0.1:8000/api
    ProxypassReverse http://127.0.0.1:8000/api
</Location>

Is this possible?

Best Answer

In Apache 2.4.7, x86_64, Ubuntu 14.04 LTS

I have found that this works

RequestHeader setIfEmpty X-Forwarded-For "127.0.0.1"

works all day long. However if one tries to use a dynamic value,

RequestHeader setIfEmpty X-Forwarded-For "%{REMOTE_ADDR}e"

does not work. I have found that you need the help of mod_rewrite to harvest the value. My configuration now looks like this, and it works.

RewriteRule . - [E=noxff:%{REMOTE_ADDR}] RequestHeader setIfEmpty X-Forwarded-For "%{noxff}e"

I know it is stoopid, but it works.

Related Topic