0

我想将 Apache 2 设置为反向代理,使用基于名称的虚拟主机来决定如何将请求路由到后端服务器。很简单。

问题是这些后端服务器可以以动态方式添加和删除。我的第一个想法是以编程方式重写 Apache 配置文件,并apachectl graceful在每次后端服务器启动或关闭时调用。这似乎不是正确的解决方案。有什么更好的方法来实现这一点?

我需要能够优雅地将名称处理转移到不同的后端服务器。例如,Backend-Server-A 可能正在处理 example.com 的请求。监控进程可能会确定 Backend-Server-A 已过时(内存使用量过多,有新版本的服务器代码来处理 example.com 等)。监控进程启动 Backend-Server-B,它将很快处理 example.com 的请求。Apache 应将 example.com 的任何新请求定向到 Backend-Server-B,但允许 Backend-Server-A 当前正在处理的任何未决请求在 Backend-Server-A 关闭之前完成。

更新发布到服务器故障

4

1 回答 1

1

唯一想到的是使用 RewriteMap 脚本,该脚本将通过 P 标志到 RewriteRule 来决定要转到哪台机器,例如

#!/usr/bin/perl
#This is /usr/bin/requestdistributor.pl
$| = 1; # Turn off buffering
while (<STDIN>) {
        print distributeRequest($_);
}
sub distributeRequest {
    my $request = shift;
    #do whatever you have to do to find the proper machine for the request,
    #return the complete URL with a trailing newline
}

然后在Apache配置文件中

RewriteMap distributeRequests prg:/usr/bin/requestdistributor.pl 
RewriteRule (.*) ${distributeRequests:$1} [P]

#Setup the reverse proxying for all machines, use the proper URLs
ProxyPassReverse / http://machine1
ProxyPassReverse / http://machine2
#and so on...
ProxyPassReverse / http://machineN

警告:这可能有一些缺陷,因为它未经测试,当你添加一个新服务器时,你必须添加一个新的 ProxyPassReverse (并且做一个优雅的),现在我考虑一下,这取决于你可能的应用程序的细节甚至不需要 ProxyPassReverse 行。所以,测试一下,请告诉我们它是否有效(或无效)。

于 2009-10-29T06:45:59.997 回答