我想使用基于名称的虚拟主机将 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 被我的监控进程关闭之前完成。
(注意:我最初将其发布在 Stack Overflow 上。)
唯一想到的是使用一个 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
}
Run Code Online (Sandbox Code Playgroud)
然后在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
Run Code Online (Sandbox Code Playgroud)
警告:这可能有一些缺陷,因为它未经测试,当你添加一个新的服务器时你必须添加一个新的 ProxyPassReverse(并且做一个优雅的),现在我想到它,这取决于你可能的应用程序的细节甚至不需要 ProxyPassReverse 行。所以,测试一下,请告诉我们它是否有效(或无效)。
| 归档时间: |
|
| 查看次数: |
3597 次 |
| 最近记录: |