Bab 10: Caching dan Performance Optimization di Nginx

Bab 10: Caching dan Performance Optimization di Nginx

Caching dan optimasi performa penting untuk mempercepat load website, mengurangi beban server, dan meningkatkan pengalaman pengguna.

Nginx menyediakan beberapa mekanisme caching dan optimasi, baik untuk konten statis maupun dinamis.


1. Static Content Caching

Nginx dapat menyajikan file statis (HTML, CSS, JS, gambar) dengan header caching agar browser menyimpannya untuk beberapa waktu.

Contoh konfigurasi:

server {
    listen 80;
    server_name example.com;

    root /var/www/html/example;

    location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
        expires 30d;
        add_header Cache-Control "public";
    }
}
  • expires 30d → file disimpan browser selama 30 hari
  • Cache-Control: public → file dapat dicache oleh browser dan CDN

Tips: gunakan untuk semua static assets, hindari caching untuk halaman yang sering berubah.


2. Proxy Cache

Proxy cache menyimpan response dari backend agar request berikutnya tidak selalu ke backend.

Contoh konfigurasi:

proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m max_size=1g inactive=60m use_temp_path=off;

server {
    listen 80;
    server_name example.com;

    location / {
        proxy_pass http://backend;
        proxy_cache my_cache;
        proxy_cache_valid 200 10m;
        proxy_cache_use_stale error timeout updating;
        add_header X-Proxy-Cache $upstream_cache_status;
    }
}
  • proxy_cache_path → direktori cache dan ukuran memori untuk key
  • proxy_cache_valid 200 10m → cache response 200 selama 10 menit
  • proxy_cache_use_stale → gunakan cache lama saat backend error atau timeout
  • X-Proxy-Cache → header untuk debugging status cache (HIT, MISS, BYPASS)

3. FastCGI Cache

FastCGI cache digunakan untuk website dinamis seperti PHP. Nginx menyimpan hasil render dari FastCGI backend agar tidak memproses ulang PHP setiap request.

Contoh konfigurasi:

fastcgi_cache_path /var/cache/nginx/fastcgi levels=1:2 keys_zone=php_cache:100m inactive=60m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";

server {
    listen 80;
    server_name example.com;

    root /var/www/html/example;

    location ~ \.php$ {
        fastcgi_pass unix:/run/php/php8.1-fpm.sock;
        include fastcgi_params;
        fastcgi_cache php_cache;
        fastcgi_cache_valid 200 10m;
        add_header X-FastCGI-Cache $upstream_cache_status;
    }
}
  • fastcgi_cache → mengaktifkan caching untuk PHP
  • fastcgi_cache_valid → durasi cache untuk response 200
  • X-FastCGI-Cache → header untuk memonitor cache status

4. Gzip Compression

Gzip memperkecil ukuran file sebelum dikirim ke client, meningkatkan kecepatan loading.

Contoh konfigurasi:

http {
    gzip on;
    gzip_disable "msie6";

    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
}
  • gzip on → aktifkan gzip
  • gzip_types → tipe file yang dikompresi
  • gzip_comp_level → level kompresi (1-9)
  • gzip_vary on → mempermudah caching oleh proxy/CDN

5. Limit Rate / Bandwidth Throttling

Membatasi jumlah request atau bandwidth per client untuk mencegah abuse atau overload.

Contoh limit rate:

http {
    limit_req_zone $binary_remote_addr zone=mylimit:10m rate=10r/s;
    
    server {
        location / {
            limit_req zone=mylimit burst=20 nodelay;
            proxy_pass http://backend;
        }
    }
}
  • rate=10r/s → maksimal 10 request per detik per IP
  • burst=20 → menampung request lebih saat spike
  • nodelay → request melebihi burst ditolak langsung

Limit bandwidth:

server {
    location /downloads/ {
        limit_rate 500k;
    }
}
  • Membatasi kecepatan download per client menjadi 500 KB/s

6. Kesimpulan

Dengan caching dan optimasi performa di Nginx, kamu bisa:

  1. Mempercepat load website dengan static content caching
  2. Mengurangi beban backend dengan proxy cache dan FastCGI cache
  3. Mengurangi ukuran transfer data dengan gzip compression
  4. Melindungi server dari traffic berlebihan dengan limit rate dan bandwidth throttling

Kombinasi teknik ini membuat website lebih cepat, stabil, dan aman untuk traffic tinggi.