如何通过haproxy配置多个独立站点

通过之前的文章《Centos系统环境下进行负载均衡软件Haproxy安装及配置》(http://www.21yunwei.com/archives/5111) 记录下如何安装haproxy,当时测试的比较简单,不过还是里边配置了几个站点,这里单独拿出来进行一下站点配置说明。

配置目的:对三个站点lvs.21yunwei.com、21yunwei.21yunwei.com、bbs.21yunwei.com进行单独设置web后端节点以及设置节点的权重,实现访问每个域名,都访问具体自己设置的站点节点以及自己的网站内容。没有配置站点ID的,则访问设置好的默认站点。

1、配置haproxy

说白了,就是针对haproxy进行配置后重启。根据配置目的,haproxy配置文件如下:

global                                                     
    log         127.0.0.1 local2                     
    chroot      /usr/share/haproxy
    pidfile     /var/run/haproxy.pid         
    maxconn     4000                                
    user        haproxy
    group       haproxy                        
    daemon                                               
defaults
    mode                    http                      
    log                        global   
    option                  httplog      

    option                  dontlognull          
    option http-server-close                  
    option forwardfor       except 127.0.0.0/8    
    option                  redispatch        
    retries                 3                             
    timeout http-request    10s             
    timeout queue           1m                
    timeout connect         10s                
    timeout client          1m                   
    timeout server          1m                   
    timeout http-keep-alive 10s           
    timeout check           10s                 
    maxconn                 3000                
frontend  http_default_80
    bind 0.0.0.0:80
    mode http
    log  global
    option  httplog
    option  httpclose
    option  forwardfor
    acl 21yunwei_site  hdr(host) -i 21yunwei.21yunwei.com
    acl lvs_site hdr(host) -i   lvs.21yunwei.com
    acl bbs_site  hdr(host) -i bbs.21yunwei.com
    use_backend   web_21yunwei  if 21yunwei_site
    use_backend   web_lvs   if lvs_site
    use_backend   web_bbs   if bbs_site
    default_backend             web_21yunwei
frontend www
   bind :81
   monitor-uri   /haproxy 
backend static                                                
    balance     roundrobin                             
    server      static 127.0.0.1:80 check            
backend web_21yunwei          
    mode    http                       
    balance roundrobin                              
    cookie SERVERID 
    option httpchk GET /index.html
    server  21web1 192.168.1.123:80  check inter 2000 fall 3 weight 1             
    server  21web2 192.168.1.122:80  check inter 2000 fall 3 weight 1
backend web_lvs
    mode    http
    balance roundrobin    
    cookie SERVERID
    option httpchk GET /index.html
    server  lvsweb1 192.168.1.123:80  check inter 2000 fall 3 weight 1 
    server  lvsweb2 192.168.1.122:80  check inter 2000 fall 3 weight 3  
backend web_bbs
    mode    http
    balance roundrobin
    cookie SERVERID
    option httpchk GET /ok.php
    server  21web1 192.168.1.123:80  check inter 2000 fall 3 weight 1
    server  21web2 192.168.1.122:80  check inter 2000 fall 3 weight 1

listen  admin_stats 0.0.0.0:88
        option httplog
         stats refresh 30s
         stats uri /admin
         stats realm Haproxy Manager
         stats auth admin:admin

大如上边配置文件,我们配置了一个的frontend 指定前端访问虚拟节点和三个backend后端集群。
说明:

  • frontend里边包含了网站的端口、模式、日志以及acl规则,这里主要是acl规则,访问每个域名都指定访问哪个后端集群,不在acl访问控制规则里边的则访问到默认站点。
  • backend主要是配置对应的后端集群,包含模式、指定算法、健康检测url以及对应后端的真实server节点,节点包含具体IP以及端口、检测以及权重。

  • 补充内容:haproxy作为一款负载均衡软件,本身还是支持动静分离的,可以通过acl规则设置动静分离,分别走独立配置的backend后端集群。比如这样配置可以设置动静分离(本博客这里没这样设置,有实际需要的时候可以针对设置):

   acl url_static       path_beg       -i /static /images /javascript /stylesheets
   acl url_static       path_end       -i .jpg .jpeg .gif .png .css .js .html
   acl url_php          path_end       -i .php

   use_backend static_servers          if url_static
   use_backend dynamic_servers         if url_php
   default_backend dynamic_servers

2、测试

由于设置了监控页面,可以先进入监控页面看下节点是否都正常:

查看节点没异常,这里分别访问三个域名进行测试:

访问bbs.21yunwei.com效果:

访问21yunwei.21yunwei.com效果:

访问lvs.21yunwei.com效果:

可见设置是正确的,没问题。 同时安装了论坛或者其他有session会话保持的系统的,可以进行session会话保持测试。 单独停掉其中的一个web节点,会话保持不受影响,依旧可以进行正常登陆操作。

标签:HAproxy 发布于:2019-11-11 02:44:58