首页  >  文章  >  后端开发  >  Apache 虚拟主机:负载均衡器

Apache 虚拟主机:负载均衡器

Susan Sarandon
Susan Sarandon原创
2024-10-11 10:10:30604浏览

Apache Virtual Host: Load Balancer

负载均衡是跨多个后端服务器分发请求、提高系统可扩展性和可用性的绝佳策略。可以使用 mod_proxy_balancer 模块将 Apache 配置为负载均衡器

这是在 Apache 中实现负载平衡的完整指南:

启用所需的模块

首先,在 Apache 中启用所需的模块:

sudo a2enmod proxy
sudo a2enmod proxy_http
sudo a2enmod proxy_balancer
sudo a2enmod lbmethod_byrequests

重新启动 Apache 以加载模块:

sudo systemctl restart apache2

配置虚拟主机负载平衡

现在,编辑虚拟主机的配置文件以添加负载平衡指令。

打开配置文件:

sudo your_editor /etc/apache2/sites-available/php.conf

添加以下代码块以在多个后端服务器之间配置负载平衡

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName php.info

    # Load balancer configuration
    <Proxy "balancer://meucluster">
        BalancerMember http://localhost:8080
        BalancerMember http://localhost:8081
        BalancerMember http://localhost:8082
        ProxySet lbmethod=byrequests
    </Proxy>

    ProxyPreserveHost On
    ProxyPass / balancer://meucluster/
    ProxyPassReverse / balancer://meucluster/

    <Directory /var/www/html/php/>
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/php_error_http.log
    CustomLog ${APACHE_LOG_DIR}/php_access_http.log combined
</VirtualHost>

以上元素的解释:

  • BalancerMember:定义后端服务器。在本例中,我们配置了三台服务器侦听端口 8080、8081 和 8082。您可以将这些值替换为您的实际服务器。
  • lbmethod=byrequests:定义平衡方法。 byrequests 在服务器之间平均分配请求。其他方法包括:
    • bytraffic:根据流量进行分配。
    • bybusyness:根据活跃连接数进行分配。
    • heartbeat:使用先进的健康监测方法。

添加后端服务器

在上面的示例中,我假设您在 localhost 上的端口 8080、8081 和 8082 上运行了三个后端服务。请确保这些服务正在运行。

否则,您可以使用正确的端口配置后端服务器或使用 Docker 容器来模拟多个服务。

为 HTTPS 启用 SSL 模块

如果您想通过 HTTPS 进行负载平衡,还可以添加 SSL 虚拟主机 (/etc/apache2/sites-available/php-le-ssl.conf) 以跨 HTTPS 后端服务器进行负载平衡:

<IfModule mod_ssl.c>
    <VirtualHost *:443>
        ServerAdmin webmaster@localhost
        ServerName php.info
        DocumentRoot /var/www/meu_projeto

        # Configuração do Balanceador de Carga
        <Proxy "balancer://meucluster">
            BalancerMember http://localhost:8080
            BalancerMember http://localhost:8081
            BalancerMember http://localhost:8082
            ProxySet lbmethod=byrequests
        </Proxy>

        ProxyPreserveHost On
        ProxyPass / balancer://meucluster/
        ProxyPassReverse / balancer://meucluster/

        SSLEngine on
        SSLCertificateFile /etc/letsencrypt/live/php.info/fullchain.pem
        SSLCertificateKeyFile /etc/letsencrypt/live/php.info/privkey.pem
        Include /etc/letsencrypt/options-ssl-apache.conf

        ErrorLog ${APACHE_LOG_DIR}/php_error_https.log
        CustomLog ${APACHE_LOG_DIR}/php_access_https.log combined
    </VirtualHost>
</IfModule>

高级配置选项

设置服务器权重

您可以为服务器设置不同的权重,这意味着某些服务器比其他服务器接收更多的流量。示例:

BalancerMember http://localhost:8080 loadfactor=1
BalancerMember http://localhost:8081 loadfactor=2
BalancerMember http://localhost:8082 loadfactor=1

在这种情况下,位于 localhost:8081 的服务器将收到两倍于其他服务器的请求。

设置失败超时和重试

您可以设置超时和重试来检测后端服务器上的故障:

<Proxy "balancer://mycluster">
BalancerMember http://localhost:8080 retry=5 timeout=10
BalancerMember http://localhost:8081 retry=5 timeout=10
BalancerMember http://localhost:8082 retry=5 timeout=10
ProxySet lbmethod=byrequests
</Proxy>

监控和管理负载均衡器

要监控负载均衡器运行状况并动态管理活动/非活动成员,请启动 Balancer Manager 界面:

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

您现在可以访问http://php.info/balancer-manager来查看负载均衡器的健康状况并实时调整设置。

重新启动 Apache

进行配置更改后,重新启动 Apache 以使更改生效:

sudo systemctl restart apache2

测试负载平衡

现在,当您访问 http://php.info 时,Apache 会在定义的后端服务器之间分发请求。

实施健康检查(可选)

您可以配置 Apache 以检查后端服务器的运行状况,并在后端服务器出现故障时自动将其从池中删除。为此,您可以使用 mod_proxy_hcheck 模块。

首先,启用模块:

sudo a2enmod proxy_hcheck
sudo systemctl restart apache2

然后,将以下配置添加到您的 中区块:

<Proxy "balancer://meucluster">
    BalancerMember http://localhost:8080 hcheck=on hcmethod=HEAD
    BalancerMember http://localhost:8081 hcheck=on hcmethod=HEAD
    BalancerMember http://localhost:8082 hcheck=on hcmethod=HEAD
    ProxySet lbmethod=byrequests
</Proxy>

Apache 现在将自动检查后端服务器,如果出现故障,请将它们从池中删除。

结论

将 Apache 配置为负载均衡器,您可以在多个后端服务器之间分配流量,确保可扩展性和冗余。使用 SSL 和额外的运行状况检查有助于保持环境的安全和稳健。

以上是Apache 虚拟主机:负载均衡器的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn