Apache Load Balancing Setup Method mod_proxy use Introduction _linux

Source: Internet
Author: User
In general, load balancing is the client's request to the backend of the various real servers to achieve load balancing purposes. There is another way is to use two servers, one as the primary server (master), the other as a hot Standby, the request is all to the primary server, when the primary server, immediately switch to the backup server, to improve the overall system can
The first time I saw this headline I was surprised that Apache can still do load balancing? It's so powerful. After a survey found that it can, and the function is not bad. This is thanks to the Mod_proxy module. Is worthy of the mighty Apache AH.

less nonsense, the following is to explain the load balancing method of setting.

In general, load balancing is the client's request to the backend of the various real servers to achieve load balancing purposes. There is another way is to use two servers, one as the primary server (master), the other as a hot Standby, the request all to the primary server, when the primary server when, immediately switch to the backup server to improve the overall reliability of the system.

1. Load Balancing Settings

1). Basic Configuration
Apache can handle both of these requirements. Let's discuss how to do load balancing first. Assuming an Apache server domain name is www.a.com, you first need to enable several Apache modules:
httpd.conf Code
Copy Code code as follows:

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 enables proxy servers to support HTTP protocols. If you switch mod_proxy_http into other protocol modules (such as MOD_PROXY_FTP), you might be able to support other protocols with load balancing, and interested friends can try them on their own.

Then you add the following configuration:
httpd.conf Code
Copy Code code as follows:

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 only and never added to the production environment!!!
<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

From the above proxyrequests off this can be seen, in fact, the load balancer is a reverse proxy, but its proxy forwarding address is not a specific server, but a balancer://protocol:

The Proxypass/balancer://mycluster protocol address can be defined casually. Then, set the contents of the Balancer protocol in the <Proxy> section. The Balancermember directive can add a real server address in a load-balancing group.

The following section <Location/balancer-manager> is used to monitor load balancing work, and debugging can be added (in production environment prohibited!) And then access the http://localhost/balancer-manager/to see the load-balancing work situation.

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

Error Tip:
Visit the Web page prompts internal serveral Error, view Error.log file
Error.log Code
Copy Code code as follows:

[Warn] Proxy:no protocol handler is valid for the url/admin/login_form. If you are are using a DSO version of Mod_proxy, make sure the proxy submodules are included in the configuration using LOADMO Dule.


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

2). Load Proportional Distribution
Open the Balancer-manager interface and you can see that the request is evenly distributed.

What if you don't want to distribute evenly? Add the Loadfactor parameter to the Balancermember, the value range is 1-100. For example, you have three servers, the load allocation ratio is 7:2:1, just this set:
httpd.conf Code
Copy Code code as follows:

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 Assignment algorithm

By default, load balancing tries to make the number of requests accepted by each server meet the preset proportions. If you want to change the algorithm, you can use the Lbmethod property. Such as:
httpd.conf Code
Copy Code code as follows:

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 is balanced according to the number of requests (default)
Lbmethod=bytraffic according to the flow balance
Lbmethod=bybusyness in accordance with the busy level (always assigned to the server with the least number of active requests)

The principles of the various algorithms refer to the Apache documentation.

2. Thermal backup (Hot Standby)
The implementation of hot backup is simple, you can designate a server as a backup server simply by adding the Status=+h attribute:

httpd.conf Code
Copy Code code as follows:

Proxyrequests off
<proxy balancer://mycluster>
Balancermember http://node-a.myserver.com:8080
Balancermember http://node-b.myserver.com:8080 Status=+h
</Proxy>
Proxypass/balancer://mycluster

As you can see from the Balancer-manager interface, the request always flows to the node-a, and once node-a hangs off, Apache detects the error and streams the request to node-b. Apache detects node-a every few minutes and continues to use Node-a if node-a resumes.

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.