Add varnish cache

This commit is contained in:
weidenwiesel
2026-02-02 10:20:31 +01:00
parent f673e86181
commit 9c2e7dc594
3 changed files with 81 additions and 0 deletions
+19
View File
@@ -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
+47
View File
@@ -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);
}
+15
View File
@@ -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;