天天看點

Haproxy

Haproxy

代理

Server1 172.25.68.10

Server2 172.25.68.11

Real server

Server3 172.25.68.12

Server4 172.25.68.13

真機

172.25.68.150

Evince ##檢視pdf文檔

[root@server1 haproxy]# vim haproxy.cfg

 63 #frontend  main *:5000

 64 #    acl url_static       path_beg       -i /static /p_w_picpaths /javascript /sty    lesheets

 65 #    acl url_static       path_end       -i .jpg .gif .png .css .js

 66 #

 67 #    use_backend static          if url_static

 68 #    default_backend             app

 69 #

 70 #---------------------------------------------------------------------

 71 # static backend for serving up p_w_picpaths, stylesheets and such

 72 #---------------------------------------------------------------------

 73 #backend static

 74 #    balance     roundrobin

 75 #    server      static 127.0.0.1:4331 check

 76

 77 #---------------------------------------------------------------------

 78 # round robin balancing between the various backends

 79 #---------------------------------------------------------------------

 80 #backend app

 81 #    balance     roundrobin

 82 #    server  app1 127.0.0.1:5001 check

 83 #    server  app2 127.0.0.1:5002 check

 84 #    server  app3 127.0.0.1:5003 check

 85 #    server  app4 127.0.0.1:5004 check

 86

 87 listen westos *:80

 88         balance roundrobin

 89         server web1 172.25.68.12:80 check

 90         server web1 172.25.68.13:80 check

[root@server1 haproxy]# /etc/init.d/haproxy start

Starting haproxy:                                          [  OK  ]

[root@server1 haproxy]# netstat -antlp

Active Internet connections (servers and established)

Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   

tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      1436/haproxy        

tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      905/sshd            

tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      981/master          

tcp        0      0 172.25.68.10:22             172.25.68.250:52284         ESTABLISHED 1031/sshd           

tcp        0      0 :::22                       :::*                        LISTEN      905/sshd            

tcp        0      0 ::1:25                      :::*                        LISTEN      981/master          

[root@server1 haproxy]# /etc/init.d/haproxy reload

Reloading haproxy:

[root@server3 ~]# vim /var/www/html/index.html

Server3.example.com

[root@server3 ~]# /etc/init.d/httpd start

Starting httpd:

[root@server4 ~]# vim /var/www/html/index.html

server4.example.com

[root@server4 ~]# /etc/init.d/httpd start

Starting httpd:                                            [  OK  ]

測試

重新整理會變成下圖

[root@foundation68 Desktop]# curl 172.25.68.10

server3.example.com

[root@server1 haproxy]# vim haproxy.cfg

 87 listen admin *:8080

 88         stats enable

 89         stats uri /status

 90         stats auth admin:westos

 91         stats refresh 5s

 92

[root@server1 haproxy]# vim /etc/rsyslog.conf

 13 $ModLoad imudp

 14 $UDPServerRun 514

 42 *.info;mail.none;authpriv.none;cron.none;local2.none                /var/    log/messages

 62 local2.*                                                /var/log/haproxy.

    log

[root@server1 haproxy]# /etc/init.d/rsyslog restart

Shutting down system logger:                               [  OK  ]

Starting system logger:                                    [  OK  ]

[root@server3 conf.d]# /etc/init.d/httpd stop

Stopping httpd:                                            [  OK  ]

此時重新整理頁面隻能出現server4.example.com

[root@server4 ~]# /etc/init.d/httpd stop

此時重新整理頁面會出現503報錯

 93 frontend westos *:80

 94

 95         default_backend app

 96

 97

 98 backend app

 99         balance roundrobin

100         server web1 172.25.68.12:80 check

101         server web1 172.25.68.13:80 check

 98 backend static

101 backend app

102         balance roundrobin

103         server web2 172.25.68.13:80 check

[root@server3 p_w_picpaths]# ls

centos logo.jpg  OSI.gif

[root@server3 p_w_picpaths]# pwd

/var/www/html/p_w_picpaths

 94         acl url_static       path_beg       -i /p_w_picpaths

 95         acl url_static       path_end       -i .jpg .gif .png

 98         use_backend static          if url_static

測試:

 98         acl badhost src 172.25.68.250

 99         block if badhost

[root@foundation68 haproxy.安裝包]# curl 172.25.68.10

<html><body><h1>403 Forbidden</h1>

Request forbidden by administrative rules.

</body></html>

[root@foundation68 haproxy.安裝包]# curl -I 172.25.68.10

HTTP/1.0 403 Forbidden

Cache-Control: no-cache

Connection: close

Content-Type: text/html

[root@server1 haproxy]# vim /etc/httpd/conf/httpd.conf

 135 #Listen 12.34.56.78:80

 136 Listen 8000

[root@server1 haproxy]# vim /var/www/html/index.html

bienaole,fangwenbuliao.........

~          

100         errorloc 403 http://172.25.68.10:8000

 99 #       block if badhost

100 #       errorloc 403 http://172.25.68.10:8000

101

102

103         use_backend static          if url_static

104         default_backend app

105

106

107 backend static

108         balance roundrobin

109         server web1 172.25.68.12:80 check

110 backend app

111         balance roundrobin

112         server web2 172.25.68.13:80 check

113         server local 172.25.68.10:8000 backup

Stopping httpd:                                            [  OK  

102         redirect location http://172.25.68.10:8000 if badhost

HTTP/1.1 302 Found

Content-length: 0

Location: http://172.25.68.10:8000

100         acl westos.org hdr_beg(host) -i westos.org

101 #       block if badhost

102 #       errorloc 403 http://172.25.68.10:8000

103

104         #redirect code 301 location http://172.25.68.10:8000 if badhost

105         redirect code 301 location http://www.westos.org if westos.org

107         use_backend static          if url_static

108         default_backend app

109

110

100         acl 172.25.68.10 hdr(host) -i 172.25.68.10

101         acl westos.org hdr_beg(host) -i westos.org

102 #       block if badhost

103 #       errorloc 403 http://172.25.68.10:8000

104

105         #redirect code 301 location http://172.25.68.10:8000 if badhost

106         redirect code 301 location http://www.westos.org if westos.org

107         redirect code 301 location http://www.westos.org if 172.25.68.10

108

在浏覽器裡輸入172.25.68.10會自動跳轉到www.westos.org

在浏覽器輸入westos.org會自動補全為www.westos.org

繼續閱讀