使用Apache的负载均衡模块mod_proxy和mod_proxy_balancer可以实现Apache负载均衡。以下是一个示例配置:
sudo apt-get install apache2
sudo a2enmod proxy
sudo a2enmod proxy_balancer
sudo a2enmod lbmethod_byrequests
sudo systemctl restart apache2
sudo nano /etc/apache2/sites-available/000-default.conf
在VirtualHost标签中添加以下配置:
ProxyPass / balancer://mycluster/
ProxyPassReverse / balancer://mycluster/
BalancerMember http://localhost:8000
BalancerMember http://localhost:8001
# 添加更多的后端服务器,可以是IP地址或域名
# BalancerMember http://backend2.example.com
# BalancerMember http://backend3.example.com
ProxySet lbmethod=byrequests
sudo systemctl restart apache2
现在,Apache服务器将通过轮询方式将请求转发给后端服务器。你可以根据需要添加更多的后端服务器,并调整负载均衡算法。
注意:此配置仅适用于HTTP请求的负载均衡。如果需要负载均衡HTTPS请求,还需要配置SSL证书和相关选项。