Varnish 配置(始终为MISS)

标签 varnish varnish-vcl

我曾经在某一时间工作过,但是现在又被打破了(可能是由于php中的一些代码更改了),但是default.vcl并没有改变。

Varnish 版本是

    varnishd (varnish-3.0.7 revision f544cd8)
    Copyright (c) 2006 Verdens Gang AS
    Copyright (c) 2006-2014 Varnish Software AS

这是我要 Varnish 遵循的一些规则
  • 我需要 Varnish 来缓存所有页面并删除php session 或任何其他cookie,除非有特殊的cookie“ sh_loggedin ”当前
  • /social-signup应该通过,因为当用户登录
  • 时,它会创建上述cookie
  • /content应该通过,因为那是管理区域
  • 将HIT/MISS或counter相关的东西添加到 header 中,以便我知道 Varnish 是否在工作
  • 忽略所有缓存控制或年龄 header ,并确保将所有内容 Varnish ,除非出现“ sh_loggedin ” cookie出现
  • js,css,图像等应始终由 Varnish 提供服务,而不管
  • 允许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
    

    最佳答案

    响应 header 中的age值大于零,表示您正在获取缓存的响应。否则它将为零。

    关于 Varnish 配置(始终为MISS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39028390/

    相关文章:

    centos - 何时增加 VARNISH_STORAGE_SIZE?

    varnish - 使用Varnish缓存选定的URL

    caching - Varnish 4以从具有不同内容的多台服务器缓存

    使用cookie缓存而不使用cookie

    Varnish 提供错误的文件

    Varnish ,用于阻止拒绝服务攻击

    varnish - 如何设置具有两个后端的 Varnish ?

    caching - Varnish 仅缓存特定的URL路径

    apache - 为 Varnish 配置更改 httpd 配置上的监听端口

    varnish - 在 Varnish 4 中重试期间更改后端