【问题标题】:Varnish ignores ttl清漆忽略 ttl
【发布时间】:2016-02-24 17:15:08
【问题描述】:

我有一个问题,我找不到它的来源。

我在带有 Nginx 的专用服务器上使用 Varnish Cache,并将网站重定向到他们的后端。

一切都像预期的那样工作,所有页面都被缓存 2 分钟,即使我为每个页面定义了不同的 ttl,定义的 ttl 被忽略或绕过或其他。

这是我的配置:

/etc/varnish/vcl_backend_response:

#example.com:
if (beresp.http.host == "example.com" || beresp.http.host == "www.example.com" ) {
include "/etc/varnish/backend_response/example.conf";
}
return(deliver);

        set beresp.http.Vary = "Accept-Encoding";
        set beresp.ttl = 1h;

        if (beresp.ttl <= 0s ||
                beresp.http.Set-Cookie ||
                beresp.http.Vary == "*") {
                set beresp.ttl = 120 s;
                set beresp.uncacheable = true;
                return (deliver);
        }

        unset beresp.http.Server;
        set beresp.http.Server = "Microsoft IIS/7.5";

    if (beresp.http.content-type ~ "text") {
        set beresp.do_gzip = true;
    }

    if (bereq.http.X-UA-Device) {
        if (!beresp.http.Vary) { # no Vary at all
            set beresp.http.Vary = "X-UA-Device";
        } elseif (beresp.http.Vary !~ "X-UA-Device") { # add to existing Vary
            set beresp.http.Vary = beresp.http.Vary + ", X-UA-Device";
        }
    }
        set beresp.http.X-UA-Device = bereq.http.X-UA-Device;
        set beresp.grace = 2h;

}

/etc/varnish/backend_response/example.conf

if (bereq.url ~ "(?i)\.(css|bmp|tif|ttf|docx|woff2|js|pict|tiff|eot|xlsx|jpg|csv|eps|woff|xls|jpeg|doc|ejs|otf|pptx|gif|pdf|swf|svg|ps|ico|pls|midi|svgz|class|png|ppt|mid|webp|jar)(\?[a-zA-Z0-9\=\.\-]+)?$") {
        set beresp.ttl = 4h;
        unset beresp.http.expires;
        set beresp.http.cache-control = "max-age=345600";
        set beresp.http.magicmarker = "1";
        set beresp.grace = 300m;
        set beresp.do_gzip = true;
} elseif(bereq.url == "/" ) {
        set beresp.ttl = 1m;
        set beresp.grace = 300m;
        set beresp.do_gzip = true;
} else {
        set beresp.ttl = 5m;
        set beresp.grace = 300m;
        set beresp.do_gzip = true;
}

首页缓存1m,其余页面5m,不知什么原因全部缓存2m。

/etc/varnish/vcl_recv.conf:

sub vcl_recv {

#exmaple.com:
        if (req.http.host == "exmaple.com" || req.http.host == "www.exmaple.com" ) {
                set req.backend_hint = exmaple;
                include "/etc/varnish/recv/exmaple.conf";
        }

include "/etc/varnish/recv/global.conf";
    unset req.http.cookie;
    unset req.http.Accept-Encoding;

call identify_device;

        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 ~ "\.(jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg)$") {
                        unset req.http.Accept-Encoding;
                } elseif (req.http.Accept-Encoding ~ "gzip") {
                        set req.http.Accept-Encoding = "gzip";
                } elsif (req.http.Accept-Encoding ~ "deflate") {
                        set req.http.Accept-Encoding = "deflate";
                } else {
                        unset req.http.Accept-Encoding;
                }
        }

        if (req.http.Cookie) {
                set req.http.Cookie = regsuball(req.http.Cookie, "(^|(?<=; )) *__utm.=[^;]+;? *", "\1");

                if (req.http.Cookie == "PHPSESSID|LOGIN") {
                        unset req.http.Cookie;
                }
        }

}

/etc/varnish/recv/exmaple.conf:

if (req.http.x-forwarded-for) {
        set req.http.X-Forwarded-For = req.http.X-Forwarded-For;
} else {
        set req.http.X-Forwarded-For = client.ip;
}
if (req.method != "GET" && req.method != "HEAD") {
        return(pass);
}
if (req.url ~ "(?i)\.(css|bmp|tif|ttf|docx|woff2|js|pict|tiff|eot|xlsx|jpg|csv|eps|woff|xls|jpeg|doc|ejs|otf|pptx|gif|pdf|swf|svg|ps|ico|pls|midi|svgz|class|png|ppt|mid|webp|jar)(\?[a-zA-Z0-9\=\.\-]+)?$") {
        unset req.http.cookie;
        return (hash);
} elseif (req.url ~ "/custom-page") {
        return (pass);
} elseif (req.url == "/" ) {
        return (hash);
} else {
        return (hash);
}

有什么线索吗?

提前致谢。

【问题讨论】:

    标签: caching nginx varnish ttl


    【解决方案1】:

    您的代码始终输入此 if 块,因此将 ttl 设置为 120 秒。请检查 3 个条件中的一个或多个条件评估为真(通过在 if 条件中一一删除它们)

      if (beresp.ttl <= 0s ||
                    beresp.http.Set-Cookie ||
                    beresp.http.Vary == "*") {
                    set beresp.ttl = 120 s; 
                    set beresp.uncacheable = true;
                    return (deliver);
            }
    

    【讨论】:

    • 尝试完全消除这种情况,仍然缓存 2 分钟,有什么想法吗?
    猜你喜欢
    • 2014-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-19
    • 2011-05-15
    • 2022-09-26
    • 2018-10-03
    • 2018-03-01
    相关资源
    最近更新 更多