Apache 负载均衡器:根据 URL 直接指向特定应用程序
Apache load-balancer: direct to specific application based on URL
我在 Tomcat 的 webapps 文件夹中部署了多个应用程序(app1.0、app1.1、app1.2 等)。当我点击 www.example.com:8080/app1.0 时,会出现相应的应用程序。
但是在负载均衡服务器上怎么办呢?例如,我有一个网站,我可以在该网站上单击一个按钮(app1.0、app1.1、app1.2 等),然后会弹出一个 URL,如:www.lb.com/app1.0/.../... 如何根据URL中的应用程序版本定向到应用程序?使用 RewriteCond 和正则表达式并将其传递给 ProxyPass?我真的不知道如何编写脚本,有人可以帮忙吗? :)
编辑:这是我为 1 Tomcat 的 2 个应用程序和 2 Tomcat 的 2 个应用程序所做的,但有时我得到 404,因为 Tomcat 有另一个版本已被负载均衡器选择。
<VirtualHost *:80>
#Add a http header to explicitly identify the node and be sticky
Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e; path=/" env=BALANCER_ROUTE_CHANGED
#Declare the http server pool
<Proxy "balancer://plf">
BalancerMember "http://worker1.com:8080" route=worker1
BalancerMember "http://worker2.com:8080" route=worker2
ProxySet stickysession=ROUTEID
ProxySet lbmethod=bybusyness
</Proxy>
#Common options
ProxyRequests Off
ProxyPreserveHost On
#Declare the redirection for the http requests
ProxyPassMatch "/app(.*)" "balancer://plf/app"
ProxyPassReverse "/app(.*)" "balancer://plf/app"
我是这样做的:
1)定义一个平衡器代理:
<Proxy balancer://portalcluster stickysession=JSESSIONID>
BalancerMember ajp://TOMCATSERVER1:8009 route=TOMCARSERVER1-0
BalancerMember ajp://TOMCATSERVER2:8009 route=TOMCATSERVER2-100
</Proxy>
2) 在您的 VirtualHost 中代理它:
Listen 443
<Virtualhost *:443>
ServerName example.com
Alias /static /var/www/portalstatic
ProxyPass /static !
ProxyPass / balancer://portalcluster/
ProxyPassReverse / balancer://portalcluster/
</Virtualhost>
NB 我从中删除了 lot 与问题无关的配置(日志、拒绝条款、证书指令,...)。这只是为了说明我做代理的方式。
NB2 我确实留下了 /static
技巧,因为这通常是您想要做的事情。静态文件必须保留在 HTTP 上,而不是一直从 Tomcat 发送它们。
我在 Tomcat 的 webapps 文件夹中部署了多个应用程序(app1.0、app1.1、app1.2 等)。当我点击 www.example.com:8080/app1.0 时,会出现相应的应用程序。
但是在负载均衡服务器上怎么办呢?例如,我有一个网站,我可以在该网站上单击一个按钮(app1.0、app1.1、app1.2 等),然后会弹出一个 URL,如:www.lb.com/app1.0/.../... 如何根据URL中的应用程序版本定向到应用程序?使用 RewriteCond 和正则表达式并将其传递给 ProxyPass?我真的不知道如何编写脚本,有人可以帮忙吗? :)
编辑:这是我为 1 Tomcat 的 2 个应用程序和 2 Tomcat 的 2 个应用程序所做的,但有时我得到 404,因为 Tomcat 有另一个版本已被负载均衡器选择。
<VirtualHost *:80>
#Add a http header to explicitly identify the node and be sticky
Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e; path=/" env=BALANCER_ROUTE_CHANGED
#Declare the http server pool
<Proxy "balancer://plf">
BalancerMember "http://worker1.com:8080" route=worker1
BalancerMember "http://worker2.com:8080" route=worker2
ProxySet stickysession=ROUTEID
ProxySet lbmethod=bybusyness
</Proxy>
#Common options
ProxyRequests Off
ProxyPreserveHost On
#Declare the redirection for the http requests
ProxyPassMatch "/app(.*)" "balancer://plf/app"
ProxyPassReverse "/app(.*)" "balancer://plf/app"
我是这样做的:
1)定义一个平衡器代理:
<Proxy balancer://portalcluster stickysession=JSESSIONID>
BalancerMember ajp://TOMCATSERVER1:8009 route=TOMCARSERVER1-0
BalancerMember ajp://TOMCATSERVER2:8009 route=TOMCATSERVER2-100
</Proxy>
2) 在您的 VirtualHost 中代理它:
Listen 443
<Virtualhost *:443>
ServerName example.com
Alias /static /var/www/portalstatic
ProxyPass /static !
ProxyPass / balancer://portalcluster/
ProxyPassReverse / balancer://portalcluster/
</Virtualhost>
NB 我从中删除了 lot 与问题无关的配置(日志、拒绝条款、证书指令,...)。这只是为了说明我做代理的方式。
NB2 我确实留下了 /static
技巧,因为这通常是您想要做的事情。静态文件必须保留在 HTTP 上,而不是一直从 Tomcat 发送它们。