From f0b5b7e79f0b3f08651490f2cdc3cf58576f8abd Mon Sep 17 00:00:00 2001 From: Nick Ballenger Date: Thu, 18 Jul 2019 16:27:25 -0700 Subject: [PATCH] Updated Varnish to cache SQL API / Windshaft The previous version of this file was enough to cache requests for the SQL API, but unfortunately no traffic was ever reaching Varnish to be cached. Nginx was proxying directly to the SQL API port, and Varnish was set to listen on 6081, so it wasn't able to intercept those requests. I updated the Nginx proxy config to aim at 6081 for requests to both SQL API and Windshaft, so now Varnish is receiving traffic. However, in order to know which backend to send traffic to, I had to add a custom HTTP header in the Nginx proxy pass. That header is picked up in the `vcl_recv` varnish subroutine and used to switch between backends. Additionally I've added logic for controlling what hosts can issue an HTTP PURGE command--in this case just localhost, since everything is on a single image. The purges will typically come from a Postgres trigger. As an overview of the purge related changes, see the Varnish docs here: https://varnish-cache.org/docs/3.0/tutorial/purging.html#http-purges --- config/varnish.vcl | 50 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 47 insertions(+), 3 deletions(-) diff --git a/config/varnish.vcl b/config/varnish.vcl index 0e3fed4..45bd7fb 100644 --- a/config/varnish.vcl +++ b/config/varnish.vcl @@ -1,4 +1,48 @@ -backend default { - .host = "127.0.0.1"; - .port = "8080"; +acl purge { + "localhost"; + "127.0.0.1"; +} + +backend sqlapi { + .host = "127.0.0.1"; + .port = "8080"; +} + +backend windshaft { + .host = "127.0.0.1"; + .port = "8181"; +} + +sub vcl_recv { + # Allowing PURGE from localhost + if (req.request == "PURGE") { + if (!client.ip ~ purge) { + error 405 "Not allowed."; + } + return (lookup); + } + + # Routing request to backend based on X-Carto-Service header from nginx + if (req.http.X-Carto-Service == "sqlapi") { + set req.backend = sqlapi; + remove req.http.X-Carto-Service; + } + if (req.http.X-Carto-Service == "windshaft") { + set req.backend = windshaft; + remove req.http.X-Carto-Service; + } +} + +sub vcl_hit { + if (req.request == "PURGE") { + purge; + error 200 "Purged."; + } +} + +sub vcl_miss { + if (req.request == "PURGE") { + purge; + error 200 "Purged."; + } }