我是varnish的新手,并且使用Varnish 5.2并想知道为什么Varnish不会缓存AJAX Get请求,而如果通过浏览器的URL调用请求相同的Ajax调用,它会缓存内容。
以下是default.vcl文件
vcl 4.0;
import std;
backend default {
.host = "127.0.0.1";
.port = "8080";
}
sub vcl_recv {
# Normalize the header, remove the port (in case you're testing this on various TCP ports)
set req.http.Host = regsub(req.http.Host, ":[0-9]+", "");
# Remove the proxy header (see https://httpoxy.org/#mitigate-varnish)
unset req.http.proxy;
# Normalize the query arguments
set req.url = std.querysort(req.url);
# Allow purging
if (req.method == "PURGE") {
# If you got this stage (and didn't error out above), purge the cached result
return (purge);
}
# Some generic cookie manipulation, useful for all templates that follow
# Remove the "has_js" cookie
set req.http.Cookie = regsuball(req.http.Cookie, "has_js=[^;]+(; )?", "");
set req.http.Cookie = regsuball(req.http.Cookie, "PHPSESSID=[^;]+(; )?", "");
# Remove any Google Analytics based cookies
set req.http.Cookie = regsuball(req.http.Cookie, "__utm.=[^;]+(; )?", "");
set req.http.Cookie = regsuball(req.http.Cookie, "_ga=[^;]+(; )?", "");
set req.http.Cookie = regsuball(req.http.Cookie, "_gat=[^;]+(; )?", "");
set req.http.Cookie = regsuball(req.http.Cookie, "utmctr=[^;]+(; )?", "");
set req.http.Cookie = regsuball(req.http.Cookie, "utmcmd.=[^;]+(; )?", "");
set req.http.Cookie = regsuball(req.http.Cookie, "utmccn.=[^;]+(; )?", "");
# Remove DoubleClick offensive cookies
set req.http.Cookie = regsuball(req.http.Cookie, "__gads=[^;]+(; )?", "");
# Remove the Quant Capital cookies (added by some plugin, all __qca)
set req.http.Cookie = regsuball(req.http.Cookie, "__qc.=[^;]+(; )?", "");
# Remove the AddThis cookies
set req.http.Cookie = regsuball(req.http.Cookie, "__atuv.=[^;]+(; )?", "");
set req.http.Cookie = regsuball(req.http.Cookie, "^;\s*", "");
if (req.http.cookie ~ "^\s*$") {
unset req.http.cookie;
}
if (req.url ~ "^[^?]*\.(7z|avi|bz2|flac|flv|gz|mka|mkv|mov|mp3|mp4|mpeg|mpg|ogg|ogm|opus|rar|tar|tgz|tbz|txz|wav|webm|xz|zip)(\?.*)?$") {
unset req.http.Cookie;
return (hash);
}
if (req.url ~ "^[^?]*\.(7z|avi|bmp|bz2|css|csv|doc|docx|eot|flac|flv|gif|gz|ico|jpeg|jpg|js|less|mka|mkv|mov|mp3|mp4|mpeg|mpg|odt|otf|ogg|ogm|opus|pdf|png|ppt|pptx|rar|rtf|svg|svgz|swf|tar|tbz|tgz|ttf|txt|txz|wav|webm|webp|woff|woff2|xls|xlsx|xml|xz|zip)(\?.*)?$") {
unset req.http.Cookie;
return (hash);
}
if (req.http.X-Requested-With == "XMLHttpRequest"){
return (hash);
}
set req.http.Surrogate-Capability = "abc=ESI/1.0";
return (hash);
}
sub vcl_backend_response {
set beresp.grace = 1200s;
set beresp.ttl = 120s;
if (beresp.http.Surrogate-Control ~ "ESI/1.0")
{ unset beresp.http.Surrogate-Control; // for Varnish >= 3.0 set beresp.do_esi = true; // for Varnish < 3.0 // esi;
}
}
以下是varnishlog命令文件的响应
* << Request >> 98313
- Begin req 98311 rxreq
- Timestamp Start: 1523473475.288141 0.000000 0.000000
- Timestamp Req: 1523473475.288141 0.000000 0.000000
- ReqStart 192.168.33.1 39288
- ReqMethod GET
- ReqURL /get-test/sf1102505aabc133ddbae?customerId=110250
- ReqProtocol HTTP/1.1
- ReqHeader Host: test-website.test
- ReqHeader Connection: keep-alive
- ReqHeader Pragma: no-cache
- ReqHeader Cache-Control: no-cache
- ReqHeader Accept: */*
- ReqHeader X-Requested-With: XMLHttpRequest
- ReqHeader User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36
- ReqHeader Referer: http://test-website.test/get-widget-110250-ProductID1
- ReqHeader Accept-Encoding: gzip, deflate
- ReqHeader Accept-Language: en,es;q=0.9,en-US;q=0.8
- ReqHeader Cookie: PHPSESSID=e41a91kihnchjn9c4aor07jl3p
- ReqHeader X-Forwarded-For: 192.168.33.1
- VCL_call RECV
- ReqUnset Host: test-website.test
- ReqHeader Host: test-website.test
- ReqURL /get-test/sf1102505aabc133ddbae?customerId=110250
- ReqUnset Cookie: PHPSESSID=e41a91kihnchjn9c4aor07jl3p
- ReqHeader Cookie: PHPSESSID=e41a91kihnchjn9c4aor07jl3p
- ReqUnset Cookie: PHPSESSID=e41a91kihnchjn9c4aor07jl3p
- ReqHeader Cookie:
- ReqUnset Cookie:
- ReqHeader Cookie:
- ReqUnset Cookie:
- ReqHeader Cookie:
- ReqUnset Cookie:
- ReqHeader Cookie:
- ReqUnset Cookie:
- ReqHeader Cookie:
- ReqUnset Cookie:
- ReqHeader Cookie:
- ReqUnset Cookie:
- ReqHeader Cookie:
- ReqUnset Cookie:
- ReqHeader Cookie:
- ReqUnset Cookie:
- ReqHeader Cookie:
- ReqUnset Cookie:
- ReqHeader Cookie:
- ReqUnset Cookie:
- ReqHeader Cookie:
- ReqUnset Cookie:
- VCL_return hash
- ReqUnset Accept-Encoding: gzip, deflate
- ReqHeader Accept-Encoding: gzip
- VCL_call HASH
- VCL_return lookup
- VCL_call MISS
- VCL_return fetch
- Link bereq 98314 fetch
- Timestamp Fetch: 1523473475.452031 0.163890 0.163890
- RespProtocol HTTP/1.1
- RespStatus 200
- RespReason OK
- RespHeader Date: Wed, 11 Apr 2018 19:04:35 GMT
- RespHeader Server: Apache/2.4.7 (Ubuntu)
- RespHeader Access-Control-Allow-Origin: *
- RespHeader Access-Control-Allow-Credentials: true
- RespHeader Cache-Control: max-age=300, public
- RespHeader X-Cache-Debug: 1
- RespHeader Pragma: cache
- RespHeader X-Debug-Token: 4dda3e
- RespHeader X-Debug-Token-Link: http://test-website.test/_profiler/4dda3e
- RespHeader Vary: Accept-Encoding
- RespHeader Content-Encoding: gzip
- RespHeader Content-Length: 2039
- RespHeader Content-Type: text/css; charset=UTF-8
- RespHeader X-Varnish: 98313
- RespHeader Age: 0
- RespHeader Via: 1.1 varnish (Varnish/5.2)
- VCL_call DELIVER
- VCL_return deliver
- Timestamp Process: 1523473475.452071 0.163930 0.000039
- RespHeader Accept-Ranges: bytes
- RespHeader Connection: keep-alive
- Timestamp Resp: 1523473475.452225 0.164084 0.000155
- ReqAcct 589 0 589 527 2039 2566
- End
请记住:如果直接从浏览器调用
,它会获得HIT答案 0 :(得分:0)
您是否有varnishlog
正常GET
请求 - 也就是来自浏览器
我怀疑xmlhttprequest - 发送:
- ReqHeader Pragma: no-cache
- ReqHeader Cache-Control: no-cache
您可以通过取消设置来移除vcl_recv
中的2个标头:
sub vcl_recv {
unset req.http.Pragma;
unset req.http.Cache-Control;
}
或者您在应用程序本身中禁用它。例如,如果您使用的是jquery
$.ajaxSetup({
cache: true
});
但取决于你如何处理ajax请求。