Apache load balancing settings: mod

  
                  

Generally speaking, load balancing is to distribute the client's request to each real server on the back end to achieve load balancing. Another way is to use two servers, one as the primary server (Master) and the other as the hot standby (Hot Standby), requesting all the parts to the primary server, and immediately switching to the backup server when the primary server is down. To improve the overall availability of the system.

I was also surprised when I first saw this title. Is Apache still able to do load balancing? It’s too powerful. After some investigation, it was found that it was ok, and the function was not bad at all. This is due to the mod_proxy module. It is worthy of powerful Apache.

Nonsense, let's explain the load balancing method.

Generally speaking, load balancing is to distribute the client's request to each real server on the back end to achieve load balancing. Another way is to use two servers, one as the primary server (Master) and the other as the hot standby (Hot Standby), requesting all the parts to the primary server, and immediately switching to the backup server when the primary server is down. To improve the overall reliability of the system.

1. Load Balancing Settings

1).Basic Configuration

Apache can handle both of these requirements. Let's talk about how to do load balancing. Assuming that the domain name of an apache server is www.a.com, you first need to enable several modules of Apache:

Httpd.conf 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 proxy server functionality, mod_proxy_balancer provides load balancing, and mod_proxy_http allows proxy servers to support HTTP. If you change mod_proxy_http to other protocol modules (such as mod_proxy_ftp), you may be able to support load balancing of other protocols. Interested friends can try it for themselves.

Then add the following configuration:

Httpd.conf code ProxyRequests Off <Proxy balancer://mycluster> BalancerMember http://node-a.myserver.com:8080 BalancerMember http: //node-b.myserver.com:8080 </Proxy> ProxyPass /balancer://mycluster/# Warning: The following configuration is for debugging purposes only, never add it to production! ! ! <Location /balancer-manager> SetHandler balancer-manager order Deny, Allow Deny from all Allow from localhost </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

As can be seen from the above ProxyRequests Off, the load balancer is actually a reverse proxy, except that its proxy forwarding address is not a certain A specific server, but a balancer://protocol:

ProxyPass /balancer://mycluster protocol address can be defined casually. Then, set the content of the balancer protocol in the segment. The BalancerMember command adds the real server address in the load balancing group.

The following paragraph is used to monitor the load balancing work, can be added during debugging (forbidden in production environment!), then visit http://localhost/balancer-manager/to see Load balancing work conditions.

OK, restart the server after the change, access the address of your Apache server (www.a.com), you can see the effect of load balancing.

Error prompt:

Visit the web page prompt Internal Serveral Error, view the error.log file

Error.log code [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 included in the configuration using LoadModule.

The reason is: # ProxyPass /balancer://mycluster may be missing one /

2). Load ratio allocation

Open the balancer-manager interface and you can see that the request is evenly distributed.

What if I don't want to distribute evenly? Add the loadfactor parameter to BalancerMember, ranging from 1-100. For example, if you have three servers with a load distribution ratio of 7:2:1, just set this up:

Httpd.conf code ProxyRequests Off <Proxy balancer://mycluster> 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 </Proxy> ProxyPass /Balancer://mycluster

3). Load Allocation Algorithm

By default, load balancing tries to make the number of requests accepted by each server to a preset ratio. If you want to change the algorithm, you can use the lbmethod property. Such as:

Httpd.conf code ProxyRequests Off <Proxy balancer://mycluster> 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 </Proxy> ProxyPass /balancer://mycluster ProxySet lbmethod=bytraffic

lbmethod possible Values ​​are:

lbmethod=byrequests Balanced by Requests (default)

lbmethod=bytraffic Follow Traffic Balance

lbmethod=bybusyness Balanced by Busyness (Always assigned to Active The server with the fewest requests)

See the Apache documentation for the principles of various algorithms.

Copyright © Windows knowledge All Rights Reserved