Apache Reverse Proxy and Load Balancing
1. Reverse Proxy Overview
A reverse proxy is a server that receives requests from clients on the internet and forwards them to servers on an internal network. It then returns the server's responses to the requesting clients. From the external perspective, the reverse proxy appears as the original server, hiding the internal infrastructure details.
2. How Reverse Proxy Functions
Unlike traditional proxy servers designed for internal network access to the internet, reverse proxies enable external access to internal resources. Traditional proxies require client configuration, while reverse proxies function transparently to external clients. They appear as standard web servers but don't host actual content; instead, they direct requests to internal servers that store the real data.
This architecture enhances security by isolating internal servers from direct internet exposure. Reverse proxy implementations can coexist with packet filtering or standard proxy methods, creating comprehensive security solutions.
3. Benefits of Reverse Proxy
- Enhanced Security: All internet requests must pass through the proxy server first
- Caching: Can cache static resources from backend servers, reducing their load
- Load Distribution: Evenly distributes requests across multiple backend servers
4. Practical Implementation with Apache
4.1 Environment Setup
Prepare two CentOS 7 virtual machines:
# Backup existing repository configuration
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
# Download new repository files
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
# Install development tools
yum install -y gcc glibc gcc-c++ make screen tree lrzsz
4.2 Backend Server Configuration
Configure server1:
# Install Apache
yum install -y httpd
# Change default HTTP port to 8080
sed -i 's/Listen 80/Listen 8080/g' /etc/httpd/conf/httpd.conf
# Create test page
echo 'backend-server-1' > /var/www/html/index.html
# Start Apache
systemctl start httpd
systemctl enable httpd
# Test the configuration
curl http://10.0.0.101:8080/
# Should display: backend-server-1
Configure server2:
# Install Apache
yum install -y httpd
# Change default HTTP port to 8080
sed -i 's/Listen 80/Listen 8080/g' /etc/httpd/conf/httpd.conf
# Create test page
echo 'backend-server-2' > /var/www/html/index.html
# Start Apache
systemctl start httpd
systemctl enable httpd
# Test the configuration
curl http://10.0.0.102:8080/
# Should display: backend-server-2
4.3 Setting Up Apache as Reverse Proxy
On server1, compile and install Apache 2.4.25 as the reverse proxy:
cd /usr/local/src/
wget http://mirrors.hust.edu.cn/apache/httpd/httpd-2.4.25.tar.gz
tar zxf httpd-2.4.25.tar.gz
cd httpd-2.4.25
./configure --prefix=/usr/local/apache-proxy --enable-so --enable-modules="all"
make && make install
ln -s /usr/local/apache-proxy/ /usr/local/apache
Create the reverse proxy configuration file:
vim /usr/local/apache/conf/extra/reverse-proxy.conf
# Load required modules
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_connect_module modules/mod_proxy_connect.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
LoadModule lbmethod_byrequests_module modules/mod_lbmethod_byrequests.so
LoadModule lbmethod_bytraffic_module modules/mod_lbmethod_bytraffic.so
LoadModule lbmethod_bybusyness_module modules/mod_lbmethod_bybusyness.so
LoadModule slotmem_shm_module modules/mod_slotmem_shm.so
# Disable proxy requests for security
ProxyRequests Off
# Define load balancer cluster
BalancerMember http://10.0.0.101:8080
BalancerMember http://10.0.0.102:8080
# Configure proxy pass
ProxyPass /app balancer://backend-cluster
ProxyPassReverse /app balancer://backend-cluster
Include the configuration in the main Apache file and start the service:
vim /usr/local/apache/conf/httpd.conf
Include conf/extra/reverse-proxy.conf
# Create test page
echo 'reverse-proxy' > /usr/local/apache/htdocs/index.html
# Test configuration syntax
/usr/local/apache/bin/apachectl -t
# Start Apache
/usr/local/apache/bin/apachectl -k start
Test the load balancing by accessing:
http://10.0.0.101:8080/app
4.4 Adding Management Interface
Update the configuration to include a management interface:
cat >> /usr/local/apache/conf/extra/reverse-proxy.conf << EOF
SetHandler balancer-manager
Order Deny,Allow
Allow from all
EOF
Restart Apache and access the management interface at:
http://10.0.0.101:8080/balancer-manager
4.5 Virtual Host Configuration
Add a virtual host configuration:
cat >> /usr/local/apache/conf/extra/reverse-proxy.conf << EOF
ServerAdmin admin@company.com
DocumentRoot "/var/www/company"
ServerName www.company.com
ServerAlias company.com
ErrorLog "logs/company-error_log"
CustomLog "logs/company-access_log" common
ProxyPass / balancer://backend-cluster
ProxyPassReverse / balancer://backend-cluster
EOF
Update your local hosts file to test:
10.0.0.101 www.company.com
Access the virtual host at:
http://www.company.com