Skip to content

Commit

Permalink
Added Varnish as a cache server to improve response times for heavy l…
Browse files Browse the repository at this point in the history
…oads
  • Loading branch information
FlorianLeChat committed Jul 22, 2024
1 parent abad96d commit 22ce5c9
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
16 changes: 16 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
62 changes: 62 additions & 0 deletions docker/default.vcl
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 22ce5c9

Please sign in to comment.