Saturday, April 9, 2022

[SOLVED] How can I add RequestHeader from the x-forwared-for?

Issue

I added configuration like below to create custom RequestHeader on httpd.conf file(apache 2.4.51) https://httpd.apache.org/docs/2.4/expr.html

RequestHeader set x-client-ip "expr=%{HTTP:X-Forwarded-For} =~ m#\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$#"

If x-forwarded-for is X-Forwarded-For: 10.20.30.40, 50.60.70.80 then I expect that x-client-ip: 50.60.70.80.

But result is x-client-ip:~ m#d{1.

I've already tested it in sublime text, but it's working well.

enter image description here


Solution

The construct your using does not return a string, it's just a match. When you use it in string context, it's not interpreted right.

Try this simpler recipe w/o expressions

SetEnvIf X-Forwarded-For "(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$" xff=$1
RequestHeader set x-client-ip %{xff}e env=xff

If you ultimately need something more sophisticated with expressions, use setenvif still to transform the input into an envvar then use envvars in your expression.



Answered By - covener
Answer Checked By - Timothy Miller (WPSolving Admin)