Varnish:可缓存但未缓存

发布于 2025-01-06 15:31:07 字数 3237 浏览 1 评论 0原文

如果我运行此命令,

varnishtop -i txurl

该请求是“可缓存的”,但不是“已缓存的”。我看到“Age=0”(保留在清漆中的年龄),如何编辑它?

这是我的 .php 页面的请求

GET -Used http://example.com

User-Agent: lwp-request/5.810

200 OK
Cache-Control: max-age=60, public, must-revalidate
Connection: close
Date: Sat, 18 Feb 2012 12:14:33 GMT
Via: 1.1 varnish
Age: 0
Server: Apache
Vary: Accept-Encoding
Content-Type: text/html
Expires: Sat, 18 Feb 2012 12:24:33 GMT
Client-Date: Sat, 18 Feb 2012 12:14:34 GMT
Client-Peer: 173.236.219.104:80
Client-Response-Num: 1
Client-Transfer-Encoding: chunked
X-Cache: MISS
X-Cacheable: YES
X-Varnish: 840966561

<强>.htaccess代码

# BEGIN Expire headers
<ifModule mod_expires.c>
  ExpiresActive On
  ExpiresDefault "access plus 5 seconds"
  ExpiresByType image/x-icon "access plus 2592000 seconds"
  ExpiresByType image/jpeg "access plus 2592000 seconds"
  ExpiresByType image/png "access plus 2592000 seconds"
  ExpiresByType image/gif "access plus 2592000 seconds"
  ExpiresByType application/x-shockwave-flash "access plus 2592000 seconds"
  ExpiresByType text/css "access plus 604800 seconds"
  ExpiresByType text/javascript "access plus 216000 seconds"
  ExpiresByType application/javascript "access plus 216000 seconds"
  ExpiresByType application/x-javascript "access plus 216000 seconds"
  ExpiresByType text/html "access plus 600 seconds"
  ExpiresByType application/xhtml+xml "access plus 600 seconds"
</ifModule>
# END Expire headers

# BEGIN Cache-Control Headers
<ifModule mod_headers.c>
  <filesMatch "\.(ico|jpe?g|png|gif|swf)$">
    Header set Cache-Control "public"
  </filesMatch>
  <filesMatch "\.(css)$">
    Header set Cache-Control "public"
  </filesMatch>
  <filesMatch "\.(js)$">
    Header set Cache-Control "private"
  </filesMatch>
  <filesMatch "\.(x?html?|php)$">
    Header set Cache-Control "public, must-revalidate"
  </filesMatch>
</ifModule>

Default.vlc

backend default {
    .host = "173.236.219.104";
    .port = "81";
     .connect_timeout = 600s;
    .first_byte_timeout = 600s;
    .between_bytes_timeout = 600s;
}

sub vcl_deliver {
        if (obj.hits > 0) {
                set resp.http.X-Cache = "HIT";
        } else {
                set resp.http.X-Cache = "MISS";
        }
}

sub vcl_fetch {

    # Varnish determined the object was not cacheable
    if (beresp.ttl <= 0s) {
        set beresp.http.X-Cacheable = "NO:Not Cacheable";

    # You don't wish to cache content for logged in users
    } elsif (req.http.Cookie ~ "(UserID|_session)") {
        set beresp.http.X-Cacheable = "NO:Got Session";
        return(hit_for_pass);

    # You are respecting the Cache-Control=private header from the backend
    } elsif (beresp.http.Cache-Control ~ "private") {
        set beresp.http.X-Cacheable = "NO:Cache-Control=private";
        return(hit_for_pass);

    # Varnish determined the object was cacheable
    } else {
        set beresp.http.X-Cacheable = "YES";
    }

    # ....

    return(deliver);
}

if i run this command

varnishtop -i txurl

that request is "cacheble" but not "cached". I see "Age=0" (age that is keep in varnish), how to edit it?

This is the request for my .php page

GET -Used http://example.com

User-Agent: lwp-request/5.810

200 OK
Cache-Control: max-age=60, public, must-revalidate
Connection: close
Date: Sat, 18 Feb 2012 12:14:33 GMT
Via: 1.1 varnish
Age: 0
Server: Apache
Vary: Accept-Encoding
Content-Type: text/html
Expires: Sat, 18 Feb 2012 12:24:33 GMT
Client-Date: Sat, 18 Feb 2012 12:14:34 GMT
Client-Peer: 173.236.219.104:80
Client-Response-Num: 1
Client-Transfer-Encoding: chunked
X-Cache: MISS
X-Cacheable: YES
X-Varnish: 840966561

.htaccess code

# BEGIN Expire headers
<ifModule mod_expires.c>
  ExpiresActive On
  ExpiresDefault "access plus 5 seconds"
  ExpiresByType image/x-icon "access plus 2592000 seconds"
  ExpiresByType image/jpeg "access plus 2592000 seconds"
  ExpiresByType image/png "access plus 2592000 seconds"
  ExpiresByType image/gif "access plus 2592000 seconds"
  ExpiresByType application/x-shockwave-flash "access plus 2592000 seconds"
  ExpiresByType text/css "access plus 604800 seconds"
  ExpiresByType text/javascript "access plus 216000 seconds"
  ExpiresByType application/javascript "access plus 216000 seconds"
  ExpiresByType application/x-javascript "access plus 216000 seconds"
  ExpiresByType text/html "access plus 600 seconds"
  ExpiresByType application/xhtml+xml "access plus 600 seconds"
</ifModule>
# END Expire headers

# BEGIN Cache-Control Headers
<ifModule mod_headers.c>
  <filesMatch "\.(ico|jpe?g|png|gif|swf)$">
    Header set Cache-Control "public"
  </filesMatch>
  <filesMatch "\.(css)$">
    Header set Cache-Control "public"
  </filesMatch>
  <filesMatch "\.(js)$">
    Header set Cache-Control "private"
  </filesMatch>
  <filesMatch "\.(x?html?|php)$">
    Header set Cache-Control "public, must-revalidate"
  </filesMatch>
</ifModule>

Default.vlc

backend default {
    .host = "173.236.219.104";
    .port = "81";
     .connect_timeout = 600s;
    .first_byte_timeout = 600s;
    .between_bytes_timeout = 600s;
}

sub vcl_deliver {
        if (obj.hits > 0) {
                set resp.http.X-Cache = "HIT";
        } else {
                set resp.http.X-Cache = "MISS";
        }
}

sub vcl_fetch {

    # Varnish determined the object was not cacheable
    if (beresp.ttl <= 0s) {
        set beresp.http.X-Cacheable = "NO:Not Cacheable";

    # You don't wish to cache content for logged in users
    } elsif (req.http.Cookie ~ "(UserID|_session)") {
        set beresp.http.X-Cacheable = "NO:Got Session";
        return(hit_for_pass);

    # You are respecting the Cache-Control=private header from the backend
    } elsif (beresp.http.Cache-Control ~ "private") {
        set beresp.http.X-Cacheable = "NO:Cache-Control=private";
        return(hit_for_pass);

    # Varnish determined the object was cacheable
    } else {
        set beresp.http.X-Cacheable = "YES";
    }

    # ....

    return(deliver);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凌乱心跳 2025-01-13 15:31:07

最有可能的是后端返回一个 cookie,因此 varnish 不会缓存内容。

这里有更多关于它的信息: Varnish-Cookies

还有一些您从 LWP 获得的响应不一致。
事实上,我看到 max-age 标头设置为 60 秒,但我没有在后端的任何地方看到它的配置。

向源发出 LWP 请求也可以帮助调试问题。

Most probably the backend gives back a cookie, therefore varnish does not cache the content.

Here more info about it: Varnish-Cookies

There are also some inconsistencies on the response you get from LWP.
In fact I see a max-age header set to 60 seconds, which I do not see configured on the backed end anywhere.

An LWP request to the origin as well, could help to debug the problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文