Add a request header on mod_proxy based on the value of another request header

apache-2.2mod-proxyrequestheader

I have a server running mod_proxy and I need to essentially rename a request header. A proxy closer to the origin is stripping off the X-Forwarded-For header, but we need that data to persist, so I want to add the value to a different header to allow the origin to pick it up.

From my reading of the doco there is no way to rename a header and I can't see how to pass in values into RequestHeader add in mod_headers.

Best Answer

RequestHeader can insert values of environment variables into header values, and SetEnvIf can set environment variables based on request header values, so the following configuration should achieve what you need:

SetEnvIf X-Forwarded-For (.*) saved_x_forwarded_for=$1
RequestHeader set X-Custom-Forwarded-For "%{saved_x_forwarded_for}e"

You should use set instead of add, so that even if a client sends a request containing your custom header, the client-specified value will be overwritten (although even this is not 100% reliable, because the X-Forwarded-For header could be spoofed too). Also test what happens if a request without X-Forwarded-For is received.

Related Topic