How does Apache accomplish the load balancing policy configuration

  • 2020-05-07 20:49:54
  • OfStack

With the continuous increase of the traffic, as well as the demanding requirements of the response speed, it is particularly important to set up load balancing. In the initial design of the company's system, the planning of load balancing has been taken into account. Two static servers of www were configured. Due to the tight time of the initial project and low traffic, only one was used, and the other one was in the Intranet. This is a simple test of load balancing.

First, I will introduce some configuration rules of apache mod_proxy_balancer:

There are three different ways to deploy Apache as LoadBalance front engine, which are:

1) poll the configuration of equilibrium policy
Go to the conf directory of Apache, open the httpd.conf file, and add:
ProxyPass/balancer://proxy/ # note that it ends with "/"
 
<Proxy balancer://proxy> 
BalancerMember http://192.168.6.37:6888/ 
BalancerMember http://192.168.6.38:6888/ 
</Proxy> 

We will observe the above parameters "ProxyPass/balancer://proxy/", where "ProxyPass" is the command to configure the virtual server, "/" represents the URL prefix to send Web request, such as: http://myserver/ or http://myserver/aaa, these URL will meet the above filter conditions; "balancer://proxy/" indicates that load balancing is to be configured, and proxy stands for load balancing name; BalancerMember and the URL that follows it represent the backend server to be configured, where URL is URL when requested by the backend server. The above configuration is taken as an example. The principle of load balancing is as follows:

Assuming Apache received http: / / localhost aaa request, because the request ProxyPass conditions (its URL prefix "/"), the request will be distributed to the background one 1 BalancerMember, for example, the request may be forwarded to the http: / / 192.168.6.37:6888 / aaa for processing. When the second URL to meet the conditions of the request, the request may be distributed to the other one BalancerMember, for example, may be forwarded to the http: / / 192.168.6.38:6888 /. The mechanism of load balancing is realized by repeating the loop.

2) the configuration of balanced strategy according to weight allocation
ProxyPass/balancer://proxy/ # note that it ends with "/"
 
<Proxy balancer://proxy> 
BalancerMember http://192.168.6.37:6888/ loadfactor=3 
BalancerMember http://192.168.6.38:6888/ loadfactor=1 
</Proxy> 

The parameter "loadfactor" indicates the weight of the backend server load to the request sent by Apache, which defaults to 1 and can be set to any value between 1 and 100. Above the configuration as an example, this paper introduces how to realize the load balance, according to the weight distribution hypothesis Apache received http now: / / myserver/aaa 4 times such a request, the request is load to the backend server respectively, has three consecutive such request is load to BalancerMember http: / / 192.168.6.37:6888 server, there are one such request is load BalancerMember http: / / 192.168.6.38:6888 backend server. The equilibrium strategy of continuous weight allocation is realized.

3) configuration of load balancing policy for weight request response
ProxyPass/balancer://proxy/ lbmethod=bytraffic # note that this ends with "/"
 
<Proxy balancer://proxy> 
BalancerMember http://192.168.6.37:6888/ loadfactor=3 
BalancerMember http://192.168.6.38:6888/ loadfactor=1 
</Proxy> 

The parameter "lbmethod=bytraffic" indicates the number of bytes the backend server loads the request and response, and the number of bytes processed is expressed in terms of weights. "loadfactor" represents the weight of the number of bytes that the backend server handles load requests and responses, which defaults to 1 and can be set anywhere from 1 to 100. Both scale load according to the above configuration is so, assuming Apache received http: / / myserver aaa request, forwards the request to the background server, if a BalancerMember http: / / 192.168.6.37:6888 backend server load to this request, it processes the request and response is the number of bytes BalancerMember http: / / 192.168.6.38:6888 server 3 times (back (2) the equilibrium configuration, (2) the weight load balance on the number of requests, (3) load balancing with traffic as the weight, which is the biggest difference).

See that there is no, according to different needs, can be configured in these three ways. I followed the third configuration, and it felt like this load balancing was more comprehensive. My configuration is simple, as follows:

First configure the equalizer:
 
<Proxy balancer://proxy> 
BalancerMember ajp://127.0.0.1:8009/ loadfactor=1 
BalancerMember http://192.168.10.6:8083/ loadfactor=1 
</Proxy> 

http: / / 192.168.10.6, : 8083 is actually a port start apache additionally, in order to test, it is a simple forward all requests to tomcat directly.
The following modifications can be made to the last VirtualHost:
 
<VirtualHost *:80> 
ServerName www.tsingyuan.cn 
DocumentRoot /www 
DirectoryIndex index.html index.jsp 
<Directory  " /www " > 
Options Indexes FollowSymLinks 
AllowOverride None 
Order allow,deny 
Allow from all 
</Directory> 
<Directory  " /control " > 
Options Indexes FollowSymLinks 
AllowOverride None 
Order allow,deny 
Allow from all 
</Directory> 
ProxyPass /nxt/images/ ! 
ProxyPass /nxt/js/ ! 
ProxyPass /nxt/css/ ! 
#ProxyPass / ajp://127.0.0.1:8009/ 
#ProxyPassReverse / ajp://127.0.0.1:8009/ 
ProxyPass / balancer://proxy/ 
ProxyPassReverse / balancer://proxy/ 
</VirtualHost> 

Comment out the previous ajp forwarding and configure it to be handled by balancer.

By observing access log, some of the requests were indeed sent to apache on port 8083, and some were forwarded directly to tomcat by ajp.

The above is the detailed description of how Apache completes the load balancing policy configuration. I hope this article can be helpful to php developers. Thanks for reading this article.

Related articles: