Search code examples
apacheproxyenvironment-variablesmod-proxy

Switch Apache proxypass conf according to an environnment variable


I need something that sounds trivial but I couldn't find out how to do this : switch from a proxypass conf to another according to an environnement variable (set from within conf or comming from the os). I have a few conditions that may be tricky here :

  • I can't use a variable from the command line (-DPARAMATER_A)
  • I can't change the path that are used.

I know I can do some conditionnal pass using Rewrite rules, but unless I read wrong, I can't use an environnement variable as a trigger.
I also know you can't use a < If> statement since one can't use a proxypass inside such a statement.
Eventually, I tried with a < IfDefine> statement (see below), but couldn't manage to have to work, unless I write a

Define PATH_A_ACTIVATED true

But the opposite does not work (it looks like it is still defined, whatever the value is):

Define PATH_A_ACTIVATED false

Thus, so far, I am stuck with no way to perform this.

Here's my latest (non working) attempt, trying to query a value from environnement variables and combine it with Define :

SetEnv PARAMATER_A false

<If "! -T reqenv('PARAMATER_A')">
  Define PATH_A_ACTIVATED true
</If>
<Else>
  UnDefine PATH_A_ACTIVATED
</Else>


<IfDefine PATH_A_ACTIVATED>
    ProxyPass "/someapp" "http://dv00001.someplace.fr/someapp"
    ProxyPassReverse "/someapp" "http://dv00001.someplace.fr/someapp"
</IfDefine>
<IfDefine !PATH_A_ACTIVATED>
    ProxyPass "/someapp/domain1" "http://dv00001.someotherplace.fr/someapp"
    ProxyPassReverse "/someapp/domain1" "http://dv00001.someotherplace.fr/someapp"
    ProxyPass "/someapp/domain2" "http://dv00001.someplace.fr/someapp"
    ProxyPassReverse "/someapp/domain2" "http://dv00001.someplace.fr/someapp"
</IfDefine>

Any idea?


Solution

  • Eventually, it ended up that mode_rewrite can definitely handle environnement variables. It has to be used this -tricky- way:

    RewriteRule .* - [E=TEST_ACTIVATED:false]
    
    # ====== TEST  ==================
    RewriteCond "%{ENV:TEST_ACTIVATED}" "true"
    RewriteCond "%{REQUEST_URI}" "/some/path"
    RewriteRule "^/some/path/(.*)" http://some.vm/some/path/$1 [P]
    # ====== HORS TEST ==============
    RewriteCond "%{ENV:TEST_ACTIVATED}" "false"
    RewriteCond "%{REQUEST_URI}" "/some/path"
    RewriteRule "^/some/path/(.*)" http://another.vm/some/path/$1 [P]
    

    Now, you juste have to switch TEST_ACTIVATED value