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@foundation68 Desktop]# curl 172.25.68.10

server4.example.com

[root@foundation68 Desktop]# curl 172.25.68.10

server3.example.com

[root@foundation68 Desktop]# curl 172.25.68.10

server4.example.com

[root@foundation68 Desktop]# curl 172.25.68.10

server3.example.com

[root@foundation68 Desktop]# curl 172.25.68.10

server4.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]# /etc/init.d/haproxy reload

Reloading haproxy:

 

[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

Stopping httpd:                                            [  OK  ]

 

此时刷新页面会出现503报错

[root@server1 haproxy]# vim haproxy.cfg

 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

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

Reloading haproxy:

 

[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

 93 frontend westos *:80

 94

 95         default_backend app

 96

 97

 98 backend static

 99         balance roundrobin

100         server web1 172.25.68.12:80 check

101 backend app

102         balance roundrobin

103         server web2 172.25.68.13:80 check

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

Reloading haproxy:

 

 

[root@server3 p_w_picpaths]# ls

centos logo.jpg  OSI.gif

[root@server3 p_w_picpaths]# pwd

/var/www/html/p_w_picpaths

 

[root@server1 haproxy]# vim haproxy.cfg

 

 93 frontend westos *:80

 94         acl url_static       path_beg       -i /p_w_picpaths

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

 96

 97

 98         use_backend static          if url_static

 

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

Reloading haproxy:

 

测试:

 

 

 

[root@server1 haproxy]# vim haproxy.cfg

 98         acl badhost src 172.25.68.250

 99         block if badhost

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

Reloading haproxy:

测试:

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

403 Forbidden

Request forbidden by administrative rules.

 

[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.........

~          

[root@server1 haproxy]# vim haproxy.cfg

100         errorloc 403 http://172.25.68.10:8000

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

Reloading haproxy:

测试:

 

 

[root@server1 haproxy]# vim haproxy.cfg

 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

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

Reloading haproxy:

 

测试:

 

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

Stopping httpd:                                            [  OK  

 

 

[root@server1 haproxy]# vim haproxy.cfg

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

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

Reloading haproxy:

测试:

 

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

HTTP/1.1 302 Found

Cache-Control: no-cache

Content-length: 0

Location: http://172.25.68.10:8000

Connection: close

 

 

[root@server1 haproxy]# vim haproxy.cfg

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

 

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

Reloading haproxy:

 

[root@server1 haproxy]# vim haproxy.cfg

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

106

107         use_backend static          if url_static

108         default_backend app

109

110

 

[root@server1 haproxy]# vim haproxy.cfg

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

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

Reloading haproxy:

 

测试:

在浏览器里输入172.25.68.10会自动跳转到www.westos.org

在浏览器输入westos.org会自动补全为www.westos.org