gpt4 book ai didi

php - 我如何让 Varnish 4 缓存所有内容而不考虑缓存控制 header 和 cookie

转载 作者:搜寻专家 更新时间:2023-10-31 20:37:13 24 4
gpt4 key购买 nike

简而言之,我正在尝试

   if (beresp.ttl < 120s) {
set beresp.ttl = 120s;
unset beresp.http.Cache-Control;
}

在我的 VCL 配置文件中,它不工作。更多详情如下:

这是我的请求头:

Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8
Cache-Control:max-age=0
Connection:keep-alive
Cookie:portal1 =dsfgsdfgsdfg; portal1 =sdfgsdfgsdfg; PHPSESSID=randomstring
Host:216.66.35.169
Upgrade-Insecure-Requests:1
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.130 Safari/537.36

请注意,Chrome 默认情况下会在有人输入 url 和地址栏并按回车键时将缓存控制设置为 max-age=0。这些 cookie 是通过 session_start 的默认 php cookie,也是一个自定义 session cookie。

现在我想忽略 cookie 和缓存控制 header 。

这是我的 VCL 设置:

sub vcl_recv {
# Happens before we check if we have this in cache already.
#
# Typically you clean up the request here, removing cookies you don't need,
# rewriting the request, etc.
}

sub vcl_backend_response {
# Happens after we have read the response headers from the backend.
#
# Here you clean the response headers, removing silly Set-Cookie headers
# and other mistakes your backend does.
# set beresp.ttl = 60s;


if (beresp.ttl < 120s) {
set beresp.ttl = 120s;
unset beresp.http.Cache-Control;
}
}

sub vcl_deliver {
# Happens when we have all the pieces we need, and are about to send the
# response to the client.
#
# You can do accounting or modifying the final object here.

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

响应头是:

Accept-Ranges:bytes
Age:0
Connection:keep-alive
Content-Length:24
Content-Type:text/html
Date:Thu, 27 Aug 2015 13:48:58 GMT
Server:Apache/2.2.17 (Win32) mod_ssl/2.2.17 OpenSSL/0.9.8q PHP/5.3.5
Via:1.1 varnish-v4
X-Cache:MISS
X-Powered-By:PHP/5.3.5
X-Varnish:17

这是一个失误。

后端服务器上的 php 基本上只是回显一个随机数:

<?php

echo 'Hello worlds';

echo '<hr/>';

echo rand();

?>

最佳答案

Cookie 可防止缓存命中。尝试在 vcl_recvvcl_backend_response 中剥离 cookie。

sub vcl_recv {
unset req.http.cookie;
}

在 vcl_backend_response 中

sub vcl_backend_response {


if (beresp.ttl < 120s) {
unset beresp.http.cookie;
unset beresp.http.Set-Cookie;
set beresp.ttl = 120s;
unset beresp.http.Cache-Control;
}

}

关于php - 我如何让 Varnish 4 缓存所有内容而不考虑缓存控制 header 和 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32251378/

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