How to configure Apache server load balancer: mod_proxy

Source: Internet
Author: User
Generally, server load balancer distributes client requests to various real backend servers to achieve server load balancer. Another method is to use two servers, one as the Master and the other as the hot backup ),

Generally, server load balancer distributes client requests to various real backend servers to achieve server load balancer. Another method is to use two servers, one as the Master server and the other as the Hot Standby. all requests are distributed to the Master server, switch to the backup server immediately to improve the overall system.
I was surprised to see this title for the first time. can Apache still perform load balancing? It's so powerful. After some investigation, we found that it is true, and its functions are not bad at all. This is all due to the mod_proxy module. It is a powerful Apache.

To put it bluntly, let's explain how to set up server load balancer.

Generally, server load balancer distributes client requests to various real backend servers to achieve server load balancer. Another method is to use two servers, one as the Master server and the other as the Hot Standby. all requests are distributed to the Master server, switch to the backup server immediately to improve the overall system reliability.

Server load balancer settings

Apache can meet the above two requirements. Let's first discuss how to implement load balancing. First, you need to enable several Apache modules:

Program code


LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
LoadModule proxy_http_module modules/mod_proxy_http.so


Mod_proxy provides the proxy server function. mod_proxy_balancer provides the server load balancer function. mod_proxy_http enables the proxy server to support the HTTP protocol. If you change mod_proxy_http to another protocol module (such as mod_proxy_ftp), you may be able to support load balancing for other protocols. if you are interested, try it on your own.

Then add the following configuration:

Program code


ProxyRequests Off
  
BalancerMember http://node-a.myserver.com: 8080
BalancerMember http://node-b.myserver.com: 8080
    
ProxyPass/balancer: // mycluster
# Warning: the following configurations are only used for debugging. do not add them to the production environment !!!
  
SetHandler balancer-manager
Order Deny, Allow
Deny from all
Allow from localhost
    


From the above ProxyRequests Off, we can see that the server load balancer is actually a reverse proxy, but its proxy forwarding address is not a specific server, but a balancer: // protocol:

ProxyPass/balancer: // The mycluster protocol address can be defined at will. Then, set the content of the balancer protocol in the section. The BalancerMember command can be used to add real server addresses in the server load balancer group.

The following section is used to monitor the working conditions of server load balancer. it can be added during debugging (unavailable in the production environment !), Access http: // localhost/balancer-manager/to view the working status of the server load balancer.

OK. restart the server and access the address of the server where your Apache server is located to see the effect of server load balancer. On the balancer-manager page, you can see that requests are evenly distributed.

What should I do if I don't want an average allocation? Add the loadfactor parameter to BalancerMember. the value range is 1-100. For example, if you have three servers and the load distribution ratio is, you only need to set them as follows:

Program code


ProxyRequests Off
  
BalancerMember http://node-a.myserver.com: 8080 loadfactor = 7
BalancerMember http://node-b.myserver.com: 8080 loadfactor = 2
BalancerMember http://node-c.myserver.com: 8080 loadfactor = 1
    
ProxyPass/balancer: // mycluster


By default, server load balancer tries its best to make the number of requests received by each server meet the predefined ratio. If you want to change the algorithm, you can use the lbmethod attribute. For example:

Program code


ProxyRequests Off
  
BalancerMember http://node-a.myserver.com: 8080 loadfactor = 7
BalancerMember http://node-b.myserver.com: 8080 loadfactor = 2
BalancerMember http://node-c.myserver.com: 8080 loadfactor = 1
    
ProxyPass/balancer: // mycluster
ProxySet lbmethod = bytraffic


Possible values of lbmethod include:

Lbmethod = byrequests balanced by the number of requests (default)

Lbmethod = bytraffic balanced by traffic

Lbmethod = bybusyness balanced by the degree of busy (always allocated to the server with the least number of active requests)

For the principles of various algorithms, see [url = signature

Hot Standby)

Hot backup is easy to implement. you only need to add the status = + H attribute to specify a server as a backup server:

Program code


ProxyRequests Off
  
BalancerMember http://node-a.myserver.com: 8080
BalancerMember http://node-b.myserver.com: 8080 status = + H
    
ProxyPass/balancer: // mycluster


From the balancer-manager interface, we can see that requests always flow to node-a. Once node-a fails, Apache will detect errors and distribute requests to node-B. Apache checks the status of node-a every few minutes. if node-a recovers, it continues to use node-.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.