Setting Varnish Cache as HTTP Load Balancer on Ubuntu 24.04

Setting Varnish Cache sebagai HTTP Load Balancer pada Ubuntu 24.04

  • Server Varnish : 192.168.10.100 – Port 80
  • Server Web1 : 192.168.10.11 – Port 80
  • Server Web2 : 192.168.10.12 – Port 80

  • Install varnish
apt update && apt install varnish -y
  • Edit port ExecStart Varnish dari 6081 menjadi 80
systemctl edit --full  varnish
LimitMEMLOCK=85983232
ExecStart=/usr/sbin/varnishd \
          -j unix,user=vcache \
          -F \
          -a :80 \
          -T localhost:6082 \
          -f /etc/varnish/default.vcl \
          -S /etc/varnish/secret \
          -s malloc,256m
ExecReload=/usr/share/varnish/varnishreload
ProtectSystem=full
ProtectHome=true
PrivateTmp=true
PrivateDevices=true
  • Konfigurasi varnish load balancer
  • Ganti IP pada baris .host sesuai dengan kebutuhan
nano /etc/varnish/default.vcl 
vcl 4.1;

import directors;

backend web1 {
    .host = "192.168.10.11";
    .port = "80";
    .probe = {
        .url = "/";
        .timeout = 3s;
        .interval = 5s;
        .window = 5;
        .threshold = 3;
    }
}

backend web2 {
    .host = "192.168.10.12";
    .port = "80";
    .probe = {
        .url = "/";
        .timeout = 3s;
        .interval = 5s;
        .window = 5;
        .threshold = 3;
    }
}

sub vcl_init {
    new balancer = directors.round_robin();
    balancer.add_backend(web1);
    balancer.add_backend(web2);
}

sub vcl_recv {
    set req.backend_hint = balancer.backend();

    # Bypass cache for WP Admin
    if (req.http.Cookie ~ "wordpress_logged_in" || req.url ~ "^/wp-(login|admin)/") {
        return (pass);
    }

    # Bypass cache for admin or logged-in users
    if (req.http.Cookie ~ "sessionid") {
        return (pass);
    }

    # Cache static files
    if (req.url ~ "\.(png|gif|jpg|jpeg|swf|css|js|html|ico|woff|woff2|ttf|eot)$") {
        return (hash);
    }

    # Bypass cache for certain dynamic pages
    if (req.url ~ "\.php$") {
        return (pass);
    }

    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 ~ "\.(png|gif|jpg|jpeg|swf|css|js|html|ico|woff|woff2|ttf|eot)$") {
        unset beresp.http.Server;
        if (beresp.http.content-type ~ "(text|application|javascript|css|html)") {
            set beresp.do_gzip = true;
        }
        unset beresp.http.set-cookie;
        set beresp.grace = 1d;
        set beresp.ttl = 60s;
        return (deliver);
    } else {
        if (bereq.url ~ "\.php$") {
            # PHP dynamic content; do not cache by default
            set beresp.uncacheable = true;
            return (pass);
        }
        # Optionally, cache other dynamic content with specific rules
        unset beresp.http.Server;
        if (beresp.http.content-type ~ "(text|application|javascript|css|html)") {
            set beresp.do_gzip = true;
        }
        unset beresp.http.set-cookie;
        set beresp.grace = 1d;
        return (deliver);
    }
}

sub vcl_deliver {
    unset resp.http.via;
    unset resp.http.x-varnish;
    set resp.http.grace = req.http.grace;
    if (obj.hits > 0) {
       set resp.http.X-Cache = "HIT";
    } else {
       set resp.http.X-Cache = "MISS";
    }
    return (deliver);
}
  • Verifikasi konfigurasi varnish, pastikan tidak ada error
varnishd -C -f /etc/varnish/default.vcl
  • Restart service varnish
systemctl daemon-reload
systemctl restart varnish

Pengujian

  • Verifikasi varnish backend
varnishadm backend.list
---<output>---
Backend name    Admin    Probe    Health     Last change
boot.web1       probe    5/5      healthy    Thu, 13 Jun 2024 14:41:14 GMT
boot.web2       probe    5/5      healthy    Thu, 13 Jun 2024 14:41:14 GMT
boot.balancer   probe    2/2      healthy    Thu, 13 Jun 2024 14:41:14 GMT
  • Verifikasi varnish lewat browser dengan url: http://ip_server_varnish_or_domain
  • Lakukan stop server web1 kemudian verifikasi kembali varnish backend
varnishadm backend.list
---<output>---
Backend name    Admin    Probe    Health     Last change
boot.web1       probe    0/5      sick       Thu, 13 Jun 2024 15:37:18 GMT
boot.web2       probe    5/5      healthy    Thu, 13 Jun 2024 15:37:18 GMT
boot.balancer   probe    1/2      healthy    Thu, 13 Jun 2024 15:41:08 GM
  • Verifikasi kembali varnish lewat browser dengan url: http://ip_server_varnish_or_domain

herdiana3389

A system administrator with skills in system administration, virtualization, linux, windows, networking, cloud computing, container, etc.