HAProxy

Work at layer 7

Nginx  / Apache proxy: http, fastcgi
Nginx: no heartbeat check

HAProxy: http, mysql, service depending on port; Heartbeat check

Env: three servers
1 HAProxy + 2 real server

HAProxy: 192.168.199.183
Install Package: yum install haproxy
config file: /etc/haproxy/haproxy.cfg
vim /etc/haproxy/haproxy.cfg
A. frontend --> nginx server
B. backend --> nginx upstream 

acl: define static and dynamic
use_backend <backend name> if <acl rule>

Example:
acl url_static path_beg -i /static /images /javascript /stylesheets
acl url_static path_end -i .jpg .gif .png .css .js
use_backend static if url_static
backend static
    balance roundrobin
    server   server 127.0.0.1:4331 check

Test1 state page (static apache service) without user authentication
HAProxy server
#vim /etc/haproxy/haproxy.cfg
    frontend main *:80
#    acl url_static    path_beg    -i /static /images /javascript /stylesheets
#    acl url_static    path_end    -i .jpg .gif .png .css .js

#    use_backend static    if url_static
      defatult_backend    apache

#backend static
#    balance    roundrobin
#    server      static 127.0.0.1:4331 check

backend apache
    balance   roundrobin
    server     apache1 192.168.199.187:80 check
    server     apache2 192.168.199.179:80 check
    stats       enable
    stats       uri /haproxyadmin?status

#service haproxy start

config two apache server with httpd service and index.html page

Test: visit http:\\192.168.199.183\haproxyadmin?status

Test2 state page (static apache service) with user authentication
HAProxy server
#vim /etc/haproxy/haproxy.cfg
    frontend main *:80
#    acl url_static    path_beg    -i /static /images /javascript /stylesheets
#    acl url_static    path_end    -i .jpg .gif .png .css .js

#    use_backend static    if url_static
      defatult_backend    apache

#backend static
#    balance    roundrobin
#    server      static 127.0.0.1:4331 check

backend apache
    balance   roundrobin
    server     apache1 192.168.199.187:80 check
    server     apache2 192.168.199.179:80 check
    stats       enable
    stats       uri /haproxyadmin?status
    stats       realm HAProxy\ Stats
    stats       auth admin:admin(user authentication)
    stats admin if TRUE (start the manage tools)
    acl statsrc src 192.168.199.244 (ip restriction)
    http-request allow if statsrc 
    http-request deny

#service haproxy restart

config two apache server with httpd service and index.html page

Test: visit http:\\192.168.199.183\haproxyadmin?status

Test3 session bind restrict to specific backend server
HAProxy server
#vim /etc/haproxy/haproxy.cfg
    frontend main *:80
#    acl url_static    path_beg    -i /static /images /javascript /stylesheets
#    acl url_static    path_end    -i .jpg .gif .png .css .js

#    use_backend static    if url_static
      defatult_backend    apache

#backend static
#    balance    roundrobin
#    server      static 127.0.0.1:4331 check

backend apache
    balance   source
    server     apache1 192.168.199.187:80 check
    server     apache2 192.168.199.179:80 check
    stats       enable
    stats       uri /haproxyadmin?status

#service haproxy start

config two apache server with httpd service and index.html page

Test: visit http:\\192.168.199.183\haproxyadmin?status

Test4 cookie information restrict to specific backend server
HAProxy server
#vim /etc/haproxy/haproxy.cfg
    frontend main *:80
#    acl url_static    path_beg    -i /static /images /javascript /stylesheets
#    acl url_static    path_end    -i .jpg .gif .png .css .js

#    use_backend static    if url_static
      defatult_backend    apache

#backend static
#    balance    roundrobin
#    server      static 127.0.0.1:4331 check

backend apache
    balance   roundrobin
    cookie     apache insert indirect nocache
    server     apache1 192.168.199.187:80 cookie apache1 check
    server     apache2 192.168.199.179:80 cookie apache2 check
    stats       enable
    stats       uri /haproxyadmin?status

#service haproxy start

config two apache server with httpd service and index.html page

Test: visit http:\\192.168.199.183\haproxyadmin?status

Test5 url link restrict to specific backend server
HAProxy server
#vim /etc/haproxy/haproxy.cfg
      frontend main *:80
#    acl url_static    path_beg    -i /static /images /javascript /stylesheets
#    acl url_static    path_end    -i .jpg .gif .png .css .js

#    use_backend static    if url_static
      defatult_backend    apache

#backend static
#    balance    roundrobin
#    server      static 127.0.0.1:4331 check

backend apache
    balance   uri
    server     apache1 192.168.199.187:80 check
    server     apache2 192.168.199.179:80 check
    stats       enable
    stats       uri /haproxyadmin?status

#service haproxy start

config two apache server with httpd service and index.html page

Test: visit http:\\192.168.199.183\haproxyadmin?status

Test6 static and dynamic separation 
Method 1 write static items
backend server: 1 static (httpd) + 1 dynamic (LAMP)

HAProxy server
#vim /etc/haproxy/haproxy.cfg
    frontend main *:80
    acl url_static    path_end    -i .jpg .gif .png .css .js .html

    use_backend static if url_static
    defatult_backend   dynamic

backend static
    balance    roundrobin
    server      static1 192.168.199.187:80 check

backend dynamic
    balance   roundrobin
    server     dynamic1 192.168.199.179:80 check

#service haproxy start
Test: visit http:\\192.168.199.183\index.php
Test: visit http:\\192.168.199.183\index.html

Method 2 write dynamic items
backend server: 1 static (httpd) + 1 dynamic (LAMP)

HAProxy server
#vim /etc/haproxy/haproxy.cfg
    frontend main *:80
    acl url_dynamic  path_end    -i .php

    use_backend dynamic if url_dynamic
    defatult_backend   static

backend static
    balance    roundrobin
    server      static1 192.168.199.187:80 check

backenddynamic
    balance   roundrobin
    server     dynamic1 192.168.199.179:80 check

#service haproxy start
Test: visit http:\\192.168.199.183\index.php
Test: visit http:\\192.168.199.183\index.html

Comments

Popular posts from this blog

Nginx Proxy & Load Balance & LNMP

Snort+barnyard2+Snorby CentOS 6.5_64 Installation

ORACLE Error