Changing default_ttl on Varnish 4 vcl file - caching

I am willing to change the default ttl value from the configuration file and not through the varnishd.

sub vcl_backend_response {
...
set beresp.ttl = 24h;
...
}

Related

Leverage Browser Caching in PageSpeed Insights with Varnish & Nginx

Does anyone know how to remove the Leverage Browser Caching message from PageSpeed Insights using Varnish Cache and Nginx?
Google PageSpeed Image
I've tried adding the location ~*... block to the server block but that crashed the site. I think I'm missing a Varnish setting but can't find it.
Thanks in advance!
To override these headers and still put the element into cache for 2 minutes, the following configuration may be used:
sub vcl_fetch {
if (beresp.ttl < 120s) {
set beresp.ttl = 120s;
# Set the clients TTL on this object
set beresp.http.Cache-Control = "max-age=120";
}
}
Or, in Varnish 4.0 terms:
sub vcl_backend_response {
if (beresp.ttl < 120s) {
set beresp.ttl = 120s;
unset beresp.http.Cache-Control;
set beresp.http.Cache-Control = "max-age=120";
}
}

How to override stale cache while cache is not expired in varnish

After reading the varnish 4.1 document, I found that I can set 'req.hash_always_miss' to true to force a 'miss' while looking up cache and fetch the latest content in backend.
But I still can not figure out how to configure the vcl file.
below is the config file in my server.
backend default {
.host = "127.0.0.1";
.port = "8080";
}
acl purgers {
"localhost";
}
sub vcl_recv {
if (req.method == "PURGE") {
if (!client.ip ~ purgers) {
return(synth(405,"Not allowed."));
}
set req.hash_always_miss = true;
}
}
sub vcl_backend_response {
unset beresp.http.set-cookie;
set beresp.ttl = 1d;
}
while backend content change, I want to refresh the varnish cache before it get expired.
so I call curl -X PURGE http://localhost:6081/path/to/my/content
I do get the latest content as a response, but I found that varnish cache is still not get refreshed. Can someone tell me why? thanks

Varnish.4.x not caching range requests

We are trying to get Varnish to cache range requests. We are using Varnish 4.0.
We the following configurations
vcl 4.0;
import std;
# Default backend definition. Set this to point to your content server.
backend default {
.host = "194.142.x.x";
.port = "8008";
}
sub vcl_recv {
if (req.url ~ "(?i)\.(png|avi|mkv|mp4)(\?[a-z0-9]+)?$") {
unset req.http.Cookie;
}
# 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.
set req.http.host = regsub(req.http.host, "v\.","\rms\.");
std.log("REWRITED TO"+req.http.host+" "+req.url);
}
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.
}
sub vcl_deliver {
# Happens when we have all the pieces we need, and are about to send the
# response to the client.
#
vcl 4.0;
import std;
# Default backend definition. Set this to point to your content server.
backend default {
.host = "194.142.x.x";
.port = "8008";
}
sub vcl_recv {
if (req.url ~ "(?i)\.(png|avi|mkv|mp4)(\?[a-z0-9]+)?$") {
unset req.http.Cookie;
}
# 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.
set req.http.host = regsub(req.http.host, "v\.","\rms\.");
std.log("REWRITED TO"+req.http.host+" "+req.url);
}
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.
}
sub vcl_deliver {
# Happens when we have all the pieces we need, and are about to send the
# response to the client.
#
how ever range requests are taking too long to be served so we feel its not being cached as the original server is hit.
Can you solve this...?
I'm pretty sure the block config:
if (req.url ~ "(?i)\.(png|avi|mkv|mp4)(\?[a-z0-9]+)?$") {
unset req.http.Cookie;
}
# 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.
set req.http.host = regsub(req.http.host, "v\.","\rms\.");
std.log("REWRITED TO"+req.http.host+" "+req.url);
Are in wrong position, needs to be in vcl_backend_response not in vcl_recv
Caching partial objects with varnish 4.0
sub vcl_recv {
if (req.http.Range ~ "bytes=") {
set req.http.x-range = req.http.Range;
}
}
sub vcl_hash {
if (req.http.x-range ~ "bytes=") {
hash_data(req.http.x-range);
unset req.http.Range;
}
}
sub vcl_backend_fetch {
if (bereq.http.x-range) {
set bereq.http.Range = bereq.http.x-range;
}
}
sub vcl_backend_response {
if (bereq.http.x-range ~ "bytes=" && beresp.status == 206) {
set beresp.ttl = 10m;
set beresp.http.CR = beresp.http.content-range;
}
}
sub vcl_deliver {
if (resp.http.CR) {
set resp.http.Content-Range = resp.http.CR;
unset resp.http.CR;
}
}

Does setting beresp.ttl to 0s replace previous cache?

I have this issue, I use Varnish 3.0 and I want to avoid caching errors. I'm aware that this piece of code should do it:
if (beresp.status >= 500) {
set beresp.saintmode = 2m;
if (req.request != "POST") {
set beresp.ttl = 0s;
return(restart);
} else {
set beresp.ttl = 1s;
error 500 "Failed";
}
}
However, what concerns me is Varnish replacing old cache with the given error. I mean, I don't want Varnish to return a cached error if my backend is down (I have set grace mode).
So, my question is if Varnish replace the object stored for grace mode when setting beresp.ttl = 0s;
Well, I've just did some tests and Varnish does not replace the object cached.

Unable to see X-cache in Respomse Header

I want to check whether a page view on localhost encountered a cache hit or a cache miss. I'm running varnish on my local machine. Next, I wanna check the X-cache header in response. But I cant see any X-cache tag in the response header. i'm able to see server, Etag, x-runtime et. , but not X-cache
How can I see the X-cache?
By adding it;
sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT";
} else {
set resp.http.X-Cache = "MISS";
}
}

Resources