Haproxy的安裝和配置

Haproxy的安裝和配置

 

Desktop1:172.25.68.1

Desktop2:172.25.68.2

Desktop3:172.25.68.3

真機:172.25.68.250

在desktop1和desktop2上

 yum install haproxy -y

 cd /etc/haproxy

 

 /etc/init.d/nginx stop    

 /etc/init.d/haproxy start

 編輯vim haproxy.cfg

 

listen westos *:80

        balance roundrobin

        server web1 172.25.68.1:80 check

        server web2 172.25.68.2:80 check

 

 /etc/init.d/haproxy reload

 

desktop3:

  yum install httpd

 編輯 vim /var/www/html/index.html

 Desktop3.example.com

 

  /etc/init.d/httpd start

測試:輸入172.25.68.250:80

 

 

 

 

在desktop1上修改

編輯 vim haproxy.cfg

listen admin 172.25.68.250:8080

        stats enable

        stats uri /status

        stats auth admin:westos

        stats refresh 5s

 

listen westos 172.25.68.250:80

        balance roundrobin

        server web1 172.25.68.1:80 check

        server web2 172.25.68.2:80 check

 

 /etc/init.d/haproxy reload

 

測試: 172.25.68.250:8080/status 任意關閉開啓desktop1和desktop2的http服務 注意觀察顏色變化

 

動靜分離:

desktop1:

編輯vim /etc/haproxy/haproxy.cfg

listen admin 172.25.68.250:8080

        stats enable

        stats uri /status

        stats auth admin:westos

        stats refresh 5s

 

frontend  westos 172.25.68.250:80

         acl url_static       path_beg       -i  /images

         acl url_static       path_end       -i .jpg .gif .png

 

         use_backend static          if url_static

         default_backend             app

 

backend static ##靜態

        balance roundrobin

        server web1 172.25.68.2:80 check

 

backend app ##動態

        balance roundrobin

        server web2 172.25.68.1:80 check

 

 /etc/init.d/haproxy reload

 

Desktop2

cd /var/www/html

mkdir images

cd images

拷貝OSI.gifredhat.jpg到該目錄

 

 

測試:172.25.68.250/images/redhat.jpg 或者 172.25.68.250/images/OSI.gif

 

 

訪問權限的設置

desktop1:

編輯vim /etc/haproxy/haproxy.cfg

listen admin 172.25.68.250:8080

        stats enable

        stats uri /status

        stats auth admin:westos

        stats refresh 5s

 

frontend  westos 172.25.68.250:80

         acl url_static       path_beg       -i  /images

         acl url_static       path_end       -i .jpg .gif .png

 

         acl badhost src 172.25.68.2

         block if badhost

 

   use_backend static          if url_static

         default_backend             app

 

 /etc/init.d/haproxy reload

 

測試: 172.25.68.250 報錯403

 

 

修改默認端口:編輯vim /etc/httpd/conf/httpd.conf

 135 #Listen 12.34.56.78:80

 136 Listen 8000

 

編輯vim /var/www/html

維護中,表鬧........

 

/etc/init.d/httpd start

 

Desktop1:

編輯vim /etc/haproxy/haproxy.cfg

listen admin 172.25.68.250:8080

        stats enable

        stats uri /status

        stats auth admin:westos

        stats refresh 5s

 

frontend  westos 172.25.68.250:80

         acl url_static       path_beg       -i  /images

         acl url_static       path_end       -i .jpg .gif .png

 

         acl badhost src 172.25.68.2

 

#        block if badhost

         errorloc 403 http://172.25.68.2:8000

 

 use_backend static          if url_static

         default_backend             app

 

backend static

        balance roundrobin

        server web1 172.25.68.2:80 check

 

backend app

        balance roundrobin

        server web2 172.25.68.1:80 check

        server local 172.25.68.250:8000 backup

 

 /etc/init.d/haproxy reload

測試:172.25.68.250


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章