Menambahkan Cloudflare: CDN & Security untuk Aplikasi Web
Menambahkan Cloudflare: CDN & Security untuk Aplikasi Web
Cloudflare adalah salah satu must-have tools untuk setiap web developer yang peduli dengan performance dan security. Di artikel ini, aku akan share cara setup Cloudflare dari nol sampai production-ready.
Why Cloudflare?
Cloudflare memberikan 3 benefit utama secara gratis:
- Global CDN - Konten di-cache di 200+ data centers worldwide
- DDoS Protection - Automatic protection dari serangan DDoS
- Free SSL/TLS - HTTPS gratis tanpa perlu beli certificate
Plus fitur bonus: Analytics, Page Rules, Workers, dan masih banyak lagi.
Setup Cloudflare: Step by Step
Step 1: Sign Up & Add Site
- Daftar di cloudflare.com
- Click "Add a Site"
- Masukkan domain kamu (contoh: )
example.com
- Pilih plan Free (sudah cukup powerful!)
Step 2: Update Nameservers
Cloudflare akan memberikan 2 nameserver addresses:
ns1.cloudflare.com
ns2.cloudflare.com
Update nameservers di domain registrar kamu (GoDaddy, Namecheap, dll):
- Login ke domain registrar
- Cari settings DNS/Nameservers
- Replace dengan nameserver Cloudflare
- Save changes
Note: Propagasi DNS bisa memakan waktu 24-48 jam, tapi biasanya selesai dalam beberapa jam.
Step 3: Konfigurasi DNS Records
Di Cloudflare dashboard ā DNS:
Type Name Content Proxy Status
A @ YOUR_SERVER_IP Proxied š
A www YOUR_SERVER_IP Proxied š
CNAME api api.yourdomain.com Proxied š
Proxied (š ) = Traffic lewat Cloudflare (CDN + Security)
DNS Only = Direct ke server
Step 4: Enable SSL/TLS
Di Cloudflare ā SSL/TLS:
- Set mode ke Full (strict) untuk maximum security
- Enable Always Use HTTPS
- Enable Automatic HTTPS Rewrites
Di server kamu, install SSL certificate. Untuk Nginx + Let's Encrypt:
sudo certbot --nginx -d example.com -d www.example.com
Step 5: Setup Caching Rules
Di Cloudflare ā Caching:
Browser Cache TTL: 4 hours
Caching Level: Standard
Create Page Rule untuk static assets:
URL Pattern: *.example.com/assets/*
Settings: Cache Level = Cache Everything, Edge Cache TTL = 1 month
Step 6: Security Settings
Di Cloudflare ā Security:
- Security Level: Medium (adjust based on traffic)
- Challenge Passage: 30 minutes
- Enable Bot Fight Mode (Free plan)
- Create Firewall Rules jika perlu block specific countries/IPs
Performance Improvements
Setelah setup Cloudflare di salah satu project client, aku measure improvement:
Before Cloudflare:
- TTFB (Jakarta): ~800ms
- TTFB (Singapore): ~200ms
- SSL/TLS: None
After Cloudflare:
- TTFB (Jakarta): ~120ms (85% faster!)
- TTFB (Singapore): ~45ms (77% faster!)
- SSL/TLS: A+ rating
- DDoS Protection: Active
Best Practices & Tips
1. Gunakan Page Rules Secara Bijak
Free plan dapat 3 page rules. Gunakan untuk:
- Cache static assets
- Redirect www ā non-www (atau sebaliknya)
- Bypass cache untuk admin panel
2. Monitor Analytics
Cloudflare Analytics menunjukkan:
- Traffic patterns
- Threats blocked
- Bandwidth saved via CDN
3. Setup Origin Server Rules
Di server kamu, whitelist Cloudflare IPs dan block direct access:
# Nginx config
set_real_ip_from 173.245.48.0/20;
set_real_ip_from 103.21.244.0/22;
# ... add all Cloudflare IP ranges
real_ip_header CF-Connecting-IP;
4. Enable Development Mode saat Deploy
Saat deploy changes, enable "Development Mode" (3 hours) untuk bypass cache dan test langsung.
Common Pitfalls
ā Jangan proxy API yang perlu real-time data
Untuk WebSocket atau real-time API, set DNS ke "DNS Only"
ā Jangan set Browser Cache TTL terlalu tinggi
Kalau kamu sering update content, gunakan nilai yang reasonable (4-8 hours)
ā Jangan lupa purge cache setelah deploy
Cloudflare ā Caching ā Purge Everything
Kesimpulan
Cloudflare adalah game-changer untuk performance dan security web apps. Dengan setup yang benar, kamu dapat:
- 85% faster load times via global CDN
- Free SSL/TLS certificates
- Automatic DDoS protection
- Better SEO karena faster site speed
Free plan sudah sangat powerful untuk sebagian besar use cases. Pro plan ($20/month) worth it jika kamu butuh lebih banyak Page Rules dan features advanced.
Next Steps:
- Setup Cloudflare untuk semua production apps
- Explore Cloudflare Workers untuk serverless functions
- Implement WAF (Web Application Firewall) rules
Questions? Hit me up! š