Dari beberapa situs, menjelaskan kalau Ghost memerlukan resource minimal ram 1Gb, sedangkan VPS yang dipergunakan untuk hosting blog ini juga ram nya 1Gb.
Alhasil ketika dicoba diberikan test 10 pengunjung, ternyata loadnya naik.
Setelah mencari di google, akhirnya nemu tutorial disini, dan dicoba menggabungkan Ghost + Caddy + Varnish, hasilnya mampu mengurangi load cukup drastis.
Sebelum lanjut, kita memakai konfigurasi berikut :
1 2 3 |
Web Server : Caddy CMS : Ghost port 2368 Varnish : port 6081 |
Jika belum ada varnish, maka kita install dulu varnish :
1 |
apt-get install varnish |
Edit konfigurasi Varnish
Edit file konfigurasi vanish di /etc/varnish/default.vcl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# # This is an example VCL file for Varnish. # # It does not do anything by default, delegating control to the # builtin VCL. The builtin VCL is called when there is no explicit # return statement. # # See the VCL chapters in the Users Guide for a comprehensive documentation # at https://www.varnish-cache.org/docs/. # Marker to tell the VCL compiler that this VCL has been written with the # 4.0 or 4.1 syntax. vcl 4.1; # Default backend definition. Set this to point to your content server. # Karena kita mau pakai untuk Ghost, maka sesuaikan portnya backend default { .host = "127.0.0.1"; .port = "2368"; } acl purge { "localhost"; "127.0.0.1"; # Add any other IP addresses that should be allowed to purge } sub vcl_recv { if (req.url ~ "/purge-cache") { if (!client.ip ~ purge) { return(synth(403, "Not allowed.")); } ban("req.http.host == arsip.my.id"); return(synth(200, "Cache cleared")); } # Cache static files if (req.url ~ "\.(css|js|avif|webp|png|jpe?g|gif|ico|svg|woff2?|eot|ttf|otf|json|csv|pdf|mp4|webm|ogg|mp3|wav|flac)$") { unset req.http.Cookie; return(hash); } # Don't cache if these cookies are present if (req.http.Cookie ~ "ghost-members-ssr" || req.http.Cookie ~ "ghost-admin-api-session") { return(pass); } # Don't cache these paths if (req.url ~ "^/(ghost|members|p)/") { return(pass); } # Remove all cookies for other requests unset req.http.Cookie; return(hash); } sub vcl_backend_response { # Cache static files and other content in Varnish for 1 year set beresp.ttl = 1y; # Enable stale content serving set beresp.grace = 24h; # Preserve the origin's Cache-Control header for client-side caching if (beresp.http.Cache-Control) { set beresp.http.X-Orig-Cache-Control = beresp.http.Cache-Control; } } sub vcl_deliver { # Restore the origin's Cache-Control header for the browser if (resp.http.X-Orig-Cache-Control) { set resp.http.Cache-Control = resp.http.X-Orig-Cache-Control; unset resp.http.X-Orig-Cache-Control; } else { # If no Cache-Control was set by the origin, we'll set a default set resp.http.Cache-Control = "no-cache, must-revalidate"; } # Add debug headers 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; } |
Dari file default.vcl tadi, sesuaikan port Ghost di blok backend default sesuai dengan port yang dipergunakan.
1 2 3 4 5 6 |
... backend default { .host = "127.0.0.1"; .port = "2368"; <-- ini port Ghost } ... |
Edit konfigurasi Caddy
Selanjutnya, edit konfigurasi caddy /etc/caddy/Caddyfile agar request diarahkan ke port Varnish.
1 2 3 4 |
https://arsip.my.id { encode zstd gzip reverse_proxy 127.0.0.1:6081 <-- kita arahkan ke port Varnish } |
Jadi konsepnya seperti ini :
1 2 |
Web Request : arsip.my.id –> Caddy (https) -> Varnish (port 6081) -> Ghost (port 2368) |
Enable varnish dan start :
1 2 |
systemctl enable varnish service varnish start |
Restart Caddy :
1 |
service caddy restart |
Untuk melihat apakah Varnish berfungsi, jalankan perintah berikut :
1 |
varnishstat |
Kendala ketika kita mempergunakan cache, adalah konten tidak langsung muncul ketika ada perubahan atau update di Ghost, untuk itu kita perlu membuat webhook supaya purge cache ketika ada update.
Caranya, masuk ke halaman Settings – Advanced – Integrations – Add Custom Integrations
Klik di Add Custom Integrations, kasih nama, misalnya Varnish, lalu klik Add
Akan muncul tampilan seperti ini :
Scroll kebawah, akan muncul Add Web Hook.
Klik di Add webhook, contoh pengisiannya :
- Name – Custome webhook : Purge (bebas, asal mudah diingat)
- Select an event : Site changed (rebuilt)
- Target URL : http://127.0.0.1:6081/purge-cache
Lalu klik Add