#########################################################
# haproxy L7 實戰講解
#date:2010-06-09
#做者:老男孩---《老男孩linux就業培訓中心 》
#QQ:31333741 MAIL:oldboy521@hotmail.com
#QQ交流羣:45039636
#blog: http://oldboy.blog.51cto.com
#psite: http://oldboy.cc(即將開放)
#pblog:http://blog.etiantian.org
##########################################################
前言:
haproxy 就很少說了。是值得信賴的優秀的7層轉發軟件。
本人實際應用4000以上萬pv的訪問量級站點,作L7的總體表現和F5/netscaler等硬件負載均衡比,絕不遜色。
固然,這須要一個好的架構體系和比較優的配置優化。
主要體如今可維護性,自主掌控能力,價格,擴展能力,靈活性等N多方面php
本文針對 玩過haproxy的哥們,所以 就不基礎掃盲工做了。望你們諒解,問題能夠進羣和你們交流。
有想了解初級內容的,請關注我年末將要出版的書籍,暫定名 《老男孩的linux運維筆記》css
#######################################################
haproxy 7層負載均衡代理轉發實戰講解(一) 之301跳轉測試
#######################################################html
█ 一、rs web server機測試環境準備:java
●1.1 在/var下分別創建php、nginx、pic三個站點目錄,並增長index.htms文件及增長內容
假定 php nginx pic分別表明解析不一樣的服務。linux
[root@ha2 ~]#for name in php nginx pic ;do mkdir -p /var/$name;echo $name >/var/$name/index.html;done
●1.2 檢查結果
[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 → :picnginx
●1.3 安裝http服務
[root@ha2 ~]# yum install httpd -yweb
●1.4 配置http服務redis
先作配置文件備份
[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服務。
●1.5 http服務器本地增長host內容以下
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內容
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
嚴重提示:
1.這裏解析的IP 爲http server的IP
2.這裏的host至關於模擬DNS的解析
●1.7測試增長http虛擬主機的配置
訪問:http://nginx.etiantian.org 結果應該爲nginx,其它類推。
訪問:http://php.etiantian.org 結果應該爲php,其它類推。
訪問:http://pic.etiantian.org 結果應該爲pic,其它類推。
█ 2 配置haproxy L7負載均衡
●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
●2.2 更改hosts
在咱們的筆記本電腦上
C:\WINDOWS\system32\drivers\etc\hosts增長以下hosts內容
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
嚴重提示:這裏解析的IP 爲haproxy server的IP
●2.3 測試haproxy的轉發應用
確認host文件配置正常後,能夠瀏覽 etiantian.org
看是否能跳轉到nginx.etiantian.org 檢查點:url和內容顯示
提示:個別瀏覽器,有可能看不到URL跳轉,只要是內容顯示正確就對了。
能夠修改配置在測試下:
acl short_dom hdr(Host) -i etiantian.org
redirect prefix http://php.etiantian.org code 301 if short_dom
目的:使訪問http://etiantian.org 跳轉到http://php.etiantian.org
重起haproxy服務後,進行訪問查看。
#########################################################
注意以上 已經應用到正式環境N久 你們可放心使用。
更多7層的應用測試,請關注 http://www.etiantian.org
●2.4 更多7層的 的技術
更多7層的應用測試,請隨時關注 http://www.etiantian.org!
好比 根據後綴進行過濾轉發
acl url_static path_end .gif .png .jpg .css .js
在好比根據目錄進行過濾轉發
acl oldboy_java path_beg /java/
acl static_ryan path_beg /p_w_picpaths/
acl static_ryan path_beg /css/
偶會盡快整理詳細的文檔 近期放出 和你們分享.
謝謝你們瀏覽啊。哈哈!
文章結尾,給你們上傳張圖,誰在用haproxy.