Reverse proxy and load balancing in Apache 2.4

The content of the article is from the official document: http://httpd.apache.org/docs/2.4/howto/reverse_proxy.html

Like nginx, Apache httpd provides reverse proxy and load balance.

The following modules need to be enabled in the Apache httpd configuration:

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

# This also needs to be turned on, otherwise httpd starts to report an error
LoadModule slotmem_shm_module modules/mod_slotmem_shm.so

# The following load balancing strategies are enabled on demand
LoadModule lbmethod_bybusyness_module modules/mod_lbmethod_bybusyness.so
LoadModule lbmethod_byrequests_module modules/mod_lbmethod_byrequests.so
LoadModule lbmethod_bytraffic_module modules/mod_lbmethod_bytraffic.so
LoadModule lbmethod_heartbeat_module modules/mod_lbmethod_heartbeat.so

Enable mod under linux_ proxy_ hcheck.so To check member survival (dynamic health check)

mod_lbmethod module is on-demand, if only on-demand, mod is on_ lbmethod_ bytraffic.so that will do

mod_proxy provides proxy function, mod_proxy_balancer provides load balancing function, mod_proxy_http enables the proxy server to support the HTTP protocol.

Instructions needed: ProxyPass, ProxyPassReverse, BalancerMember

Implement simple reverse proxy

Directive ProxyPass maps input requests to back-end servers (or server clusters). Let's take a simple example of reverse proxy all requests ("/") to a server:

ProxyPass "/"  "http://www.example.com/"

To ensure that the "Location" header generated from the back-end server is modified to point to the reverse proxy instead of returning to itself (forming a dead cycle), the ProxyPassReverse instruction needs to be added:

ProxyPass "/"  "http://www.example.com/"
ProxyPassReverse "/"  "http://www.example.com/"

If you want to set the specified url to be reverse proxy, set it as follows:

ProxyPass "/images"  "http://www.example.com/"
ProxyPassReverse "/images"  "http://www.example.com/"

In the above example, all requests starting with "/ images" are proxied to the specified backend, and other requests are processed locally

ProxyPass "/"  "http://www.example.com/"

Clusters and balancers

Through balancer: / /, an equalizer is set to reverse proxy requests to multiple back-end servers.

<Proxy balancer://myset>
    BalancerMember http://www2.example.com:8080
    BalancerMember http://www3.example.com:8080
    ProxySet lbmethod=bytraffic
</Proxy>
ProxyPass "/images/"  "balancer://myset/"
ProxyPassReverse "/images/"  "balancer://myset/"

The above code proxies the request starting with "/ images /" to two back-end servers defined by BalancerMembers, and uses ProxySet to define the load balancing policy.

Equalizer members are sometimes referred to as workers

Balancer and balancermember configuration

The default is to distribute the load evenly. If you need to specify it, you can set the load factor.

<Proxy balancer://myset>
    BalancerMember http://www2.example.com:8080 loadfactor=8
    BalancerMember http://www3.example.com:8080 loadfactor=2
    ProxySet lbmethod=bytraffic
</Proxy>
ProxyPass "/images/"  "balancer://myset/"
ProxyPassReverse "/images/"  "balancer://myset/"

Two servers are set up above, and the load distribution ratio is set to 8:2. If you do not set loadactor, the default is 1.

If you want to set the load of www3 to twice that of www2 and the timeout time is 1s, you can set it as follows:

<Proxy balancer://myset>
    BalancerMember http://www2.example.com:8080
    BalancerMember http://www3.example.com:8080 loadfactor=3 timeout=1
    ProxySet lbmethod=bytraffic
</Proxy>
ProxyPass "/images"  "balancer://myset/"
ProxyPassReverse "/images"  "balancer://myset/"

Failover (Failover)

You can also fine tune various failover scenarios to specify which worker threads or even which equalizers should be accessed in this case. The following settings implement three failover scenarios:

<Proxy balancer://myset>
    BalancerMember http://www2.example.com:8080 loadfactor=7
    BalancerMember http://www3.example.com:8080 loadfactor=3
    BalancerMember http://spare1.example.com:8080 status=+R
    BalancerMember http://spare2.example.com:8080 status=+R
    BalancerMember http://hstandby.example.com:8080 status=+H
    BalancerMember http://bkup1.example.com:8080 lbset=1
    BalancerMember http://bkup2.example.com:8080 lbset=1
    ProxySet lbmethod=byrequests
</Proxy>
ProxyPass "/images/"  "balancer://myset/"
ProxyPassReverse "/images/"  "balancer://myset/"
  1. When one or both of www3 and www2 are unavailable, the traffic will be allocated to spare1 and spare2. (a redundant end will be used to replace an unavailable member of this equalizer. )
  2. When all the threads (workers) with lbset=0 are unavailable, the traffic will be allocated to hstandby.
  3. If all threads, redundancy and hot standby with lbset=0 are unavailable, bkup1 and bkup2 of the backup machine with blset=1 will enter the cycle (allocated to traffic) only when and only when.

Each equalizer setting can use one or more hot spares and hot standby members.

For failover, sparing is used to replace the unavailable threads in the same load balancer. A thread is considered unavailable if it is running out, stopped, or in an error / failure state. If all threads and sparing in the load balancer set are not available, hot spare is enabled. Always try the load balancer set (and their respective redundancy and hot spares) in the lowest to highest order.

Equalizer Manager

Apache's built-in balancer manager can display the current configuration and working status. You can not only display parameters, but also dynamically reconfigure most parameters at run time, including adding new members to existing equalizers.

In the Apache httpd configuration file, add:

<Location "/balancer-manager">
    SetHandler balancer-manager
    Require host localhost
</Location>

Note: the production server cannot be opened unless you have already done so security setting.

Access after restarting httpd http://yourdomain/balancer-manager/ :

Tested: balancer manager can only be placed in configuration file httpd.conf It is not valid in VirtualHost

Dynamic health check

Before the httpd reverse agent makes a request to a thread, it can "test" whether the thread is available by setting the parameter ping for the thread using ProxyPass. In general, it is more useful to dynamically check the health of threads out of band. This is through the mod of Apache httpd_ proxy_ Hcheck module.

Practical operation

Before profile httpd.conf Setting up a reverse proxy in affects the entire site on the server. If there are multiple sites (domain names) on a server, you need to specify some domain names to use reverse proxy, then you can transfer the configuration to VirtualHost (cancel at the same time httpd.conf balancer settings in).

Now we do load balancing test directly under the local apache service.

At httpd-vhost.conf Add the following configuration to:

<VirtualHost *:80>
    ServerAdmin aben_sky@qq.com
    ServerName www.example.com
    ErrorLog "d:/wamp/logs/www.example.com.log"
    CustomLog "d:/wamp/logs/www.example.com.log" combined
	
	#Load Balance===========================
	#Close forward proxy ProxyRequests
	ProxyRequests Off
    <Proxy balancer://myset>
        BalancerMember http://www2.example.com loadfactor=5
        BalancerMember http://www3.example.com loadfactor=5
        ProxySet lbmethod=bytraffic
    </Proxy>
    ProxyPass "/"  "balancer://myset/"
    ProxyPassReverse "/"  "balancer://myset/"
</VirtualHost>
<VirtualHost *:80>
    DocumentRoot "E:/WWW/example.com/www2/"
    ServerName www2.example.com
    ErrorLog "d:/wamp/logs/www.example.com.log"
    CustomLog "d:/wamp/logs/www.example.com.log" combined

    <Directory "E:/WWW/example.com/www2/">
        Options FollowSymLinks
        AllowOverRide all
        Order Deny,Allow
        Allow from all
        Require all granted
    </Directory>
</VirtualHost>
<VirtualHost *:80>
    DocumentRoot "E:/WWW/example.com/www3/"
    ServerName www3.example.com
    ErrorLog "d:/wamp/logs/www.example.com.log"
    CustomLog "d:/wamp/logs/www.example.com.log" combined

    <Directory "E:/WWW/example.com/www3/">
        Options FollowSymLinks
        AllowOverRide all
        Order Deny,Allow
        Allow from all
        Require all granted
    </Directory>
</VirtualHost>

There are three websites: www, www2, www3. WWW is the control end of load balancing, and www2 and www3 are the actual websites. We set up a file under the entry folder of www2 and www3 index.php The contents of the document are as follows:

<?php
session_start();
echo date('Y-m-d H:i:s').'<br/>';
echo $_SERVER['HTTP_HOST'].'<br/>';
var_dump($_SERVER);

Then add domain name resolution in the local hosts, and then visit http://www.example.com have a look:

This completes the construction of a simple load balancing service.

If BalancerMember is distributed on different servers and session synchronization is needed, the method of folder synchronization and saving to memcache/redis can be used.

Keywords: Programming Apache Load Balance PHP Nginx

Added by vund0 on Sat, 13 Jun 2020 11:05:30 +0300