使用Apache进行负载均衡路由

时间:2021-11-12 20:31:32

My application has two internal section:

我的申请有两个内部部分:

  1. Upload section url: %URI%/upload/%action%
  2. 上传部分网址:%URI%/ upload /%action%

  3. Login section url: %URL%/Login/%action%
  4. 登录部分网址:%URL%/登录/%操作%

The code for Login section is located on one server 5.123.12.1, whereas the code for Upload is located on another server 5.123.12.2.

Login部分的代码位于一个服务器5.123.12.1上,而Upload的代码位于另一个服务器5.123.12.2上。

After the server routing, there will be further routing pertaining to the request parameters or action parameters.

在服务器路由之后,将存在与请求参数或动作参数有关的进一步路由。

How to construct rules for mod-rewrite and mod-cond in Apache configuration file for the two machines so that the routing can be done?

如何在两台机器的Apache配置文件中构建mod-rewrite和mod-cond的规则,以便可以完成路由?

2 个解决方案

#1


For your setup I would use the following on another Apache server running as a front end.

对于您的设置,我将在另一个作为前端运行的Apache服务器上使用以下内容。

<VirtualHost *:80>
  ServerName    your.domain.com
  RewriteEngine on
  ProxyPreserveHost on
  ProxyPassReverse /    http://5.123.12.1/
  ProxyPassReverse /    http://5.123.12.2/

  RewriteRule ^/Login(.*)   http://5.123.12.1/Login$1 [P,L]
  RewriteRule ^/upload(.*)  http://5.123.12.2/upload$1 [P,L]

</VirtualHost>

This is assuming that you have mod_rewrite enabled. I'm not sure you can use the ip address in the rewrite rules so you may have to set up an internal host name for the two servers you have listed.

这假设您已启用mod_rewrite。我不确定您是否可以在重写规则中使用IP地址,因此您可能必须为列出的两个服务器设置内部主机名。

#2


You may be better off using a dedicated bit of load-balancing software such as HAProxy or Perlbal. The advantage being that you can balance your requests for the same URL across more than one backend server.

您可能最好使用专用的负载平衡软件,如HAProxy或Perlbal。优点是您可以跨多个后端服务器平衡对同一URL的请求。

Failing that, there is a balancer module for Apache called mod_proxy_balancer:

如果做不到这一点,Apache有一个名为mod_proxy_balancer的平衡器模块:

<Proxy balancer://mycluster>
 BalancerMember http://192.168.1.50:80
 BalancerMember http://192.168.1.51:80
</Proxy>
ProxyPass /test balancer://mycluster/

#1


For your setup I would use the following on another Apache server running as a front end.

对于您的设置,我将在另一个作为前端运行的Apache服务器上使用以下内容。

<VirtualHost *:80>
  ServerName    your.domain.com
  RewriteEngine on
  ProxyPreserveHost on
  ProxyPassReverse /    http://5.123.12.1/
  ProxyPassReverse /    http://5.123.12.2/

  RewriteRule ^/Login(.*)   http://5.123.12.1/Login$1 [P,L]
  RewriteRule ^/upload(.*)  http://5.123.12.2/upload$1 [P,L]

</VirtualHost>

This is assuming that you have mod_rewrite enabled. I'm not sure you can use the ip address in the rewrite rules so you may have to set up an internal host name for the two servers you have listed.

这假设您已启用mod_rewrite。我不确定您是否可以在重写规则中使用IP地址,因此您可能必须为列出的两个服务器设置内部主机名。

#2


You may be better off using a dedicated bit of load-balancing software such as HAProxy or Perlbal. The advantage being that you can balance your requests for the same URL across more than one backend server.

您可能最好使用专用的负载平衡软件,如HAProxy或Perlbal。优点是您可以跨多个后端服务器平衡对同一URL的请求。

Failing that, there is a balancer module for Apache called mod_proxy_balancer:

如果做不到这一点,Apache有一个名为mod_proxy_balancer的平衡器模块:

<Proxy balancer://mycluster>
 BalancerMember http://192.168.1.50:80
 BalancerMember http://192.168.1.51:80
</Proxy>
ProxyPass /test balancer://mycluster/