Menyiapkan FastCGI Caching
Step 1: Setting Nginx main config
Sebelum mengaktifkan FastCGI Cache di servermu, pastikan kamu sudah menginstall LEMP Stack terlebih dahulu.
Setelah server siap, buka main config Nginx dengan editor favoritmu:
vim /etc/nginx/nginx.conf
Lalu tambahkan baris berikut didalam blok http {}
fastcgi_cache_path /var/cache/nginx/fastcgi_cache levels=1:2 keys_zone=phpcache:10m max_size=64m inactive=1h; fastcgi_cache_key "$scheme$request_method$host$request_uri";
Step 2: Setting vhost
Saatnya kita melakukan setting FastCGI Cache di vhost. Sekarang buka file konfigurasi vhost, kali ini saya akan melakukan setting di default vhost:
vim /etc/nginx/conf.d/default.conf
Lalu cari blok php-fpm location ~ \.php$ {}
dan tambahkan baris berikut:
fastcgi_cache phpcache; fastcgi_cache_valid 200 301 302 15m; add_header X-FastCGI-Cache $upstream_cache_status;
Nantinya untuk konfigurasi vhost akan tampak seperti ini:
server { listen 80; root /usr/share/nginx/html; index index.php index.html index.htm; location / { try_files $uri $uri/ =404; } error_page 404 /404.html; error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } location ~ \.php$ { try_files $uri =404; fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_cache phpcache; fastcgi_cache_valid 200 301 302 15m; add_header X-FastCGI-Cache $upstream_cache_status; include fastcgi_params; } }
Sekarang reload Nginx agar konfigurasi yang baru saja kita tambahkan tersebut dapat terbaca. Tetapi sebelum mereload Nginx ada baiknya untuk mengetes konfigurasi baru dengan menggunakan perintah:
nginx -t
Jika tidak ada error maka bisa langsung kita reload Nginx.
systemctl reload nginx
Tes FastCGI Cache
Untuk mengecek apakah FastCGI Cache sudah berjalan atau belum kita bisa membuat file info.php:
echo "<?php phpinfo(); ?>" > /usr/share/nginx/html/info.php
Lalu gunakan perintah curl untuk mengecek apakah FastCGI Cache sudah aktif atau belum. Contoh seperti dibawah ini:
# curl -I http://10.10.24.5/info.php HTTP/1.1 200 OK Server: nginx/1.14.0 Date: Wed, 12 Sep 2018 09:06:46 GMT Content-Type: text/html; charset=UTF-8 Connection: keep-alive X-FastCGI-Cache: HIT
Dari http response diatas, kita mendapatkan header X-FastCGI-Cache: HIT
yang artinya saat ini kamu sedang mengakses cache dari halaman info.php. Jika header yang muncul X-FastCGI-Cache: MISS
maka halaman belum di cache, dan kamu tinggal menjalankan perintah curl lagi untuk mengakses halaman yang sudah di cache.
Konten yang tidak perlu di cache
Tidak semua konten harus di cache, contoh seperti login session, sitemap, komentar, feed, ataupun POST request. Untuk menonaktifkan cache untuk konten-konten tersebut kita dapat menambahkan konfigurasi seperti dibawah pada blok server{}
.
set $skip_cache 0; if ($request_method = POST) { set $skip_cache 1; } if ($query_string != "") { set $skip_cache 1; } if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") { set $skip_cache 1; } if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") { set $skip_cache 1; }
Lalu pada blok location ~ \.php$ {}
tambahkan konfigurasi berikut:
fastcgi_cache_bypass $skip_cache; fastcgi_no_cache $skip_cache;
Nantinya untuk konfigurasi vhost akan seperti berikut:
server { listen 80; root /usr/share/nginx/html; index index.php index.html index.htm; set $skip_cache 0; if ($request_method = POST) { set $skip_cache 1; } if ($query_string != "") { set $skip_cache 1; } if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") { set $skip_cache 1; } if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") { set $skip_cache 1; } location / { try_files $uri $uri/ =404; } error_page 404 /404.html; error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } location ~ \.php$ { try_files $uri =404; fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_cache_bypass $skip_cache; fastcgi_no_cache $skip_cache; fastcgi_cache phpcache; fastcgi_cache_valid 200 301 302 15m; add_header X-FastCGI-Cache $upstream_cache_status; include fastcgi_params; } }
Simpan konfigurasi dan reload nginx
systemctl reload nginx