目的是將 http://127.0.0.1/demo 負載均衡轉發到 http://127.0.0.1/demo1和http://127.0.0.1/demo2 php
第一步,開啓項目模塊: html
LoadModule proxy_module modules/mod_proxy.so LoadModule proxy_balancer_module modules/mod_proxy_balancer.so LoadModule proxy_http_module modules/mod_proxy_http.so LoadModule lbmethod_byrequests_module modules/mod_lbmethod_byrequests.so LoadModule slotmem_shm_module modules/mod_slotmem_shm.so
第二步,負載均衡代理配置,寫在httpd.conf文件下面: java
可配置一個或者多個負載代理,democluster 爲該代理的命名,後面須要經過引用該名字調用負載均衡轉發ProxyRequests Off <Proxy balancer://democluster> BalancerMember http://127.0.0.1:8080/demo1 BalancerMember http://127.0.0.1:8080/demo2 </Proxy>
開啓負載均衡管理界面 http://127.0.0.1/balancer-manager,生產環境慎重開啓: web
<Location /balancer-manager> SetHandler balancer-manager Require all granted </Location>
(Apache2.4:Require all granted,Apache2.2:Allow from all) 服務器
第三步,應用負載均衡,在虛擬服務器中使用,也能夠直接根配置(httpd.conf)中使用: session
<VirtualHost 127.0.0.1:80> ServerAdmin webmaster@dummy-host.example.com ServerName 127.0.0.1 ProxyRequests Off ProxyPreserveHost On <Proxy *> Require all granted </Proxy> ProxyPass /demo balancer://democluster ProxyPassReverse /demo balancer://democluster ErrorLog "logs/test.localhost-error_log" CustomLog "logs/test.localhost-access_log" common </VirtualHost>
其餘,也能夠將第二步中的負載均衡代理配置,直接配置到虛擬主機中,以下:
負載均衡
<VirtualHost 127.0.0.1:80> ServerAdmin webmaster@dummy-host.example.com ServerName 127.0.0.1 ProxyRequests Off ProxyPreserveHost On <Proxy balancer://democluster> BalancerMember http://127.0.0.1:8080/demo1 BalancerMember http://127.0.0.1:8080/demo2 </Proxy> ProxyPass /demo balancer://democluster ProxyPassReverse /demo balancer://democluster ErrorLog "logs/test.localhost-error_log" CustomLog "logs/test.localhost-access_log" common </VirtualHost>
-------------------------------------------------------------------------------------
相關配置和策略說明 ui
負載權重設置,按照比例設置分發權重,在分發連接後面加上loadfactor: spa
<Proxy balancer://democluster> BalancerMember http://127.0.0.1:8080/demo1 loadfactor=3 BalancerMember http://127.0.0.1:8080/demo2 loadfactor=7 </Proxy>
分發策略設置,經過設置lbmethod設置分發策略:
lbmethod:表示負載載均衡策略。
lbmethod=byrequests :按照請求次數均衡(默認)
lbmethod=bytraffic: 按照流量均衡
lbmethod=bybusyness :按照繁忙程度均衡(老是分配給活躍請求數最少的服務器)
.net
<Proxy balancer://democluster> BalancerMember http://127.0.0.1:8080/demo1 loadfactor=3 BalancerMember http://127.0.0.1:8080/demo2 loadfactor=7 ProxySet lbmethod=byrequests </Proxy>
ProxyPass /demo balancer://democluster lbmethod=bytraffic
<Proxy balancer://democluster> BalancerMember http://127.0.0.1:8080/demo1 BalancerMember http://127.0.0.1:8080/demo2 status=+H </Proxy>