Cara Mengaktifkan FastCGI Cache di Nginx Server
Cara Mengaktifkan FastCGI Cache di Nginx Server - Pengguna webserver Nginx sebenarnya tidak perlu repot repot jika ingin mengkonfigurasi cache file statis di servernya karena di Nginx sudah ada yang namanya FastCGI Cache. Oke di tutorial ini saya akan membahas tentang mengaktifkan FastCGI cache secara singkat, padat, jelas.
Pertama, kita tentukan serverblock mana yang ingin kita aktifkan cache nya. Sebenarnya bisa langsung disatukan di nginx.conf, tapi saya sendiri memilih pasang satu-satu di serverblock agar lebih rapi.
Pertama, kita tentukan serverblock mana yang ingin kita aktifkan cache nya. Sebenarnya bisa langsung disatukan di nginx.conf, tapi saya sendiri memilih pasang satu-satu di serverblock agar lebih rapi.
nano /etc/nginx/sites-enabled/linuxsec.confLalu tambahkan kode berikut diluar block server{}.
fastcgi_cache_path /etc/nginx/cache/ levels=1:2 keys_zone=linuxseccache:10m max_size=512m inactive=1h; add_header X-Cache $upstream_cache_status;
- linuxseccache adalah nama memory zone nya. Sesuaikan sendiri
- max_size=512m maksudnya ukutan total cache 512MB. Sesuaikan sendiri dengan menghitung ketersediaan kapasitas RAM kalian.
- fastcgi_cache_path diatur di /etc/nginx/cache/. Direktori tersebut akan otomatis dibuat setelah konfigurasi Nginx direload.
fastcgi_cache_path /etc/nginx/cache/ levels=1:2 keys_zone=linuxseccache:10m max_size=512m inactive=1h; add_header X-Cache $upstream_cache_status; server { listen 80; server_name linuxsec.org; return 301 https://$server_name$request_uri; .....................Selanjutnya tambahkan baris kode berikut di block PHP-FPM nya ( location ~ \.php${} ).
fastcgi_cache linuxseccache; fastcgi_cache_key $scheme$host$request_uri$request_method; fastcgi_cache_valid 200 301 302 30s; fastcgi_cache_use_stale updating error timeout invalid_header http_500; fastcgi_pass_header Set-Cookie; fastcgi_pass_header Cookie; fastcgi_ignore_headers Cache-Control Expires Set-Cookie; fastcgi_cache_bypass $no_cache; fastcgi_no_cache $no_cache;Terakhir letakkan kode baris berikut di blok location / {}. Ini untuk memfilter mana laman yang akan di cache dan mana yang bukan.
#Cache everything by default set $no_cache 0; #Don't cache POST requests if ($request_method = POST) { set $no_cache 1; } #Don't cache if the URL contains a query string if ($query_string != "") { set $no_cache 1; } #Don't cache the following URLs if ($request_uri ~* "/(wp-admin/|wp-login.php)") { set $no_cache 1; } #Don't cache if there is a cookie called PHPSESSID if ($http_cookie = "PHPSESSID") { set $no_cache 1; }
Selanjutnya test apakah ada error di config nginx nya.
root@linuxsec:~# service nginx configtest * Testing nginx configuration [ OK ] root@linuxsec:~#Oke tidak ada kesalahan. Sekarang kita reload.
service nginx reloadLalu test apakah FastCGI cache bekerja
root@linuxsec:~# curl -X GET -I https://linuxsec.org/ HTTP/1.1 200 OK Date: Sat, 13 Oct 2018 22:48:01 GMT Content-Type: text/html; charset=UTF-8 Transfer-Encoding: chunked ............................ X-XSS-Protection: 1; mode=block; report=https://linuxsec.report-uri.com/r/d/xss/enforce Strict-Transport-Security: max-age=31536000; includeSubDomains X-Cache: HITX-Cache: HIT berarti caching bekerja. Atau kita coba cek apakah folder cache yang kita setup diatas sudah dibuat.
root@linuxsec:~# ls -lha /etc/nginx/cache/ total 72K drwx------ 18 web-user root 4.0K Oct 14 00:10 . drwxr-xr-x 8 root root 4.0K Oct 13 23:49 .. drwx------ 6 web-user web-user 4.0K Oct 14 00:49 0 drwx------ 5 web-user web-user 4.0K Oct 14 00:06 1 drwx------ 7 web-user web-user 4.0K Oct 14 00:14 2 drwx------ 5 web-user web-user 4.0K Oct 14 00:47 3 drwx------ 10 web-user web-user 4.0K Oct 14 00:47 4 drwx------ 5 web-user web-user 4.0K Oct 14 00:43 5 drwx------ 7 web-user web-user 4.0K Oct 14 00:27 6 drwx------ 5 web-user web-user 4.0K Oct 14 00:27 7 drwx------ 6 web-user web-user 4.0K Oct 14 00:43 8 drwx------ 7 web-user web-user 4.0K Oct 14 00:09 9 drwx------ 5 web-user web-user 4.0K Oct 14 00:36 a drwx------ 5 web-user web-user 4.0K Oct 14 00:06 b drwx------ 7 web-user web-user 4.0K Oct 14 00:47 c drwx------ 4 web-user web-user 4.0K Oct 14 00:39 d drwx------ 5 web-user web-user 4.0K Oct 14 00:17 e drwx------ 6 web-user web-user 4.0K Oct 14 00:18 fOke sekian tutorial kali ini, semoga bermanfaat, jika ada yang kurang jelas silahkan bertanya.
Posting Komentar untuk "Cara Mengaktifkan FastCGI Cache di Nginx Server"