1

I'm trying to set some specific headers based on the client IP address via an Apache reverse proxy. I try to use SetEnvIfNoCase/SetEnvIf but I somehow fail to write a correct regular expression :(

Here what I tried to set in httpd.conf (assume that the reverse proxy directives are ok - they are):

SetEnvIfNoCase Remote_Addr "192\.168*" user_location_internal
RequestHeader set x-acme-user-location internal env=user_location_internal

Lets say that the request comes from 192.168.1.100. Then the regular expression is supposed to match - I tried it in several online validators that claim to be perl compliant and 192.168.1.100 matches to "192.168*". The documentation of SetEnvIf also claims to be perl reg ex compliant.

However it doesn't work. The only syntax that worked was following:

SetEnvIfNoCase Remote_Addr 192* user_location_internal
RequestHeader set x-acme-user-location internal env=user_location_internal

Then the header is set so the issue is somehow related to the regular expression syntax. So my best guess is that I'm not correctly escaping the dot. However according to: http://perldoc.perl.org/perlre.html#Regular-Expressions backslash is the correct symbol to escape metacharacters.

Any guesses what is wrong?

Reaces
  • 5,597
  • 4
  • 38
  • 46
vap78
  • 113
  • 3

1 Answers1

1

The regex to match the IP could be:

SetEnvIfNoCase Remote_Addr ^192\.168.* user_location_internal

Note the .* part. Without the dot, it works because 192* means 192 zero or more times, which matches the Remote_Addr:

$ pcretest
PCRE version 8.33 2013-05-28

  re> /^192*/
data> 192.foo.bar.baz
 0: 192
  re> /^192\.168.*/
data> 192.168.12.12
 0: 192.168.12.12
data> 192.167.23.45
No match
dawud
  • 15,096
  • 3
  • 42
  • 61
  • Thanks a lot - that one worked! Also part of the problem was that I was accessing the reverse proxy via localhost which results in Remote_Addr being set to "127.0.0.1". When I used the real host name then the actual IP was available and the regular expression worked! – vap78 May 15 '14 at 10:55