docs: Update troubleshooting CORS dengan langkah restart PHP-FPM yang jelas

This commit is contained in:
mwpn
2025-12-17 14:09:13 +07:00
parent e59bf9d981
commit 8b81de7fa6

View File

@@ -27,6 +27,7 @@ CORS_ALLOW_CREDENTIALS=true
``` ```
**Untuk Production (lebih aman):** **Untuk Production (lebih aman):**
```env ```env
# Ganti * dengan domain yang diizinkan # Ganti * dengan domain yang diizinkan
CORS_ALLOWED_ORIGINS=https://app.example.com,https://dashboard.example.com CORS_ALLOWED_ORIGINS=https://app.example.com,https://dashboard.example.com
@@ -63,6 +64,7 @@ curl -I -H "Origin: http://localhost:3000" https://api.btekno.cloud/health
``` ```
**Harus ada header:** **Harus ada header:**
``` ```
Access-Control-Allow-Origin: * Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS
@@ -86,46 +88,82 @@ curl -X OPTIONS \
Buka browser console (F12) dan jalankan: Buka browser console (F12) dan jalankan:
```javascript ```javascript
fetch('https://api.btekno.cloud/health', { fetch("https://api.btekno.cloud/health", {
method: 'GET', method: "GET",
headers: { headers: {
'Content-Type': 'application/json' "Content-Type": "application/json",
} },
}) })
.then(res => res.json()) .then((res) => res.json())
.then(data => { .then((data) => {
console.log('✅ CORS OK:', data); console.log("✅ CORS OK:", data);
}) })
.catch(err => { .catch((err) => {
console.error('❌ CORS Error:', err); console.error("❌ CORS Error:", err);
}); });
``` ```
## 🔍 Troubleshooting ## 🔍 Troubleshooting
### Masalah: CORS headers tidak muncul ### Masalah: CORS headers tidak muncul di HTTP response
**Jika `php bin/test_cors.php` sudah menunjukkan CORS headers muncul, tapi curl/HTTP tidak:**
**Solusi:** **Solusi:**
1. **Restart PHP-FPM** (PENTING!):
```bash
systemctl reload php-fpm-83 # Sesuaikan dengan PHP version
# Atau via aaPanel: Website -> PHP -> Service Management -> Reload
```
2. **Clear PHP Opcache**:
```bash
php -r "opcache_reset();"
# Atau via aaPanel: Website -> PHP -> Opcache -> Clear Cache
```
3. **Cek apakah code sudah ter-pull**:
```bash
cd /www/wwwroot/api.btekno.cloud/api
git pull origin main
composer dump-autoload --optimize
```
4. **Test lagi dengan curl**:
```bash
curl -v -H "Origin: http://localhost/retribusi" https://api.btekno.cloud/health
```
### Masalah: CORS headers tidak muncul sama sekali
**Jika `php bin/test_cors.php` juga tidak menunjukkan CORS headers:**
**Solusi:**
1. Pastikan `CorsMiddleware` sudah di-load di `src/Bootstrap/app.php` ✅ 1. Pastikan `CorsMiddleware` sudah di-load di `src/Bootstrap/app.php` ✅
2. Pastikan `.env` sudah ada konfigurasi CORS ✅ 2. Pastikan `.env` sudah ada konfigurasi CORS ✅
3. Restart PHP-FPM 3. Pastikan `composer dump-autoload --optimize` sudah dijalankan
4. Clear browser cache dan coba lagi 4. Check error log: `tail -f /www/wwwlogs/api.btekno.cloud.error.log`
### Masalah: Preflight OPTIONS return 404 ### Masalah: Preflight OPTIONS return 404
**Solusi:** **Solusi:**
- Pastikan routing middleware sudah di-load setelah CORS middleware ✅ - Pastikan routing middleware sudah di-load setelah CORS middleware ✅
- Cek nginx config: pastikan `try_files` mengarah ke `index.php` - Cek nginx config: pastikan `try_files` mengarah ke `index.php`
### Masalah: CORS hanya work untuk localhost ### Masalah: CORS hanya work untuk localhost
**Solusi:** **Solusi:**
- Edit `.env` dan set `CORS_ALLOWED_ORIGINS` dengan domain yang benar - Edit `.env` dan set `CORS_ALLOWED_ORIGINS` dengan domain yang benar
- Atau gunakan `*` untuk development (tidak recommended untuk production) - Atau gunakan `*` untuk development (tidak recommended untuk production)
## 📝 Catatan Penting ## 📝 Catatan Penting
1. **CORS middleware sudah otomatis handle:** 1. **CORS middleware sudah otomatis handle:**
- Preflight OPTIONS request - Preflight OPTIONS request
- CORS headers di semua response - CORS headers di semua response
- Localhost detection (http://localhost, http://127.0.0.1 dengan port apapun) - Localhost detection (http://localhost, http://127.0.0.1 dengan port apapun)
@@ -135,4 +173,3 @@ fetch('https://api.btekno.cloud/health', {
3. **Untuk production:** Ganti `CORS_ALLOWED_ORIGINS=*` dengan list domain yang spesifik 3. **Untuk production:** Ganti `CORS_ALLOWED_ORIGINS=*` dengan list domain yang spesifik
4. **CORS headers akan muncul di semua endpoint** - termasuk `/health`, `/auth`, `/retribusi`, dll 4. **CORS headers akan muncul di semua endpoint** - termasuk `/health`, `/auth`, `/retribusi`, dll