清漆忽略了ttl

时间:2016-02-22 09:54:11

标签: caching nginx varnish ttl

我发现了一个无法找到问题的问题。

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

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

这是我的配置:

的/ etc /清漆/ 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;
}

主页缓存为1米,其余页面为5米,但由于未知原因,所有页面都缓存了2米。

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

有任何线索吗?

提前致谢。

1 个答案:

答案 0 :(得分:0)

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

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