From 9c2e7dc5941855523b9f4d4e05a25153b68f4143 Mon Sep 17 00:00:00 2001 From: weidenwiesel Date: Mon, 2 Feb 2026 10:20:31 +0100 Subject: [PATCH] Add varnish cache --- docker-compose.yml | 19 ++++++++++++ etc/varnish/default.vcl | 47 +++++++++++++++++++++++++++++ nginx/sites-enabled/invidious.vhost | 15 +++++++++ 3 files changed, 81 insertions(+) create mode 100644 etc/varnish/default.vcl diff --git a/docker-compose.yml b/docker-compose.yml index 5d56f54..e767f3e 100755 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -391,6 +391,25 @@ services: invidious_proxy: ipv4_address: 192.42.6.2 + invidious-cache: + container_name: invidious-cache + restart: unless-stopped + cpus: 2 + mem_limit: "1024m" + memswap_limit: "1024m" + image: varnish:latest + user: 10043:10043 # Put uid/gid of your invidious user here + volumes: + - /srv/invidious/varnish:/var/lib/varnish + command: "-a :6081 -T localhost:6082 -f /var/lib/varnish/default.vcl -s file,/var/lib/varnish/varnish_storage.bin,16G -p cli_timeout=15" + ulimits: + memlock: + soft: 1073741824 + hard: 1073741824 + networks: + invidious_proxy: + ipv4_address: 192.42.6.3 + networks: invidious_proxy: enable_ipv6: true diff --git a/etc/varnish/default.vcl b/etc/varnish/default.vcl new file mode 100644 index 0000000..86936a1 --- /dev/null +++ b/etc/varnish/default.vcl @@ -0,0 +1,47 @@ +vcl 4.1; + +backend invidious_service { + .host = "192.42.6.2"; + .port = "3000"; + .connect_timeout = 5s; + .first_byte_timeout = 15s; + .between_bytes_timeout = 2s; +} + +sub vcl_recv { + set req.backend_hint = invidious_service; + + if (req.url ~ "(?i)\.(png|gif|jpg|jpeg|svg)$") { + unset req.http.Cookie; + } + + return (hash); +} + +sub vcl_hash { + hash_data(req.url); + if (req.http.Host) { + hash_data(req.http.Host); + } else { + hash_data(server.ip); + } + return (lookup); +} + +sub vcl_backend_response { + if (bereq.url ~ "(?i)\.(png|gif|jpg|jpeg|svg)$") { + unset beresp.http.Set-Cookie; + set beresp.ttl = 7d; + } + + return (deliver); +} + +sub vcl_deliver { + if (obj.hits > 0) { + set resp.http.X-Cache = "HIT"; + } else { + set resp.http.X-Cache = "MISS"; + } + return (deliver); +} diff --git a/nginx/sites-enabled/invidious.vhost b/nginx/sites-enabled/invidious.vhost index fcc2742..760007d 100644 --- a/nginx/sites-enabled/invidious.vhost +++ b/nginx/sites-enabled/invidious.vhost @@ -18,6 +18,11 @@ upstream invidious_worker { keepalive 1; } +# Varnish cache +upstream varnish_invidious { + server 192.42.6.3:6081; +} + geo $upstream_server { default invidious; } @@ -241,6 +246,16 @@ server { root /srv/nginx/public/invidious; } + # Varnish cache + location ~^(/vi/|/ggpht/) { + proxy_pass http://varnish_invidious; + + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + } + # Disable anubis for api stats location = /api/v1/stats { proxy_set_header Host $host;