Pengantar: Memulai dari Core Server

Banyak pengguna WordPress mengandalkan plugin cache berat seperti WP Rocket atau W3 Total Cache untuk mempercepat situs web mereka. Padahal, setiap kali request kode PHP server harus memprosenya: menghubungkan database SQL, lalu menyusun HTML.

Dengan Nginx FastCGI Cache, halaman HTML yang di-generate dinamis oleh PHP akan disimpan langsung di memory server. Request berikutnya tidak akan menyentuh PHP-FPM sama sekali.

Langkah 1: Mendefinisikan Cache Zone pada Nginx.conf

Buka konfigurasi server blok utama Anda melalui terminal SSH:

LANG: BASH
bash $ nano /etc/nginx/nginx.conf

Tambahkan baris parameter berikut di dalam blok http { ... }:

LANG: NGINX
fastcgi_cache_path /var/nginx/cache levels=1:2 keys_zone=WORDPRESS:100m inactive=60m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
fastcgi_cache_use_stale error timeout invalid_header http_500;

Langkah 2: Mengkonfigurasi Server Block Virtual Host

Pada file server block website Anda, tentukan pengecualian cookie:

LANG: NGINX
# Jangan cache halaman admin WordPress
if ($request_uri ~* "/wp-admin/|xmlrpc.php|wp-*.php|feed/|sitemap(.*)?.xml") {
    set $skip_cache 1;
}

# Jangan cache user yang sedang login
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
    set $skip_cache 1;
}

Lalu arahkan block PHP handler Anda menuju key zone yang didaftarkan:

LANG: NGINX
location ~ \.php$ {
    try_files $uri =404;
    fastcgi_pass unix:/run/php/php8.2-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;

    fastcgi_cache WORDPRESS;
    fastcgi_cache_valid 200 360 301 1h;
    fastcgi_cache_bypass $skip_cache;
    fastcgi_no_cache $skip_cache;

    add_header X-FastCGI-Cache $upstream_cache_status;
}

Langkah 3: Verifikasi Status Handshake

Wipe/restart Nginx daemon Anda untuk menerapkan perubahan:

LANG: BASH
bash $ nginx -t && systemctl restart nginx

Gunakan perintah curl untuk melihat HTTP header:

LANG: BASH
bash $ curl -I https://portofolio.web.id

Sistem akan memberikan respon HTTP/1.1 200 dengan header x-fastcgi-cache: HIT.

Status HIT menandakan bahwa loading aset halaman berjalan instan secepat static minimal HTML!