From 22ce5c9e4eb67a81d630f70f54dbd8b7b9e6c094 Mon Sep 17 00:00:00 2001 From: Florian Trayon <26360935+FlorianLeChat@users.noreply.github.com> Date: Mon, 22 Jul 2024 21:06:53 +0200 Subject: [PATCH] Added Varnish as a cache server to improve response times for heavy loads --- docker-compose.yml | 16 ++++++++++++ docker/default.vcl | 62 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 docker/default.vcl diff --git a/docker-compose.yml b/docker-compose.yml index 0fbeb817..26ede6a4 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -57,6 +57,22 @@ services: ports: - "8080:80" + # https://hub.docker.com/_/varnish + varnish: + image: varnish:alpine + depends_on: + - php + volumes: + - ./docker/default.vcl:/etc/varnish/default.vcl:ro + environment: + VARNISH_SIZE: 128m + healthcheck: + test: wget -O - http://localhost + retries: 3 + timeout: 5s + ports: + - "8081:80" + # https://github.com/FlorianLeChat/Source-Web-Console php: image: source-web-console diff --git a/docker/default.vcl b/docker/default.vcl new file mode 100644 index 00000000..3d603644 --- /dev/null +++ b/docker/default.vcl @@ -0,0 +1,62 @@ +vcl 4.1; + +backend default { + .host = "php"; + .port = "9000"; +} + +sub vcl_recv { + set req.backend_hint = vdir.backend(); + + if (req.http.Cookie) { + set req.http.Cookie = ";" + req.http.Cookie; + set req.http.Cookie = regsuball(req.http.Cookie, "; +", ";"); + set req.http.Cookie = regsuball(req.http.Cookie, ";(PHPSESSID)=", "; \1="); + set req.http.Cookie = regsuball(req.http.Cookie, ";[^ ][^;]*", ""); + set req.http.Cookie = regsuball(req.http.Cookie, "^[; ]+|[; ]+$", ""); + + if (req.http.Cookie == "") { + unset req.http.Cookie; + } + } + + if (req.method != "GET" && + req.method != "HEAD" && + req.method != "PUT" && + req.method != "POST" && + req.method != "TRACE" && + req.method != "OPTIONS" && + req.method != "DELETE" + ) { + return (pipe); + } + + if (req.http.Authorization) { + return (pass); + } + + if (req.method != "GET" && req.method != "HEAD") { + return (pass); + } +} + +sub vcl_backend_response { + if (beresp.status >= 400 && beresp.status < 600) { + set beresp.uncacheable = true; + return (deliver); + } + + set beresp.ttl = 5m; + + return (deliver); +} + +sub vcl_deliver { + if (obj.hits > 0) { + set resp.http.X-Cache = "HIT"; + } else { + set resp.http.X-Cache = "MISS"; + } + + set resp.http.X-Cache-Hits = obj.hits; +} \ No newline at end of file