Varnish 清漆配置(始终未命中)

Varnish 清漆配置(始终未命中),varnish,varnish-vcl,Varnish,Varnish Vcl,我曾经让它工作过,但现在它又坏了(可能是因为php中的一些代码更改),但是default.vcl没有更改 清漆版是 varnishd (varnish-3.0.7 revision f544cd8) Copyright (c) 2006 Verdens Gang AS Copyright (c) 2006-2014 Varnish Software AS 以下是我想遵循的一些规则 我需要varnish来缓存所有页面并删除php会话或任何其他cookie,除非存在特殊c

我曾经让它工作过,但现在它又坏了(可能是因为php中的一些代码更改),但是default.vcl没有更改

清漆版是

    varnishd (varnish-3.0.7 revision f544cd8)
    Copyright (c) 2006 Verdens Gang AS
    Copyright (c) 2006-2014 Varnish Software AS
以下是我想遵循的一些规则

  • 我需要varnish来缓存所有页面并删除php会话或任何其他cookie,除非存在特殊cookie“sh_loggedin

  • /社交注册应该是通过的,因为当用户登录时,它会创建上面的cookie

  • /内容应该通过,因为这是管理区域

  • 在标题中添加命中/未命中或计数器相关的内容,以便我知道清漆是否起作用

  • 忽略所有缓存控制或年龄头,并确保varnish命中所有内容,除非出现“sh_loggedin”cookie

  • js、css、图像等应始终由varnish提供

  • 允许谷歌分析跟踪工作

这是它的样子

    backend default {
      .host = "127.0.0.1";
      .port = "8080";
      .connect_timeout = 600s;
      .first_byte_timeout = 600s;
      .between_bytes_timeout = 600s;
    }

    acl purge {
      "localhost";
      "127.0.0.1";
    }

    sub vcl_recv {

        if(req.url ~ "/social-signup") {
            return (pass);
        }
        if(req.url ~ "/scripts") {
            return (pass);
        }
        if(req.url ~ "/content") {
            return (pass);
        }
        if(req.url ~ "/api") {
            return (pass);
        }

    // Remove has_js and Google Analytics __* cookies.
    set req.http.Cookie = regsuball(req.http.Cookie, "(^|;\s*)(_[_a-z]+|has_js)=[^;]*", "");
    // Remove a ";" prefix, if present.
    set req.http.Cookie = regsub(req.http.Cookie, "^;\s*", "");


      if (!req.backend.healthy) {
        unset req.http.Cookie;
      }

      if (req.request == "GET" && req.url ~ "^/varnishcheck$") {
        error 200 "Varnish is Ready";
      }

    if(req.url ~ "/blog") {
        return (pass);
    }
      if (req.request != "GET" &&
        req.request != "HEAD" &&
        req.request != "PUT" &&
        req.request != "POST" &&
        req.request != "TRACE" &&
        req.request != "OPTIONS" &&
        req.request != "PURGE" &&
        req.request != "DELETE") {
          # Non-RFC2616 or CONNECT which is weird.
          return (pipe);
      }

      # We only deal with GET, PURGE and HEAD by default.
      if (req.request != "GET" && req.request != "HEAD" && req.request != "PURGE") {
        return (pass);
      }

      # --- PURGE ---
      if (req.request == "PURGE") {
        # Check if the ip coresponds with the acl purge
        if (!client.ip ~ purge) {
          # Return error code 405 (Forbidden) when not
          error 405 "Not allowed.";
        }
        return (lookup);
      }

      # --- PASSTHROUGH ---

      # Always cache things with these extensions.
      if (req.url ~ "\.(js|css|jpg|jpeg|png|gif|gz|tgz|bz2|tbz|mp3|ogg|swf)$") {
        unset req.http.cookie;
        return (lookup);
      }

    if(req.url ~ "/scripts") {
        return (pass);
    }
    if(req.url ~ "/api") {
        return (pass);
    }
      # Skip the Varnish cache for install, update, and cron.
      if (req.url ~ "install\.php|update\.php|cron\.php") {
        return (pass);
      }

      # Pass server-status.
      if (req.url ~ ".*/server-status$") {
        return (pass);
      }

      # Support for Pressflow Cookie-Cache Bypass.
      if (req.http.cookie ~ "NO_CACHE") {
        return (pass);
      }

      # Force lookup if the request is a no-cache request from the client.
      if (req.http.Cache-Control ~ "no-cache") {
        return (pass);
      }

      # Don't check cache if Drupal SESSION is set.
      if (req.http.cookie ~ "SESS") {
        return (pass);
      }

      # We  "hide" the non-session cookies.
      if (req.http.cookie) {
        set req.http.X-Varnish-Cookie = req.http.cookie;
        unset req.http.cookie;
      }

      # --- MISC ---

      # Normalize the Accept-Encoding header
      # as per: http://varnish-cache.org/wiki/FAQ/Compression
      if (req.http.Accept-Encoding) {
        if (req.url ~ "\.(jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg)$") {
          # No point in compressing these.
          unset req.http.Accept-Encoding;
        }
        else if (req.http.Accept-Encoding ~ "gzip") {
          set req.http.Accept-Encoding = "gzip";
        }
        else if (req.http.Accept-Encoding ~ "deflate") {
          # Next, try deflate if it is supported.
          set req.http.Accept-Encoding = "deflate";
        }
        else {
          # Unknown or deflate algorithm.
          unset req.http.Accept-Encoding;
        }
      }

      # Let's have a little grace.
      set req.grace = 5m;

      return (lookup);
    }

    sub vcl_hash {
      if (req.http.cookie) {
        hash_data(req.http.cookie);
      }
    }

    # Strip any cookies before an image/js/css is inserted into cache.
    sub vcl_fetch {


      remove beresp.http.Cache-Control;
      remove beresp.http.Age;
      set beresp.http.Age = "10";
      set beresp.http.Cache-Control = "public";

      set beresp.grace = 5m;

      # These status codes should always pass through and never cache.
      if (beresp.status == 503 || beresp.status == 500) {
        set beresp.http.X-Cacheable = "NO: obj.status";
        set beresp.http.X-Cacheable-status = beresp.status;
        return (hit_for_pass);
      }

      if (req.url ~ "\.(js|css|jpg|jpeg|png|gif|gz|tgz|bz2|tbz|mp3|ogg|swf)(\?[a-z0-9]+)?$") {
        unset beresp.http.set-cookie;
      }
      else if (beresp.http.Cache-Control) {
        unset beresp.http.Expires;
      }
    if(req.url !~ "/content") {
        unset beresp.http.Expires;
    }
        if (bereq.http.Cookie !~ "__sh_loggedin__") {
            unset bereq.http.Cookie;
            unset beresp.http.Set-Cookie;
        }


      if (beresp.status == 301) {
        set beresp.ttl = 1h;
        return(deliver);
      }

      # All tests passed, therefore item is cacheable
      set beresp.http.X-Cacheable = "YES";
    }

    # Set a header to track a cache HIT/MISS.
    sub vcl_deliver {
    set resp.http.cache-control = "max-age = 3600";
    set resp.http.Age = "10";

      if (obj.hits > 0) {
        set resp.http.X-Varnish-Cache = "HIT";
        set resp.http.X-Varnish-Hits = obj.hits;
      }
      else {
        set resp.http.X-Varnish-Cache = "MISS";
      }
      # Set a header to track the webhead.
      set resp.http.X-Varnish-IP = server.ip;
    }

    sub vcl_hit {
      if (req.request == "PURGE") {
        purge;
        error 200 "Purged.";
      }
    }

    sub vcl_miss {
      if (req.http.X-Varnish-Cookie) {
        set bereq.http.cookie = req.http.X-Varnish-Cookie;
        unset bereq.http.X-Varnish-Cookie;
      }
      if (req.request == "PURGE") {
        purge;
        error 200 "Purged.";
      }
    }

    sub vcl_error {
      set obj.http.Content-Type = "text/html; charset=utf-8";
      if (obj.status == 401) {
        # Prompt for password.
        set obj.http.WWW-Authenticate = "Basic realm=Secured";
      }
      synthetic {"
        <?xml version="1.0" encoding="utf-8"?>
        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
        <html>
          <head>
            <title>"} + obj.status + " " + obj.response + {"</title>
          </head>
          <body>
            <div id="page">
              <h1>Page Could Not Be Loaded</h1>
              <p>We're very sorry, but the page could not be loaded properly. This should be fixed very soon, and we apologize for any inconvenience.</p>
              <hr />
              <h4>Debug Info:</h4>
                <pre>Status: "} + obj.status + {"
    Response: "} + obj.response + {"
    XID: "} + req.xid + {"</pre>
            </div>
          </body>
        </html>
      "};

      return (deliver);
    }

响应头中的年龄值大于零,这意味着您正在获取缓存响应。否则它将为零。

响应头中的年龄值大于零,这意味着您正在获取缓存响应。否则它将是零。

因为我认为您已经在VCL配置中定义了年龄头,这就是为什么我们在响应头中得到它。
如果我错了,请纠正我,因为我认为您已经在VCL配置中定义了年龄标头,这就是为什么我们在响应标头中得到它。 如果我错了,请纠正我

    HTTP/1.1 200 OK
    Accept-Ranges:  bytes
    Age:    10
    cache-control:  max-age = 3600
    Content-Encoding:   gzip
    Content-Type:   text/html; charset=UTF-8
    Date:   Thu, 18 Aug 2016 23:29:25 GMT
    Server: Apache/2.4.23 (Amazon) OpenSSL/1.0.1k-fips PHP/5.6.24
    Vary:   Accept-Encoding,User-Agent
    Via:    1.1 varnish
    X-Cacheable:    YES
    X-Content-Type-Options: nosniff
    X-Frame-Options:    GOFORIT
    X-Varnish:  1595154742
    X-Varnish-Cache:    MISS
    X-Varnish-IP:   172.31.41.246
    X-XSS-Protection:   1; mode=block
    Connection: keep-alive