(go) Ubuntu apache2 load balancing and reverse proxy

Source: Internet
Author: User

Apache function is actually very powerful, recently studied the next Apache reverse proxy and load balancing, reverse proxy network network export and my blog export is through the reverse proxy Apache implementation, summed up, the emphasis is on the configuration of load balance.

First, let's start with the experimental environment.

Operating system: DEBIAN 5.03 (Ubuntu can also be configured similarly)

First, install Apache loading module

Apt-get Install Apache2
Then go to the Apache configuration directory
Cd/etc/apache2
Apache reverse proxy and load balancing is actually using the principle of reverse proxy, as to what is called reverse proxy can point this http://baike.baidu.com/view/1165595.htm

The key needs to load the following three modules
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

Due to the Debian system under all the Apache modules are placed inside the/usr/lib/apache2/modules/, through the/etc/apache2/mods-available inside the *.load loaded, if you want to module, you need to Etc/apache2/mods-enabled inside with Ln to establish links, of course you can also/etc/apache2/apache2.conf inside directly to the above three sentences written into, but do not recommend this. The execution code is as follows
Ln-s/etc/apache2/mods-available/proxy.load/etc/apache2/mods-enabled/proxy.load
Ln-s/etc/apache2/mods-available/proxy_http.load/etc/apache2/mods-enabled/proxy_http.load
Ln-s/etc/apache2/mods-available/proxy_balancer.load/etc/apache2/mods-enabled/proxy_banancer.load

After a few steps, we'll restart Apache.
/etc/init.d/apache2 restart

Second, configure the reverse proxy function

After the module is loaded, a virtual host can be established to act as a reverse proxy
Vi/etc/apache2/sites_available/proxy
Configured as follows
Listen 202.xxx.xxx.xxx:80 (IP and ports accessed by others)
<virtualhost 202.xxx.xxx.xxx:80> (IBID.)
ServerAdmin [email protected]
Proxyrequests Off (indicates that the reverse proxy is turned on)
Proxymaxforwards 100
Proxypreservehost on
proxypass/http://10.x.xx.xxx/(forwarding to a request on a URL)
proxypassreverse/http://10.x.xx.xxx/
<proxy *> (This section is access control)
Order Deny,allow
Allow from all
</Proxy>
</VirtualHost>
And then create the ln link inside the/etc/apache2/sites_enabled/.
Ln-s/etc/apache2/sites_available/proxy/etc/apache2/sites_enabled/proxy
Restart Apache
/etc/init.d/apache2 restart

And then the reverse proxy is turned on.
When others enter http://202.xxx.xxx.xxx, they go through the reverse proxy to the http://10.x.xx.xxx, so the simple reverse proxy function opens the

C. Apache Load Balancer Configuration

A, simple load balancing

Then go ahead and talk about the Apache Load Balancer module.
In the same vein, create a virtual host to load balance
Vi/etc/apache2/sites_available/balancer
Configured as follows
Listen 202.x.xx.xxx:80 (IP and ports accessed by others)
<virtualhost 202.x.xx.xxx:80>
ServerAdmin [email protected]
Proxyrequests OFF
proxypass/balancer://proxy/
<proxy balancer://proxy>
Order Deny,allow
Allow from all
Balancermember http://10.0.0.1
Balancermember http://10.0.0.2
</Proxy>
</VirtualHost>

As can be seen from the above proxyrequests Off, the load balancer is actually a reverse proxy, but its proxy forwarding address is not a specific server, but a balancer://protocol address
proxypass/balancer://proxy/

The protocol address can be defined casually. Then, set the contents of the Balancer protocol in the <Proxy> section. The balancermember instruction can add a real server address in a Load balancer group.

And then create the ln link inside the/etc/apache2/sites_enabled/.
Ln-s/etc/apache2/sites_available/balancer/etc/apache2/sites_enabled/balancer
Restart Apache
/etc/init.d/apache2 restart
So Apache's load balancer is configured.

If you access the HTTP://202.X.XX.XXX will be evenly open http://10.0.0.1 and http://10.0.0.2, such as you open two times the http://202.x.xx.xxx will open again/HTTP// 10.0.0.1 once open http://10.0.0.2 so that is two servers accepted the average, to achieve the effect of load balancing.

b, load ratio distribution

Of course if you find that your two servers are not configured the same, one is better and one is poor, then you have to start configuring load balancing at different scales. If two servers you want to configure the load distribution ratio to 3:1 then the configuration file is as follows

Listen 202.x.xx.xxx:80 (IP and ports accessed by others)
<virtualhost 202.x.xx.xxx:80>
ServerAdmin [email protected]
Proxyrequests OFF
proxypass/balancer://proxy/
<proxy balancer://proxy>
Order Deny,allow
Allow from all
Balancermember http://10.0.0.1 loadfactor= 3
Balancermember http://10.0.0.2 loadfactor= 1
</Proxy>
</VirtualHost>

c, load distribution algorithm

Of course you might want to use different algorithms for load balancing, for example, according to the number of requests, or according to the flow balance, the instructions used here are Lbmethod
Possible values for Lbmethod are:
Lbmethod=byrequests by Request number equalization (default)
Lbmethod=bytraffic according to the flow balance
Lbmethod=bybusyness in a busy balance (always assigned to the server with the fewest number of active requests)

According to the balance of traffic configuration is as follows

Listen 202.x.xx.xxx:80        (IP and port accessed by someone else)
<virtualhost 202.x.xx.xxx : 80>
     ServerAdmin [email protected]
     proxyrequests OFF
     proxypass/balancer://proxy/ 
     proxyset lbmethod= bytraffic   (plus this sentence)
     <proxy balancer://proxy>
          Order deny,allow
         allow from all
         balancermember http://10.0.0.1   loadfactor= 3
         balancermember http://10.0.0.2   loadfactor= 1
     </proxy>
</virtualhost>
so your load balancer can be balanced by traffic.

D, hot backup
at the end of the hot backup, the implementation of hot backup is very simple, just add the Status=+h property, you can designate a server as a backup server, the configuration file is as follows
Listen 202.x.xx.xxx:80         (IP and port accessed by others)
<virtualhost 202.x.xx.xxx:80>
     ServerAdmin [email protected]
     proxyrequests Off
     PROXYPASS/BALANCER://PROXY/&NBSP
     proxyset lbmethod=bytraffic   (plus this sentence)
     <proxy balancer://proxy>
         Order Deny,allow
         allow from all
          balancermember http://10.0.0.1   
          balancermember http://10.0.0.2   status=+h
     </proxy>
</virtualhost>

From the configuration, it can be seen that the request always flows to http://10.0.0.1, and once http://10.0.0.1 hangs, Apache detects an error and diverts the request to http://10.0.0.2. Apache will check the status of http://10.0.0.1 every few minutes, if http://10.0.0.1 recovery, continue to use http://10.0.0.1, so that you can achieve hot backup

(go) Ubuntu apache2 load balancing and reverse 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.