haproxy 7層負載平衡代理轉寄實戰講解

來源:互聯網
上載者:User

█ 3 配置haproxy L7負載平衡-之根據URL地址目錄轉寄

●3.1 準備RS SERVER測試環境

建立測試目錄:
for name in php nginx pic ;do mkdir -p /var/$name/$name;echo $name >/var/$name/$name/index.html;done

配置http服務
刪除上面做的任何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 基於目錄轉寄的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/...,提供靜態內容訪問(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是虛構的,只是告訴大家可以這樣加server.有沒有都不會影響訪問。以下同。

#http://www.etiantian.org/php/... 提供動態內容訪問(以*.php結尾)
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
        #列出更詳細的參數,供大家參考使用。
#http://www.etiantian.org/pic/... 提供圖片內容訪問(以*.jpg,*.png,*.gif等結尾)
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後面的名稱是自己定義的,名稱不要重複就好。

當多台伺服器時,每個池子可配置成如下形式:
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

●3.3 配置使用者端hosts
10.0.0.162 www.etiantian.org
提示:10.0.0.162 為haproxy的IP,這裡是類比DNS的解析,實際配置時,在DNS裡解析即可


●3.4 在使用者端進行訪問測試
  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地址 看是否符合預期要求
分別訪問:
http://www.etiantian.org/nginx/
http://www.etiantian.org/php/
http://www.etiantian.org/pic/
如果能返回對應自己目錄的內容,那恭喜你,搞定了。

 

下面還有一個配置代理的例子

global
        maxconn 12345
        nbproc  2

 

frontend myproxy_1000
        mode    http
        bind *:10000

        monitor-uri /haproxy

        acl baiduduty url_beg  /duty
        acl baiduhost hdr_beg(host) -i www.baidu.com
        acl itrushost hdr_beg(host) -i www.itrus.com.cn

        use_backend baidu if baiduhost
        use_backend itrus if itrushost

#       default_backend google

backend baidu
        mode    http
        server  server2 www.baidu.com:80

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.