varnish实战--VCL实战

作者: 4e8ea348373f | 来源:发表于2018-04-21 11:04 被阅读5次

    实战一:

           backend websrv1 {
                .host = "172.16.100.68";
                .port = "80";
                .probe = {
                    .url = "/test1.html";
                }
            }
    
            backend websrv2 {
                .host = "172.16.100.69";
                .port = "80";
                .probe = {
                    .url = "/test1.html";
                }
            }
    
            sub vcl_recv {
                if (req.url ~ "(?i)\.(jpg|png|gif)$") {
                    set req.backend_hint = websrv1;
                } else {
                    set req.backend_hint = websrv2;
                }               
            }
    

    实战二:

            import directors;
    
            sub vcl_init {
                new mycluster = directors.round_robin();
                mycluster.add_backend(websrv1);
                mycluster.add_backend(websrv2);
            }
    
            vcl_recv {
                set req.backend_hint = mycluster.backend();
            }
    
            负载均衡算法:
                fallback, random, round_robin, hash
    

    补充:

        移除单个缓存对象
    
            purge用于清理缓存中的某特定对象及其变种(variants),因此,在有着明确要修剪的缓存对象时可以使用此种方式。HTTP协议的PURGE方法可以实现purge功能,不过,其仅能用于vcl_hit和vcl_miss中,它会释放内存工作并移除指定缓存对象的所有Vary:-变种,并等待下一个针对此内容的客户端请求到达时刷新此内容。另外,其一般要与return(restart)一起使用。下面是个在VCL中配置的示例。
    
            acl purgers {
                "127.0.0.1";
                "192.168.0.0"/24;
            }
    
            sub vcl_recv {
                if (req.request == "PURGE") {
                    if (!client.ip ~ purgers) {
                        error 405 "Method not allowed";
                    }
                    return (lookup);
                }
            }
            sub vcl_hit {
                if (req.request == "PURGE") {
                    purge;
                    error 200 "Purged";
                }
            }
            sub vcl_miss {
                if (req.request == "PURGE") {
                    purge;
                    error 404 "Not in cache";
                }
            }
            sub vcl_pass {
                if (req.request == "PURGE") {
                    error 502 "PURGE on a passed object";
                }
            }
    
            客户端在发起HTTP请求时,只需要为所请求的URL使用PURGE方法即可,其命令使用方式如下:
            # curl -I -X PURGE http://varniship/path/to/someurl
    

    生产环境案例一则:

        acl purge {
          "localhost";
          "127.0.0.1";
          "10.1.0.0"/16;
          "192.168.0.0"/16;
        }
    
        sub vcl_hash {
          hash_data(req.url);
          return (hash);
        }
    
        sub vcl_recv {
          set req.backend = shopweb;
        #  set req.grace = 4h; 
          if (req.request == "PURGE") {
            if (!client.ip ~ purge) {
              error 405 "Not allowed.";
            }
            return(lookup);
          }
          if (req.request == "REPURGE") {
            if (!client.ip ~ purge) {
              error 405 "Not allowed.";
            }
            ban("req.http.host == " + req.http.host + " && req.url ~ " + req.url);
            error 200 "Ban OK";
          }
          if (req.restarts == 0) {
            if (req.http.x-forwarded-for) {
              set req.http.X-Forwarded-For = req.http.X-Forwarded-For + ", " + client.ip;
            } 
            else {
              set req.http.X-Forwarded-For = client.ip;
            }
          }
          if (req.request != "GET" &&
            req.request != "HEAD" &&
            req.request != "PUT" &&
            req.request != "POST" &&
            req.request != "TRACE" &&
            req.request != "OPTIONS" &&
            req.request != "DELETE") {
            /* Non-RFC2616 or CONNECT which is weird. */
            return (pipe);
          }
          if (req.request != "GET" && req.request != "HEAD") {
            /* We only deal with GET and HEAD by default */
            return (pass);
          }
          if (req.http.Authorization) {
            /* Not cacheable by default */
            return (pass);
          }
          
    
          if ( req.url == "/Heartbeat.html" ) {
            return (pipe);
          }
          if ( req.url == "/" ) {
            return (pipe);
          }
          if ( req.url == "/index.jsp" ) {
            return (pipe);
          }
    
          if (req.http.Cookie ~ "dper=") {
            return (pass);
          }
          if (req.http.Cookie ~ "sqltrace=") {
            return (pass);
          }
          if (req.http.Cookie ~ "errortrace=") {
            return (pass);
          }
        #   if ( req.request == "GET" && req.url ~ "req.url ~ "^/shop/[0-9]+$" ) {
          if ( req.url ~ "^/shop/[0-9]+$" || req.url ~ "^/shop/[0-9]?.*" ) {
            return (lookup);
          }
    
         if ( req.url ~ "^/shop/(\d{1,})/editmember" || req.url ~ "^/shop/(\d{1,})/map" || req.url ~ "^/shop/(\d+)/dish-([^/]+)" ) {
            return (lookup);
          } 
    
          return (pass);
        #   return (lookup);
        }
    
        sub vcl_pipe {
          return (pipe);
        }
    
        sub vcl_pass {
          return (pass);
        }
    
        sub vcl_hit {
          if (req.request == "PURGE") {
            purge;
            error 200 "Purged.";
          }
          return (deliver);
        }
    
        sub vcl_miss {
          if (req.request == "PURGE") {
            error 404 "Not in cache.";
          }
        #   if (object needs ESI processing) {
        #     unset bereq.http.accept-encoding;
        #   }
          return (fetch);
        }
    
    
        sub vcl_fetch {
          set beresp.ttl = 3600s;
          set beresp.http.expires = beresp.ttl;
          #set beresp.grace = 4h;
        #   if (object needs ESI processing) {
        #     set beresp.do_esi = true;
        #     set beresp.do_gzip = true;
        #   }
    
          if ( req.url ~ "^/shop/[0-9]+$" || req.url ~ "^/shop/[0-9]?.*" ) {   
            set beresp.ttl = 4h;
          }
    
         if ( req.url ~ "^/shop/(\d{1,})/editmember" || req.url ~ "^/shop/(\d{1,})/map" || req.url ~ "^/shop/(\d+)/dish-([^/]+)" ) {
             set beresp.ttl = 24h;
          } 
    
          if (beresp.status != 200){
            return (hit_for_pass);
          }
          return (deliver);
        }
    
        sub vcl_deliver {
          if (obj.hits > 0){
            set resp.http.X-Cache = "HIT";
          } 
          else {
            set resp.http.X-Cache = "MISS";
          }
          set resp.http.X-Powered-By = "Cache on " + server.ip;
          set resp.http.X-Age = resp.http.Age;
          return (deliver);
        }
    
        sub vcl_error {
          set obj.http.Content-Type = "text/html; charset=utf-8";
          set obj.http.Retry-After = "5";
          synthetic {""} + obj.status + " " + obj.response + {""};
          return (deliver);
        }
    
        sub vcl_init {
          return (ok);
        }
    
        sub vcl_fini {
          return (ok);
        }
    

    补充资料:

    varnish的线程模型:
        cache-worker线程
        cache-main线程:此线程只有一个,用于启动caceh;
        ban luker:
        acceptor:
        epoll:线程池管理器
        expire:清理过期缓存
    
        varnish定义其最大并发连接数:线程池模型:
            thread_pools:线程池个数;默认为2;
            thread_pool_max:单线程池内允许启动的最多线程个数;
            thread_pool_min
            thread_pool_timeout:多于thread_pool_min的线程空闲此参数指定的时长后即被purge;
    
    varnish的param查看及改变:
        param.show [-l] [param]
        param.set [param] [value]
    
    varnish的命令行工具:
        varnishadm, 
    
        varnishtop: 内存日志区域查看工具
            RxHeader User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.81 Safari/537.36
    
            其中:
                RxHeader:称为tag, 基于tag过滤,可使用-i或-x选项;
                User-Agent起始的内容:称为日志信息,可使用-I或-X选项进行过滤;
    
                -I regexp: 仅显示被模式匹配到的条目
                -X regexp:仅显示不被模式匹配到的条目
                -C: 忽略字符大小写;
                -d: 显示已有日志;
    
        varnishstat:
            -f field, field, ...
            -l: 列出所有可用字段
            -x: xml输出格式 
            -j: json输出格式
    
        varnishlog, varnishncsa

    相关文章

      网友评论

        本文标题:varnish实战--VCL实战

        本文链接:https://www.haomeiwen.com/subject/mdezkftx.html