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 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 availability of the 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.

1. Server Load balancer settings 

1). basic configuration 
Apache can meet the above two requirements. Let's first discuss how to implement load balancing. Assume that the domain name of an Apache server is www.a.com. You must first enable several modules of Apache:

Httpd. conf code
  1. Loadmodule proxy_module modules/mod_proxy.so
  2. Loadmodule proxy_balancer_module modules/mod_proxy_balancer.so
  3. 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:

Httpd. conf code
  1. Proxyrequests off
  2. <Proxy balancer: // mycluster>
  3. Balancermember http://node-a.myserver.com: 8080
  4. Balancermember http://node-b.myserver.com: 8080
  5. </Proxy>
  6. Proxypass/balancer: // mycluster/
  7. # Warning: The following configurations are only used for debugging. Do not add them to the production environment !!!
  8. <Location/balancer-Manager>
  9. Sethandler balancer-Manager
  10. Order deny, allow
  11. Deny from all
  12. Allow from localhost
  13. </Location>



Note: node-a.myserver.com, node-b.myserver.com is the domain name of the other two servers, not the domain name of the current server

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 <proxy> section. The balancermember command can be used to add real server addresses in the Server Load balancer group.

The following <location/balancer-Manager> 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 your Apache server (www.a.com) to see the effect of Server Load balancer.

Error prompt:
The webpage prompts internal serveral error. Check the error. log file.

Error. Log Code
  1. [Warn] Proxy: no protocol handler was valid for the URL/admin/login_form. if you are using a DSO version of mod_proxy, make sure the proxy submodules are encoded in the configuration using loadmodule.


The reason is that the configuration: # proxypass/balancer: // mycluster may be missing one/

2). Load Ratio allocation 
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:

Httpd. conf code
  1. Proxyrequests off
  2. <Proxy balancer: // mycluster>
  3. Balancermember http://node-a.myserver.com: 8080 loadfactor = 7
  4. Balancermember http://node-b.myserver.com: 8080 loadfactor = 2
  5. Balancermember http://node-c.myserver.com: 8080 loadfactor = 1
  6. </Proxy>
  7. Proxypass/balancer: // mycluster



3). Load Distribution Algorithm 

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:

Httpd. conf code
  1. Proxyrequests off
  2. <Proxy balancer: // mycluster>
  3. Balancermember http://node-a.myserver.com: 8080 loadfactor = 7
  4. Balancermember http://node-b.myserver.com: 8080 loadfactor = 2
  5. Balancermember http://node-c.myserver.com: 8080 loadfactor = 1
  6. </Proxy>
  7. Proxypass/balancer: // mycluster
  8. 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 the Apache documentation.

2. 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:

Httpd. conf code
  1. Proxyrequests off
  2. <Proxy balancer: // mycluster>
  3. Balancermember http://node-a.myserver.com: 8080
  4. Balancermember http://node-b.myserver.com: 8080 status = + H
  5. </Proxy>
  6. 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-.

How to configure Apache Server Load balancer: mod_proxy

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.