如何使用动态后端服务器设置 Apache 2 反向代理?

发布于 2024-08-09 02:24:16 字数 1459 浏览 4 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

街角卖回忆 2024-08-16 02:24:16

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

#!/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 配置文件中的“Then”

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 行。因此,请对此进行测试,然后告诉我们它是否有效(或无效)。

The only thing that comes to mind is to use a RewriteMap script which will decide which machine to go to, via the P flag to RewriteRule, something like

#!/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
}

Then in the Apache configuration file

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

Caveats: This might have some flaws as it's untested, you would have to add a new ProxyPassReverse when you add a new server (and do a graceful), and, now that I think about it, depending on the specifics of the applications you might not even need the ProxyPassReverse lines. So, test this and please tell us if it worked (or not).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文