Varnish Cache Webserver
Varnish Cache: HTTP-Beschleuniger
Varnish ist ein HTTP-Reverse-Proxy, der Webseiten cached und extrem schnell ausliefert. Er sitzt vor dem Webserver und kann die Antwortzeiten drastisch reduzieren.
Installation
# Ubuntu/Debian
sudo apt update
sudo apt install varnish -y
# Status
sudo systemctl status varnish
Grundkonfiguration
# Port ändern
sudo nano /etc/default/varnish
DAEMON_OPTS="-a :80 \
-T localhost:6082 \
-f /etc/varnish/default.vcl \
-S /etc/varnish/secret \
-s malloc,256m"
VCL Konfiguration
sudo nano /etc/varnish/default.vcl
vcl 4.1;
backend default {
.host = "127.0.0.1";
.port = "8080";
}
sub vcl_recv {
# Statische Dateien cachen
if (req.url ~ "\.(css|js|jpg|jpeg|png|gif|ico|svg|woff|woff2)$") {
unset req.http.Cookie;
return (hash);
}
# Admin-Bereich nicht cachen
if (req.url ~ "^/admin" || req.url ~ "^/wp-admin") {
return (pass);
}
}
sub vcl_backend_response {
# Cache-Zeit für statische Dateien
if (bereq.url ~ "\.(css|js|jpg|jpeg|png|gif|ico)$") {
set beresp.ttl = 7d;
unset beresp.http.set-cookie;
}
# Standard-TTL
if (beresp.ttl <= 0s) {
set beresp.ttl = 1h;
}
}
sub vcl_deliver {
# Debug-Header
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT";
} else {
set resp.http.X-Cache = "MISS";
}
}
Apache/Nginx auf anderen Port
# Nginx: Port 8080
listen 8080;
# Apache: Port 8080
Listen 8080
<VirtualHost *:8080>
Varnish-Befehle
# Cache leeren
varnishadm "ban req.url ~ ."
# Statistiken
varnishstat
# Live-Log
varnishlog
# Top-Requests
varnishtop -i ReqURL
Cache-Kontrolle
# Bestimmte URL invalidieren
varnishadm "ban req.url == /page.html"
# Bestimmtes Pattern
varnishadm "ban req.url ~ ^/blog/"
# Alles für einen Host
varnishadm "ban req.http.host == example.com"
Health Check
backend default {
.host = "127.0.0.1";
.port = "8080";
.probe = {
.url = "/health";
.timeout = 2s;
.interval = 5s;
.window = 5;
.threshold = 3;
}
}
Weitere Hilfe
- 📖 Varnish Dokumentation
- 📧 E-Mail: support@enjyn.de