搜尋
首頁後端開發php教程Apache 虛擬主機:負載平衡器

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>
    ServerAdmin webmaster@localhost
    ServerName php.info

    # Load balancer configuration
    <proxy>
        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></directory>
        AllowOverride All
        Require all granted
    

    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>
        ServerAdmin webmaster@localhost
        ServerName php.info
        DocumentRoot /var/www/meu_projeto

        # Configuração do Balanceador de Carga
        <proxy>
            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>
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>
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>
    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
如何使PHP應用程序更快如何使PHP應用程序更快May 12, 2025 am 12:12 AM

tomakephpapplicationsfaster,關注台詞:1)useopcodeCachingLikeLikeLikeLikeLikePachetoStorePreciledScompiledScriptbyTecode.2)MinimimiedAtabaseSqueriSegrieSqueriSegeriSybysequeryCachingandeffeftExting.3)Leveragephp7 leveragephp7 leveragephp7 leveragephpphp7功能forbettercodeefficy.4)

PHP性能優化清單:立即提高速度PHP性能優化清單:立即提高速度May 12, 2025 am 12:07 AM

到ImprovephPapplicationspeed,關注台詞:1)啟用opcodeCachingwithapCutoredUcescriptexecutiontime.2)實現databasequerycachingingusingpdotominiminimizedatabasehits.3)usehttp/2tomultiplexrequlexrequestsandreduceconnection.4 limitesclection.4.4

PHP依賴注入:提高代碼可檢驗性PHP依賴注入:提高代碼可檢驗性May 12, 2025 am 12:03 AM

依赖注入(DI)通过显式传递依赖关系,显著提升了PHP代码的可测试性。1)DI解耦类与具体实现,使测试和维护更灵活。2)三种类型中,构造函数注入明确表达依赖,保持状态一致。3)使用DI容器管理复杂依赖,提升代码质量和开发效率。

PHP性能優化:數據庫查詢優化PHP性能優化:數據庫查詢優化May 12, 2025 am 12:02 AM

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

簡單指南:帶有PHP腳本的電子郵件發送簡單指南:帶有PHP腳本的電子郵件發送May 12, 2025 am 12:02 AM

phpisusedforsenderemailsduetoitsbuilt-inmail()函數andsupportivelibrariesLikePhpMailerAndSwiftMailer.1)usethemail()functionForbasiceMails,butithasimails.2)butithasimail.2)

PHP性能:識別和修復瓶頸PHP性能:識別和修復瓶頸May 11, 2025 am 12:13 AM

PHP性能瓶颈可以通过以下步骤解决:1)使用Xdebug或Blackfire进行性能分析,找出问题所在;2)优化数据库查询并使用缓存,如APCu;3)使用array_filter等高效函数优化数组操作;4)配置OPcache进行字节码缓存;5)优化前端,如减少HTTP请求和优化图片;6)持续监控和优化性能。通过这些方法,可以显著提升PHP应用的性能。

PHP的依賴注入:快速摘要PHP的依賴注入:快速摘要May 11, 2025 am 12:09 AM

依賴性注射(DI)InphpisadesignPatternthatManages和ReducesClassDeptions,增強量強制性,可驗證性和MATIALWINABIOS.ItallowSpasspassingDepentenciesLikEdenciesLikedAbaseConnectionStoclasseconnectionStoclasseSasasasasareTers,interitationAseTestingEaseTestingEaseTestingEaseTestingEasingAndScalability。

提高PHP性能:緩存策略和技術提高PHP性能:緩存策略和技術May 11, 2025 am 12:08 AM

cachingimprovesphpermenceByStorcyResultSofComputationsorqucrouctationsorquctationsorquickretrieval,reducingServerLoadAndenHancingResponsetimes.feftectivestrategiesinclude:1)opcodecaching,whereStoresCompiledSinmememorytssinmemorytoskipcompliation; 2)datacaching datacachingsingMemccachingmcachingmcachings

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中