1

I have a few virtual hosts running on a server, a couple are through a CDN and have X-Forward-For added to the headesr, sometimes it just goes through a load balancer where RemoteIPHeader is added.

I have 3 custom log configs:

LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" proxy
LogFormat "%a %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" lb

and in my Virtual Host configs I use either lb, combined or proxy for the log format.

When I add RemoteIPHeader X-Forwarded-For to set the client IP instead of load balancer IP, the lb log works correctly but the proxy one uses the LB IP. When I remove the RemoteIPHeader X-Forwarded-For the proxy one has the client IP but the lb one has the load balancer IP. Which makes sense as I need to conditionally set X-Forwarded-For dependent on if it's already set by the CDN or not.

I've tried to do this like so:

<IfModule mod_headers>
    RequestHeader set X-Forward-For RemoteIPHeader expr="req('X-Forward-For')==''"
</IfModule>

from this question however it doesn't do anything. If do <IfModule mod_headers.c> it produces a syntax issue, if I put the above where I'm setting the log it says RequestHeader set X-Forward-For RemoteIPHeader expr="req('X-Forward-For')==''" has an unexpected $end.

How can set X-Forward-For correctly based on if it's set or not from the CDN?

Rudiger
  • 191
  • 1
  • 15

0 Answers0