haproxy 7層負(fù)載均衡代理轉(zhuǎn)發(fā)實(shí)戰(zhàn)講解
假定 php nginx pic分別代表解析不同的服務(wù)。
[root@ha2 ~]#for name in php nginx pic ;do mkdir -p /var/$name;echo $name >/var/$name/index.html;done
●1.2 檢查結(jié)果
[root@ha2 ~]# for name in php nginx pic ;do echo -n "/var/$name/index.html → :";cat /var/$name/index.html; done
/var/php/index.html → :php
/var/nginx/index.html → :nginx
/var/pic/index.html → :pic
[root@ha2 ~]# yum install httpd -y ●1.4 配置http服務(wù)
先做配置文件備份
[root@ha2 conf]# cd /etc/httpd/conf
[root@ha2 conf]# cp httpd.conf httpd.conf.oldboy.110625
[root@ha2 conf]# ls -l
total 88
-rw-r--r-- 1 root root 34399 Jun 26 16:40 httpd.conf
-rw-r--r-- 1 root root 34399 Jun 26 16:49 httpd.conf.oldboy.110625
-rw-r--r-- 1 root root 13139 May 4 18:54 magic
編輯httpd.conf 最下面加
<Directory "/var">
Options FollowSymLinks
AllowOverride none
Order allow,deny
Allow from all
</Directory>
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin 49000448@qq.com
ServerName nginx.etiantian.org
ServerAlias etiantian.org
DocumentRoot "/var/nginx"
</VirtualHost>
<VirtualHost *:80>
ServerAdmin 49000448@qq.com
ServerName php.etiantian.org
DocumentRoot "/var/php"
</VirtualHost>
<VirtualHost *:80>
ServerAdmin 49000448@qq.com
ServerName pic.etiantian.org
DocumentRoot "/var/pic"
</VirtualHost>
提示:配置完成記得重起http服務(wù)。
●1.5 http服務(wù)器本地增加host內(nèi)容如下echo '10.0.0.162 nginx.etiantian.org' >>/etc/hosts
echo '10.0.0.162 php.etiantian.org' >>/etc/hosts
echo '10.0.0.162 pic.etiantian.org' >>/etc/hosts
echo '10.0.0.162 etiantian.org' >>/etc/hosts ●1.6 在我們的筆記本電腦上
C:\WINDOWS\system32\drivers\etc\hosts增加如下hosts內(nèi)容
10.0.0.162 nginx.etiantian.org
10.0.0.162 php.etiantian.org
10.0.0.162 pic.etiantian.org
10.0.0.162 etiantian.org
嚴(yán)重提示:
1.這里解析的IP 為http server的IP
2.這里的host相當(dāng)于模擬DNS的解析
訪問:http://nginx.etiantian.org 結(jié)果應(yīng)該為nginx,其它類推。
訪問:http://php.etiantian.org 結(jié)果應(yīng)該為php,其它類推。
訪問:http://pic.etiantian.org 結(jié)果應(yīng)該為pic,其它類推。
█ 2 配置haproxy L7負(fù)載均衡
●2.1 haproxy.conf配置
#______________________________________________________________________
defaults
log global
mode http
retries 3
option redispatch
contimeout 5000
clitimeout 50000
srvtimeout 50000
stats enable
stats hide-version
stats uri /admin?stats
stats auth proxy:123456
option httpclose
在我們的筆記本電腦上
C:\WINDOWS\system32\drivers\etc\hosts增加如下hosts內(nèi)容
10.0.0.162 nginx.etiantian.org
10.0.0.162 php.etiantian.org
10.0.0.162 pic.etiantian.org
10.0.0.162 etiantian.org
嚴(yán)重提示:這里解析的IP 為haproxy server的IP
確認(rèn)host文件配置正常后,可以瀏覽 etiantian.org
看是否能跳轉(zhuǎn)到nginx.etiantian.org 檢查點(diǎn):url和內(nèi)容顯示
提示:個別瀏覽器,有可能看不到URL跳轉(zhuǎn),只要是內(nèi)容顯示正確就對了。
可以修改配置在測試下:
acl short_dom hdr(Host) -i etiantian.org
redirect prefix http://php.etiantian.org code 301 if short_dom
目的:使訪問http://etiantian.org 跳轉(zhuǎn)到http://php.etiantian.org
重起haproxy服務(wù)后,進(jìn)行訪問查看。
#########################################################
注意以上 已經(jīng)應(yīng)用到正式環(huán)境N久 大家可放心使用。
比如 根據(jù)后綴進(jìn)行過濾轉(zhuǎn)發(fā)
acl url_static path_end .gif .png .jpg .css .js
在比如根據(jù)目錄進(jìn)行過濾轉(zhuǎn)發(fā)
acl oldboy_java path_beg /java/
acl static_ryan path_beg /images/
acl static_ryan path_beg /css/
配置haproxy L7負(fù)載均衡-之根據(jù)URL地址目錄轉(zhuǎn)發(fā)
準(zhǔn)備RS SERVER測試環(huán)境
建立測試目錄:
for name in php nginx pic ;do mkdir -p /var/$name/$name;echo $name >/var/$name/$name/index.html;done
配置http服務(wù)
刪除上面做的任何apache配置(或者還原httpd.conf)
[root@ha2 conf]# cd /etc/httpd/conf
[root@ha2 conf]# /bin/cp httpd.conf.oldboy.110625 httpd.conf
然后編輯httpd.conf 最下面加
<Directory "/var">
Options FollowSymLinks
AllowOverride none
Order allow,deny
Allow from all
</Directory>
Listen 8080
Listen 8090
NameVirtualHost *:80
NameVirtualHost *:8080
NameVirtualHost *:8090
<VirtualHost *:80>
ServerAdmin 49000448@qq.com
ServerName www.etiantian.org
ServerAlias etiantian.org
DocumentRoot "/var/nginx"
</VirtualHost>
<VirtualHost *:8080>
ServerAdmin 49000448@qq.com
ServerName www.etiantian.org
DocumentRoot "/var/php"
</VirtualHost>
<VirtualHost *:8090>
ServerAdmin 49000448@qq.com
ServerName www.etiantian.org
DocumentRoot "/var/pic"
</VirtualHost>
● 3.2 基于目錄轉(zhuǎn)發(fā)的haproxy配置
frontend oldboy_test
bind *:80
acl short_domain hdr(Host) -i etiantian.org
redirect prefix http://www.etiantian.org code 301 if short_domain
#acl lang_domain hdr(Host) -i www.etiantian.org
#use_backend staticpools if lang_domain
default_backend staticpools
#http://www.etiantian.org/nginx/...,提供靜態(tài)內(nèi)容訪問(htm,html,css,js)
backend staticpools
balance roundrobin
server oldboy-1 10.0.0.151:80 check port 80 inter 3000 fall 3
server oldboy-2 10.0.0.151:81 check port 81 inter 3000 fall 3
server oldboy-3 10.0.0.151:82 check port 82 inter 3000 fall 3
#提示:后面兩個rs是虛構(gòu)的,只是告訴大家可以這樣加server.有沒有都不會影響訪問。以下同。
#http://www.etiantian.org/php/... 提供動態(tài)內(nèi)容訪問(以*.php結(jié)尾)
backend dynamicpools
balance roundrobin
cookie SERVERID insert indirect
server oldboy-4 10.0.0.151:8080 maxconn 2048 weight 50 cookie A check port 8080 inter 3000 fall 3
server oldboy-5 10.0.0.151:8180 maxconn 2048 weight 100 cookie B check port 8180 inter 3000 fall 3
server oldboy-6 10.0.0.151:8280 maxconn 2048 weight 100 cookie C check port 8280 inter 3000 fall 3
#列出更詳細(xì)的參數(shù),供大家參考使用。
#http://www.etiantian.org/pic/... 提供圖片內(nèi)容訪問(以*.jpg,*.png,*.gif等結(jié)尾)
backend picpools
balance roundrobin
server oldboy-7 10.0.0.151:8090 check port 8090 inter 3000 fall 3
server oldboy-8 10.0.0.151:8190 check port 8190 inter 3000 fall 3
server oldboy-9 10.0.0.151:8290 check port 8290 inter 3000 fall 3
#提示:server后面的名稱是自己定義的,名稱不要重復(fù)就好。
當(dāng)多臺服務(wù)器時,每個池子可配置成如下形式:
cookie SERVERID insert indirect
server oldboy-1 10.0.0.163:8080 cookie ett-1-1 check port 8080 inter 3000 fall 3
配置基于URL的健康檢查
option httpchk HEAD /checkstatus.jsp HTTP/1.0
配置用戶端hosts
10.0.0.162 www.etiantian.org
提示:10.0.0.162 為haproxy的IP,這里是模擬DNS的解析,實(shí)際配置時,在DNS里解析即可
●3.4 在用戶端進(jìn)行訪問測試
3.4.1 查看hosts解析是否正確
C:\Documents and Settings\hyran>ping www.etiantian.org
Pinging www.etiantian.org [10.0.0.162] with 32 bytes of data:
Reply from 10.0.0.162: bytes=32 time=5ms TTL=64
Reply from 10.0.0.162: bytes=32 time<1ms TTL=64
Reply from 10.0.0.162: bytes=32 time<1ms TTL=64
3.4.2 測試URL地址 看是否符合預(yù)期要求
分別訪問:
http://www.etiantian.org/nginx/
http://www.etiantian.org/php/
http://www.etiantian.org/pic/
如果能返回對應(yīng)自己目錄的內(nèi)容,那恭喜你,搞定了。