user2933916
user2933916

Reputation: 41

How to use environment variable from mod_rewrite for interpolate proxypass in httpd.conf

I'm using apache 2.4 and trying to use environment variable inside conf file for proxy pass. There's a thread [Apache proxypass using a variable URL with interpolate ]talking about this:

RewriteEngine on
RewriteMap lowercase int:tolower

#This sets the variable to env:
RewriteRule ^ - [E=SERVER_NAME:${lowercase:%{SERVER_NAME}}]

#Now interpolate makes variables available for proxypass & proxypassreverse:
ProxyPassInterpolateEnv On
ProxyPass / ajp://${SERVER_NAME}:8009/ interpolate
ProxyPassReverse / ajp://${SERVER_NAME}:8009/ interpolate

But when I tried this myself, I get a "AH00111: Config variable ${SERVER_NAME} is not defined" error. Which means Apache2.4 treats the ${SERVER_NAME} as config variable, rather than environment variable.

I also tried using the mod_rewrite sytax for the variable, like this,

ProxyPass / ajp://%{ENV:SERVER_NAME}:8009/ interpolate
ProxyPassReverse / ajp://%{ENV:SERVER_NAME}:8009/ interpolate

But %{ENV:SERVER_NAME} was treated as plaintext string, and created an error since it's not a valid URL pass.

Config variables are defined with "Define" block at server launch. What I want is to have the SERVER_NAME changing at runtime using mod_rewrite. I can't use the mod_rewrite [P] argument, since I need to have the ProxyPassReverse block working with variable as well. mod_rewrite can't deal with rewriting the response, and therefore can't mimic the function of ProxyPassReverse.

Any ideas on how to use environment variable in interpolate proxypass conf directives?

Upvotes: 4

Views: 7549

Answers (1)

pbitty
pbitty

Reputation: 151

It turns out Apache will still load and work fine, even with the warnings, using this syntax:

ProxyPassReverse / ajp://${SERVER_NAME}:8009/ interpolate

This issue was identified in this mailing list thread. The discussion is about another issue, but they mention this problem and discuss the idea of adding a second interpolation syntax to ProxyPass, etc. However, I have not found a bug report, and it seems as of Apache 2.4.10, the mod_proxy code still supports only the ${varname} syntax, which conflicts with the overall httpd.conf interpolation.

I haven't found a way to disable those specific warnings, but if you are willing to live with the warnings, Apache should still work for you.

One caveat is that if there is a system environment variable present with the same name when Apache is loading, it will overwrite the value you are trying to interpolate at runtime.

Upvotes: 4

Related Questions