Apache proxypass使用带插值的变量URL

时间:2011-01-03 09:09:29

标签: apache configuration localhost proxypass interpolation

我正在尝试设置一个用于测试目的的apache服务器。目标是模拟负载均衡器。我们试图模拟的部分就是接受客户端安全的httprequest并使用proxypass将其传递给(通过不安全的http)到正确的服务器。这一切都是100%的。

问题是:我想在几个开发环境中使用这个“负载均衡器”。为了能够测试应用程序的服务器端,我希望proxypass使用变量URL传递给客户端IP。这将需要在proxypass规则中使用变量URL。我找到了变量REMOTE_ADDR,但我似乎无法在proxypass规则中使用它。我首先尝试了apache网站告诉我使用的语法,但是没有用(http://REMOTE_ADDR/foobar/的DNS查找失败)。我知道我必须用Interpolate做一些事情,但我似乎无法弄清楚到底是什么。

这条线做它应该做的,所以服务器工作得很好:
ProxyPass / http://localhost:81/

这些是我尝试过的规则(使用变量)失败了。

ProxyPass / http:// $ {REMOTE_ADDR}:81 /
错误:[client 127.0.0.1] proxy:DNS查找失败:$ {remote_addr}由/

返回

ProxyPass / https://% {REMOTE_ADDR} s /
错误:[client 127.0.0.1] proxy:DNS查找失败:%{remote_addr}由/返回

ProxyPassInterpolateEnv On ...
ProxyPass / http:// $ {REMOTE_ADDR}:81 / interpolate
错误:[client 127.0.0.1]错误解析URL //:81:无效的主机/端口

有任何想法吗?

2 个答案:

答案 0 :(得分:13)

对于插值情况,它应该是这样的:

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

在互联网上搜索了一整夜后,我找不到任何明显的案例,比如这个或任何答案。不过我终于明白了。

答案 1 :(得分:5)

您可以使用[P]代码替换mod_rewrite规则的Proxypass指令,P代表代理。

Mod_rewrite可以让你做更具体的事情。