Showing posts with label Varnish. Show all posts
Showing posts with label Varnish. Show all posts

10/16/2019

Varnish Exemple for CACHE and Optimisation

Varnish Optimisation cache
#
# This is an example VCL file for Varnish.
#
# It does not do anything by default, delegating control to the
# builtin VCL. The builtin VCL is called when there is no explicit
# return statement.
#
# See the VCL chapters in the Users Guide at https://www.varnish-cache.org/docs/
# and https://www.varnish-cache.org/trac/wiki/VCLExamples for more examples.


# Marker to tell the VCL compiler that this VCL has been adapted to the
# new 4.0 format.
vcl 4.0;


# Default backend definition. Set this to point to your content server.
backend default {
.host = "127.0.0.1";
.port = "8080";
}


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.




# 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.


# Properly handle different encoding types
if (req.http.Accept-Encoding) {
if (req.url ~ "\.(jpg|jpeg|png|gif|gz|tgz|bz2|tbz|mp3|ogg|swf|woff)$") {
# No point in compressing these
unset req.http.Accept-Encoding;
} elsif (req.http.Accept-Encoding ~ "gzip") {
set req.http.Accept-Encoding = "gzip";
} elsif (req.http.Accept-Encoding ~ "deflate") {
set req.http.Accept-Encoding = "deflate";
} else {
# unknown algorithm (aka crappy browser)
unset req.http.Accept-Encoding;
}
}


# Cache files with these extensions
if (req.url ~ "\.(js|css|jpg|jpeg|png|gif|gz|tgz|bz2|tbz|mp3|ogg|swf|woff)$") {
unset req.http.cookie;
return (hash);
}


# Dont cache anything thats on the blog page or thats a POST request
if (req.url ~ "^/blog" || req.method == "POST") {
return (pass);
}


# This is Laravel specific, we have session-monster which sets a no-session header if we dont really need the set session cookie.
# Check for this and unset the cookies if not required
# Except if its a POST request
if (req.http.X-No-Session ~ "yeah" && req.method != "POST") {
unset req.http.cookie;
}


return (hash);
}


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 = 30s;
set beresp.grace = 24h;


if (beresp.ttl < 120s) {
unset beresp.http.cookie;
unset beresp.http.Set-Cookie;
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.
}