HaProxy+Varnish+LAMP集群实现动静分离(3)

#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
    # to have these messages end up in /var/log/haproxy.log you will
    # need to:
    #
    # 1) configure syslog to accept network log events.  This is done
    #    by adding the '-r' option to the SYSLOGD_OPTIONS in
    #    /etc/sysconfig/syslog
    #
    # 2) configure local2 events to go to the /var/log/haproxy.log
    #  file. A line like the following can be added to
    #  /etc/sysconfig/syslog
    #
    #    local2.*                      /var/log/haproxy.log
    #
    log        127.0.0.1 local2
    chroot      /var/lib/haproxy
    pidfile    /var/run/haproxy.pid
    maxconn    4000
    user        haproxy
    group      haproxy
    daemon
    # turn on stats unix socket
    stats socket /var/lib/haproxy/stats
#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
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
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend web *:80
#acl url_static path_beg-i /static /images /javascript /sytlesheets
#acl url_static path_end -i .jpg .gif .png .css .js
#use_backend staticif url_static
use_backendvarnish_srv
#---------------------------------------------------------------------
# vanrnish server balance method
#---------------------------------------------------------------------
backend    varnish_srv #定义varnish后端主机组
balance    uri    #一致性hash   
hash-type  consistent  #一致性hash url   
servervarnish1 10.1.10.6:9988 check  #varnish服务器1,并添加健康状态检测
servervarnish2 10.1.10.7:9988 check  #varnish服务器02,并添加健康状态检测
listen stats      #定义状态监控管理页
bind:9002
stats uri /alren?admin #页面URL
stats hide-version    #影藏版文本信息
stats  authadmin:alren #提供认证页面
stats  admin if TRUE #认证通过则条状到相应页面

varnish配置如下:
# This is an example VCL file for Varnish.
#
# It does not do anything by default, delegating control to the
# builtin VCL. The builtin VCL is called when there is no explicit
# return statement.
#
# See the VCL chapters in the Users Guide at https://www.varnish-cache.org/docs/
# and for more examples.
# Marker to tell the VCL compiler that this VCL has been adapted to the
# new 4.0 format.
vcl 4.0; #版本信息
import directors; #导入模块
acl purges {  #定义修剪规则
    "127.0.0.1"; 
    "10.1.10.0";
}
backend web1 {
    .host = "10.1.10.68:80";
    .port = "80";
        .url ="/heath.php";
        .timeout = 2s;
        .interval = 1s;
        .window = 6; 
        .threshold = 3; 
        .expected_response = 200;
        .initial = 2;
}
backend web2 {
    .host = "10.1.10.69:80";
    .port = "80";
        .url ="/heath.php";
        .timeout = 2s;
        .interval = 1s;
        .window = 6; 
        .threshold = 3; 
        .expected_response = 200;
        .initial = 2;
}
backend app1 {
    .host = "10.1.10.70:80";
    .port = "80";
        .url ="/heath.html";
        .timeout = 2s;
        .interval = 1s;
        .window = 6; 
        .threshold = 3; 
        .expected_response = 200;
        .initial = 2;
}
backend app2 {
    .host = "10.1.10.71:80";
    .port = "80";
        .url ="/heath.html";
        .timeout = 2s;
        .interval = 1s;
        .window = 6; 
        .threshold = 3; 
        .expected_response = 200;
        .initial = 2;
}
sub vcl_init {
    new webcluster = directors.round_robin();
    webcluster.add_backend(web1);
    webcluster.add_backend(web2);
   
    new appcluster = directors.round_robin();
    appcluster.add_backend(app1);
    appcluster.add_backend(app2);
}
sub vcl_recv { 
    if (req.method == "PURGE"){
      if(!client.ip ~ purges){
        return(synth(408,"you don't have permission purge " + client.ip));
      }
return (purge);
    }
    if (req.url ~ "(?i)\.(php|asp|aspx|jsp)($|\?)") {
set req.backend_hint = appcluster.backend();
    }
  if (req.method != "GET" &&
      req.method != "HEAD" &&
      req.method != "PUT" &&
      req.method != "POST" &&
      req.method != "TRACE" &&
      req.method != "OPTIONS" &&
      req.method != "PATCH" &&
      req.method != "DELETE") {
      return (pipe);
  } 
  if (req.method != "GET" && req.method != "HEAD") {
        return (pass);
    }
  if (req.http.Authorization || req.http.Cookie) {
        return (pass);
    }
  if (req.http.Accept-Encoding) {
        if (req.url ~ "\.(bmp|png|gif|jpg|jpeg|ico|gz|tgz|bz2|tbz|zip|rar|mp3|mp4|ogg|swf|flv)$") {
            unset req.http.Accept-Encoding;
        } elseif (req.http.Accept-Encoding ~ "gzip") {
            set req.http.Accept-Encoding = "gzip";
        } elseif (req.http.Accept-Encoding ~ "deflate") {
            set req.http.Accept-Encoding = "deflate";
        } else {
            unset req.http.Accept-Encoding;
        }
    }
        return (hash);
}
sub vcl_pipe {
return (pipe);
}
sub vcl_miss {
return(fetch);
}
sub vcl_hash {
    hash_data(req.url);
    if (req.http.host) {
        hash_data(req.http.host);
    } else {
        hash_data(server.ip);
    }
    if (req.http.Accept-Encoding ~ "gzip") {
        hash_data ("gzip");
    } elseif (req.http.Accept-Encoding ~ "deflate") {
        hash_data ("deflate");
    }
}
sub vcl_backend_response {
    if (beresp.http.cache-control !~ "s-maxage") {
      if (bereq.url ~ "(?i)\.(jpg|jpeg|png|gif|css|js|html|htm)$") {
          unset beresp.http.Set-Cookie;
          set beresp.ttl = 3600s;
      }
    }
}
sub vcl_purge {
return(synth(200,"Purged"));
}
sub vcl_deliver {
    if (obj.hits > 0) {
        set resp.http.X-Cache = "HIT via " + req.http.host;
        set resp.http.X-Cache-Hits = obj.hits;
    } else {
        set resp.http.X-Cache = "MISS via " + req.http.host;
    }
}

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/9b9de23eee081a06f12159e11ff34540.html