2.8 KiB
2.8 KiB
🔧 Fix CORS Headers Tidak Muncul
⚠️ Masalah
CORS headers tidak muncul di HTTP response meskipun php bin/test_cors.php menunjukkan middleware sudah bekerja.
✅ Solusi Langkah Demi Langkah
1. Pull Code Terbaru di Server
cd /www/wwwroot/api.btekno.cloud/api
git pull origin main
composer dump-autoload --optimize
2. Verifikasi Code Sudah Ter-update
# Cek apakah CorsMiddleware ada
ls -la src/Middleware/CorsMiddleware.php
# Cek apakah Bootstrap sudah include CORS
grep -n "CorsMiddleware" src/Bootstrap/app.php
Harus muncul:
- File
src/Middleware/CorsMiddleware.phpada src/Bootstrap/app.phpberisiCorsMiddleware
3. Restart PHP-FPM (PENTING!)
# Cek PHP version
php -v
# Restart sesuai PHP version
systemctl reload php-fpm-83 # Untuk PHP 8.3
# atau
systemctl reload php-fpm-82 # Untuk PHP 8.2
Via aaPanel:
- Website → api.btekno.cloud → PHP → Service Management → Reload
4. Clear Opcache
# Via command
php -r "opcache_reset();"
Via aaPanel:
- Website → PHP → Opcache → Clear Cache
5. Test CORS
# Test dengan curl
curl -v -H "Origin: http://localhost/retribusi" https://api.btekno.cloud/health 2>&1 | grep -i "access-control"
Harus muncul:
< access-control-allow-origin: http://localhost/retribusi
< access-control-allow-credentials: true
< access-control-allow-methods: GET, POST, PUT, DELETE, OPTIONS
< access-control-allow-headers: Content-Type, Authorization, X-API-KEY, Accept, Origin
< access-control-max-age: 86400
6. Jika Masih Belum Muncul
Cek error log:
tail -f /www/wwwlogs/api.btekno.cloud.error.log
Test middleware langsung:
php bin/test_cors.php
Jika test script menunjukkan CORS headers muncul tapi HTTP tidak, kemungkinan:
- PHP-FPM belum di-restart ✅
- Opcache masih cache code lama ✅
- Nginx mungkin menghapus headers (jarang terjadi)
Cek nginx config:
# Pastikan tidak ada proxy_hide_header atau add_header yang override
grep -i "access-control" /www/server/panel/vhost/nginx/api.btekno.cloud.conf
Jika ada, hapus atau comment out.
🎯 Quick Fix Command (All-in-One)
cd /www/wwwroot/api.btekno.cloud/api && \
git pull origin main && \
composer dump-autoload --optimize && \
php -r "opcache_reset();" && \
systemctl reload php-fpm-83 && \
echo "✅ Done! Test dengan: curl -I -H 'Origin: http://localhost/retribusi' https://api.btekno.cloud/health"
📝 Checklist
- Code sudah di-pull (
git pull origin main) - Autoloader sudah di-regenerate (
composer dump-autoload --optimize) - PHP-FPM sudah di-restart (
systemctl reload php-fpm-XX) - Opcache sudah di-clear (
php -r "opcache_reset();") - Test CORS dengan curl menunjukkan headers muncul
- Test dari browser console tidak ada CORS error