清漆配置(总是MISS)

时间:2016-08-18 22:21:41

标签: 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,除非有特殊的cookie“ sh_loggedin ”存在

  • / social-signup应该通过,因为它会在用户登录时创建上面的cookie

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

  • 将HIT / MISS或计数器相关内容添加到标题中,以便我知道清漆是否正常工作

  • 忽略所有缓存控制或年龄标头,并确保清除HITS所有内容除了“ sh_loggedin ”cookie存在时

  • js,css,images等应始终由清漆提供,无论

  • 允许Google Analytics跟踪工作

这是它的样子

    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);
    }

http://www.isvarnishworking.com/说,我的网站是由varnish正确提供的,但我知道它不是,因为HIT计数器没有显示,日志也没有这么说。

这是我得到的回应

    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

2 个答案:

答案 0 :(得分:0)

响应标头中的年龄值大于零,表示您正在获得缓存响应。否则它将为零。

答案 1 :(得分:0)

我认为您已经在VCL配置中定义了Age标头,这就是我们在响应标头中获取它的原因。 如果我错了,请纠正我