Skip to content

Instantly share code, notes, and snippets.

@sagar2009kumar
Created August 24, 2020 09:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sagar2009kumar/1829074e49df618d2dd9a1beef05c2d4 to your computer and use it in GitHub Desktop.
Save sagar2009kumar/1829074e49df618d2dd9a1beef05c2d4 to your computer and use it in GitHub Desktop.
VCl Configuration file for varnish.
#
# 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;
import std;
# 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.
if (req.method == "PURGE") {
if (!req.http.Cookie ~ "bookstorecache") {
return(synth(405,"Not allowed."));
}
ban("req.http.host ~ .*");
return (synth(200, "Purged"));
}
if ( req.url ~ "^/rest/V1/readbook" && (! req.http.Accept ~ "xml")) {
return (hash);
}
if ( req.url ~ "^/rest/V1/writebook" && ( !req.http.Accept ~ "xml")) {
return (hash);
}
if ( req.url ~ "^/rest/V1/deletebook" && ( !req.http.Accept ~ "xml")) {
return (hash);
}
if ( req.url ~ "^/rest/V1/addbook" && ( !req.http.Accept ~ "xml")) {
return (hash);
}
return (pass);
}
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.
if ( bereq.url ~ "^/rest/V1/readbook" && beresp.status == 200) {
unset beresp.http.set-cookie;
set beresp.ttl = 90s;
set beresp.http.Cache-Control = "public, max-age=90";
return (deliver);
}
if ( bereq.url ~ "^/rest/V1/writebook" && beresp.status == 200 ) {
unset beresp.http.set-cookie;
set beresp.ttl = 90s;
set beresp.http.Cache-Control = "public, max-age=90";
return (deliver);
}
if ( bereq.url ~ "^/rest/V1/deletebook" && beresp.status == 200) {
unset beresp.http.set-cookie;
set beresp.ttl = 90s;
set beresp.http.Cache-Control = "public, max-age=90";
return (deliver);
}
if ( bereq.url ~ "^/rest/V1/addbook" && beresp.status == 200) {
unset beresp.http.set-cookie;
set beresp.ttl = 90s;
set beresp.http.Cache-Control = "public, max-age=90";
return (deliver);
}
}
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.
# Insert Diagnostic header to show Hit or Miss
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT";
set resp.http.X-Cache-Hits = obj.hits;
}
else {
set resp.http.X-Cache = "MISS";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment