gpt4 book ai didi

caching - Varnish 缓存丢失-由VCL决定的TTL为0

转载 作者:行者123 更新时间:2023-12-03 17:46:15 27 4
gpt4 key购买 nike

我不确定为什么在设置适当的缓存头时VCL将TTL计算为0。我希望有更多经验的人可能会发现确实很明显的东西。

这是日志

varnish_1  | *   << BeReq    >> 18        
varnish_1 | - Begin bereq 17 pass
varnish_1 | - Timestamp Start: 1522327212.212730 0.000000 0.000000
varnish_1 | - BereqMethod GET
varnish_1 | - BereqURL /routes/%2F
varnish_1 | - BereqProtocol HTTP/1.1
varnish_1 | - BereqHeader Host: localhost:8081
varnish_1 | - BereqHeader accept: application/ld+json
varnish_1 | - BereqHeader User-Agent: Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Mobile Safari/537.36
varnish_1 | - BereqHeader DNT: 1
varnish_1 | - BereqHeader Referer: http://localhost:8081/
varnish_1 | - BereqHeader Accept-Encoding: gzip, deflate, br
varnish_1 | - BereqHeader Accept-Language: en-GB,en;q=0.9,en-US;q=0.8,la;q=0.7
varnish_1 | - BereqHeader Cookie: _ga=GA1.1.917775181.1515688563; Phpstorm-53bb5460=b5be55f5-d679-4bb9-b2b2-108bdac978eb; PHPSESSID=3gc3fulg7kdv0k5hiaitmlaq7e
varnish_1 | - BereqHeader If-None-Match: "90cd705e5c80bbb83aa64439f9b18c62"
varnish_1 | - BereqHeader X-Forwarded-For: 172.18.0.1
varnish_1 | - BereqHeader X-Varnish: 18
varnish_1 | - VCL_call BACKEND_FETCH
varnish_1 | - VCL_return fetch
varnish_1 | - BackendOpen 23 boot.default 172.18.0.4 80 172.18.0.5 34084
varnish_1 | - BackendStart 172.18.0.4 80
varnish_1 | - Timestamp Bereq: 1522327212.212948 0.000218 0.000218
varnish_1 | - Timestamp Beresp: 1522327217.902208 5.689478 5.689260
varnish_1 | - BerespProtocol HTTP/1.1
varnish_1 | - BerespStatus 200
varnish_1 | - BerespReason OK
varnish_1 | - BerespHeader Server: nginx/1.13.9
varnish_1 | - BerespHeader Content-Type: application/ld+json; charset=utf-8
varnish_1 | - BerespHeader Transfer-Encoding: chunked
varnish_1 | - BerespHeader Connection: keep-alive
varnish_1 | - BerespHeader X-Powered-By: PHP/7.2.3
varnish_1 | - BerespHeader Vary: Accept
varnish_1 | - BerespHeader Vary: Content-Type
varnish_1 | - BerespHeader Vary: Authorization
varnish_1 | - BerespHeader X-Content-Type-Options: nosniff
varnish_1 | - BerespHeader X-Frame-Options: deny
varnish_1 | - BerespHeader Cache-Control: max-age=0, public, s-maxage=3600
varnish_1 | - BerespHeader Date: Thu, 29 Mar 2018 12:40:17 GMT
varnish_1 | - BerespHeader Link: <http://localhost:8081/docs.jsonld>; rel="http://www.w3.org/ns/hydra/core#apiDocumentation"
varnish_1 | - BerespHeader ETag: "fe8fa0778f4ee8045999017ccf82c34f"
varnish_1 | - BerespHeader Cache-Tags: /routes/%252F,/pages/3d07e6ba3a1d4ba1860a1846c0c1436b,/layouts/72ecbecdf0c140db8ac146dc69d70e33,/nav_bars/ef102fd0830442348273c924ff62249f,/component_groups/e0cbdc6f83f3444ba0392d20497c19e0,/component_groups/bd7c54c561a34442869369d9d655f08f,/
varnish_1 | - TTL RFC 3600 10 -1 1522327218 1522327218 1522327217 0 3600
varnish_1 | - VCL_call BACKEND_RESPONSE
varnish_1 | - BerespHeader url: /routes/%2F
varnish_1 | - TTL VCL -1 3600 0 1522327218
varnish_1 | - BerespHeader X-Cacheable: NO:Not Cacheable
varnish_1 | - BerespHeader X-Cache-TTL: 0.000
varnish_1 | - TTL VCL 120 3600 0 1522327218
varnish_1 | - VCL_return deliver
varnish_1 | - Storage malloc Transient
varnish_1 | - ObjProtocol HTTP/1.1
varnish_1 | - ObjStatus 200
varnish_1 | - ObjReason OK
varnish_1 | - ObjHeader Server: nginx/1.13.9
varnish_1 | - ObjHeader Content-Type: application/ld+json; charset=utf-8
varnish_1 | - ObjHeader X-Powered-By: PHP/7.2.3
varnish_1 | - ObjHeader Vary: Accept, Content-Type, Authorization
varnish_1 | - ObjHeader X-Content-Type-Options: nosniff
varnish_1 | - ObjHeader X-Frame-Options: deny
varnish_1 | - ObjHeader Cache-Control: max-age=0, public, s-maxage=3600
varnish_1 | - ObjHeader Date: Thu, 29 Mar 2018 12:40:17 GMT
varnish_1 | - ObjHeader Link: <http://localhost:8081/docs.jsonld>; rel="http://www.w3.org/ns/hydra/core#apiDocumentation"
varnish_1 | - ObjHeader ETag: "fe8fa0778f4ee8045999017ccf82c34f"
varnish_1 | - ObjHeader Cache-Tags: /routes/%252F,/pages/3d07e6ba3a1d4ba1860a1846c0c1436b,/layouts/72ecbecdf0c140db8ac146dc69d70e33,/nav_bars/ef102fd0830442348273c924ff62249f,/component_groups/e0cbdc6f83f3444ba0392d20497c19e0,/component_groups/bd7c54c561a34442869369d9d655f08f,/
varnish_1 | - ObjHeader url: /routes/%2F
varnish_1 | - ObjHeader X-Cacheable: NO:Not Cacheable
varnish_1 | - ObjHeader X-Cache-TTL: 0.000
varnish_1 | - Fetch_Body 2 chunked stream
varnish_1 | - BackendReuse 23 boot.default
varnish_1 | - Timestamp BerespBody: 1522327217.902374 5.689644 0.000166
varnish_1 | - Length 3195
varnish_1 | - BereqAcct 588 0 588 1972 3195 5167
varnish_1 | - End
varnish_1 |
varnish_1 | * << Request >> 17
varnish_1 | - Begin req 16 rxreq
varnish_1 | - Timestamp Start: 1522327212.212645 0.000000 0.000000
varnish_1 | - Timestamp Req: 1522327212.212645 0.000000 0.000000
varnish_1 | - ReqStart 172.18.0.1 50692
varnish_1 | - ReqMethod GET
varnish_1 | - ReqURL /routes/%2F
varnish_1 | - ReqProtocol HTTP/1.1
varnish_1 | - ReqHeader Host: localhost:8081
varnish_1 | - ReqHeader Connection: keep-alive
varnish_1 | - ReqHeader accept: application/ld+json
varnish_1 | - ReqHeader User-Agent: Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Mobile Safari/537.36
varnish_1 | - ReqHeader DNT: 1
varnish_1 | - ReqHeader Referer: http://localhost:8081/
varnish_1 | - ReqHeader Accept-Encoding: gzip, deflate, br
varnish_1 | - ReqHeader Accept-Language: en-GB,en;q=0.9,en-US;q=0.8,la;q=0.7
varnish_1 | - ReqHeader Cookie: _ga=GA1.1.917775181.1515688563; Phpstorm-53bb5460=b5be55f5-d679-4bb9-b2b2-108bdac978eb; PHPSESSID=3gc3fulg7kdv0k5hiaitmlaq7e
varnish_1 | - ReqHeader If-None-Match: "90cd705e5c80bbb83aa64439f9b18c62"
varnish_1 | - ReqHeader X-Forwarded-For: 172.18.0.1
varnish_1 | - VCL_call RECV
varnish_1 | - VCL_return pass
varnish_1 | - VCL_call HASH
varnish_1 | - VCL_return lookup
varnish_1 | - VCL_call PASS
varnish_1 | - VCL_return fetch
varnish_1 | - Link bereq 18 pass
varnish_1 | - Timestamp Fetch: 1522327217.902388 5.689743 5.689743
varnish_1 | - RespProtocol HTTP/1.1
varnish_1 | - RespStatus 200
varnish_1 | - RespReason OK
varnish_1 | - RespHeader Server: nginx/1.13.9
varnish_1 | - RespHeader Content-Type: application/ld+json; charset=utf-8
varnish_1 | - RespHeader X-Powered-By: PHP/7.2.3
varnish_1 | - RespHeader Vary: Accept, Content-Type, Authorization
varnish_1 | - RespHeader X-Content-Type-Options: nosniff
varnish_1 | - RespHeader X-Frame-Options: deny
varnish_1 | - RespHeader Cache-Control: max-age=0, public, s-maxage=3600
varnish_1 | - RespHeader Date: Thu, 29 Mar 2018 12:40:17 GMT
varnish_1 | - RespHeader Link: <http://localhost:8081/docs.jsonld>; rel="http://www.w3.org/ns/hydra/core#apiDocumentation"
varnish_1 | - RespHeader ETag: "fe8fa0778f4ee8045999017ccf82c34f"
varnish_1 | - RespHeader Cache-Tags: /routes/%252F,/pages/3d07e6ba3a1d4ba1860a1846c0c1436b,/layouts/72ecbecdf0c140db8ac146dc69d70e33,/nav_bars/ef102fd0830442348273c924ff62249f,/component_groups/e0cbdc6f83f3444ba0392d20497c19e0,/component_groups/bd7c54c561a34442869369d9d655f08f,/
varnish_1 | - RespHeader url: /routes/%2F
varnish_1 | - RespHeader X-Cacheable: NO:Not Cacheable
varnish_1 | - RespHeader X-Cache-TTL: 0.000
varnish_1 | - RespHeader X-Varnish: 17
varnish_1 | - RespHeader Age: 0
varnish_1 | - RespHeader Via: 1.1 varnish-v4
varnish_1 | - VCL_call DELIVER
varnish_1 | - RespUnset url: /routes/%2F
varnish_1 | - RespHeader X-Cache: MISS
varnish_1 | - VCL_return deliver
varnish_1 | - Timestamp Process: 1522327217.902479 5.689834 0.000091
varnish_1 | - RespHeader Accept-Ranges: bytes
varnish_1 | - RespHeader Content-Length: 3195
varnish_1 | - Debug "RES_MODE 2"
varnish_1 | - RespHeader Connection: keep-alive
varnish_1 | - Timestamp Resp: 1522327217.902553 5.689908 0.000074
varnish_1 | - ReqAcct 568 0 568 2086 3195 5281
varnish_1 | - End
varnish_1 |
varnish_1 | * << Session >> 16
varnish_1 | - Begin sess 0 HTTP/1
varnish_1 | - SessOpen 172.18.0.1 50692 :80 172.18.0.5 80 1522327212.212513 21
varnish_1 | - Link req 17 rxreq
varnish_1 | - SessClose RX_TIMEOUT 10.695
varnish_1 | - End

RFC的TTL在我看来还可以(我认为)。但是VCL的TTL为-1

也许我的标题很长,但是如果有人可以给我一些建议,我将不胜感激。

谢谢,

编辑:
这是我的配置(认为可能会有所帮助)
vcl 4.0;

import std;

backend default {
.host = "api";
.port = "80";
# Health check
#.probe = {
# .url = "/";
# .timeout = 5s;
# .interval = 10s;
# .window = 5;
# .threshold = 3;
#}
}

# Hosts allowed to send BAN requests
acl ban {
"localhost";
"php";
}

sub vcl_backend_response {
# Ban lurker friendly header
set beresp.http.url = bereq.url;

# Add a grace in case the backend is down
set beresp.grace = 1h;

if (bereq.http.Cookie ~ "(UserID|_session)") {
set beresp.http.X-Cacheable = "NO:Got Session";
set beresp.uncacheable = true;
} elsif (beresp.ttl <= 0s) {
# Varnish determined the object was not cacheable
set beresp.http.X-Cacheable = "NO:Not Cacheable";
set beresp.http.X-Cache-TTL = beresp.ttl;
} elsif (beresp.http.set-cookie) {
# You don't wish to cache content for logged in users
set beresp.http.X-Cacheable = "NO:Set-Cookie";
set beresp.uncacheable = true;
} elsif (beresp.http.Cache-Control ~ "private") {
# You are respecting the Cache-Control=private header from the backend
set beresp.http.X-Cacheable = "NO:Cache-Control=private";
set beresp.uncacheable = true;
} else {
# Varnish determined the object was cacheable
set beresp.http.X-Cacheable = "YES";
}
}

sub vcl_deliver {
# Don't send cache tags related headers to the client
unset resp.http.url;
# Uncomment the following line to NOT send the "Cache-Tags" header to the client (prevent using CloudFlare cache tags)
#unset resp.http.Cache-Tags;
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT";
} else {
set resp.http.X-Cache = "MISS";
}
}

sub vcl_recv {
# Remove the "Forwarded" HTTP header if exists (security)
unset req.http.forwarded;

# To allow API Platform to ban by cache tags
if (req.method == "BAN") {
if (client.ip !~ ban) {
return(synth(405, "Not allowed"));
}

if (req.http.ApiPlatform-Ban-Regex) {
ban("obj.http.Cache-Tags ~ " + req.http.ApiPlatform-Ban-Regex);

return(synth(200, "Ban added"));
}

return(synth(400, "ApiPlatform-Ban-Regex HTTP header must be set."));
}
}

# From https://github.com/varnish/Varnish-Book/blob/master/vcl/grace.vcl
sub vcl_hit {
if (obj.ttl >= 0s) {
# Normal hit
return (deliver);
} elsif (std.healthy(req.backend_hint)) {
# The backend is healthy
# Fetch the object from the backend
return (fetch);
} else {
# No fresh object and the backend is not healthy
if (obj.ttl + obj.grace > 0s) {
# Deliver graced object
# Automatically triggers a background fetch
return (deliver);
} else {
# No valid object to deliver
# No healthy backend to handle request
# Return error
return (synth(503, "API is down"));
}
}
}

最佳答案

在IRC上聊天后(用户fgs非常有帮助),我可以更好地阅读日志并了解为什么会发生这种情况。

vcl_recv返回的是传递而不是查找,因为cookie是在请求中发送的。

我错过了这里的文档:
https://varnish-cache.org/docs/4.1/users-guide/increasing-your-hitrate.html#cookies

关于caching - Varnish 缓存丢失-由VCL决定的TTL为0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49555139/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com