在 Ubuntu 上用 Apache 做负载均衡,通常是使用 Apache 的 mod_proxy + mod_proxy_balancer 模块,把请求转发到多台后端服务器(如 Tomcat、Node、其他 Apache、Nginx 等)。
下面给你一个从零开始的常见做法。
sudo apt update
sudo apt install apache2 -y
sudo a2enmod proxy
sudo a2enmod proxy_http
sudo a2enmod proxy_balancer
sudo a2enmod lbmethod_byrequests
sudo a2enmod headers
重启 Apache:
sudo systemctl restart apache2
sudo nano /etc/apache2/sites-available/load-balancer.conf
<VirtualHost *:80>
ServerName example.com
# 负载均衡配置
<Proxy "balancer://mycluster">
BalancerMember "http://192.168.1.10:8080"
BalancerMember "http://192.168.1.11:8080"
# 负载均衡算法
ProxySet lbmethod=byrequests
</Proxy>
ProxyPreserveHost On
ProxyPass "/" "balancer://mycluster/"
ProxyPassReverse "/" "balancer://mycluster/"
# 可选:状态页面
<Location "/balancer-manager">
SetHandler balancer-manager
Require local
</Location>
</VirtualHost>
sudo a2ensite load-balancer.conf
sudo a2dissite 000-default.conf
sudo systemctl restart apache2
| 算法 | 模块 | 说明 |
|---|---|---|
| byrequests | lbmethod_byrequests | 轮询(默认) |
| bytraffic | lbmethod_bytraffic | 按流量 |
| bybusyness | lbmethod_bybusyness | 按忙碌程度 |
示例:
ProxySet lbmethod=bytraffic
BalancerMember "http://192.168.1.10:8080" status=+H
或自动检测:
ProxySet heartbeat=on
如果后端有登录状态:
ProxySet stickysession=JSESSIONID
curl http://localhost
多次访问,应轮流到不同后端。
访问状态页:
http://服务器IP/balancer-manager
使用 Let’s Encrypt:
sudo apt install certbot python3-certbot-apache
sudo certbot --apache
✅ Ubuntu + Apache 负载均衡 =
mod_proxy + mod_proxy_balancer
适合:
如果你愿意,我可以帮你:
你现在的后端是什么?(Tomcat / Node / Nginx / 其他)