176 lines
4.5 KiB
Markdown
176 lines
4.5 KiB
Markdown
# 🚀 Quick Deploy CORS Middleware
|
|
|
|
## ⚡ Langkah Cepat (Production Server)
|
|
|
|
```bash
|
|
# 1. Masuk ke folder project
|
|
cd /www/wwwroot/api.btekno.cloud/api
|
|
|
|
# 2. Pull code terbaru (yang sudah include CORS middleware)
|
|
git pull origin main
|
|
|
|
# 3. Regenerate autoloader (untuk load class CorsMiddleware)
|
|
composer dump-autoload --optimize
|
|
|
|
# 4. Edit .env dan tambahkan konfigurasi CORS
|
|
nano .env
|
|
```
|
|
|
|
**Tambahkan ke file `.env`:**
|
|
|
|
```env
|
|
# CORS Configuration
|
|
CORS_ALLOWED_ORIGINS=*
|
|
CORS_ALLOWED_METHODS=GET,POST,PUT,DELETE,OPTIONS
|
|
CORS_ALLOWED_HEADERS=Content-Type,Authorization,X-API-KEY,Accept,Origin
|
|
CORS_ALLOW_CREDENTIALS=true
|
|
```
|
|
|
|
**Untuk Production (lebih aman):**
|
|
|
|
```env
|
|
# Ganti * dengan domain yang diizinkan
|
|
CORS_ALLOWED_ORIGINS=https://app.example.com,https://dashboard.example.com
|
|
```
|
|
|
|
```bash
|
|
# 5. Restart PHP-FPM (PENTING! Harus di-restart setelah perubahan code)
|
|
# Via aaPanel: Website -> PHP -> Service Management -> Reload
|
|
# Atau via command (sesuaikan dengan PHP version):
|
|
systemctl reload php-fpm-83 # Untuk PHP 8.3
|
|
# systemctl reload php-fpm-82 # Untuk PHP 8.2
|
|
|
|
# 6. (Opsional) Clear PHP Opcache jika masih ada masalah
|
|
# Via aaPanel: Website -> PHP -> Opcache -> Clear Cache
|
|
# Atau via command:
|
|
php -r "opcache_reset();"
|
|
```
|
|
|
|
## ✅ Verifikasi CORS Aktif
|
|
|
|
### Test 0: Test CORS Middleware Secara Langsung (Recommended)
|
|
|
|
```bash
|
|
# Jalankan script test di server
|
|
php bin/test_cors.php
|
|
```
|
|
|
|
Script ini akan test CORS middleware secara langsung dan menunjukkan apakah headers sudah muncul.
|
|
|
|
### Test 1: Cek Response Headers
|
|
|
|
```bash
|
|
curl -I -H "Origin: http://localhost:3000" https://api.btekno.cloud/health
|
|
```
|
|
|
|
**Harus ada header:**
|
|
|
|
```
|
|
Access-Control-Allow-Origin: *
|
|
Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS
|
|
Access-Control-Allow-Headers: Content-Type, Authorization, X-API-KEY, Accept, Origin
|
|
```
|
|
|
|
### Test 2: Test Preflight (OPTIONS)
|
|
|
|
```bash
|
|
curl -X OPTIONS \
|
|
-H "Origin: http://localhost:3000" \
|
|
-H "Access-Control-Request-Method: POST" \
|
|
-H "Access-Control-Request-Headers: Content-Type,Authorization" \
|
|
https://api.btekno.cloud/auth/v1/login
|
|
```
|
|
|
|
**Harus return:** HTTP 204 No Content dengan CORS headers.
|
|
|
|
### Test 3: Test dari Browser Console
|
|
|
|
Buka browser console (F12) dan jalankan:
|
|
|
|
```javascript
|
|
fetch("https://api.btekno.cloud/health", {
|
|
method: "GET",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
})
|
|
.then((res) => res.json())
|
|
.then((data) => {
|
|
console.log("✅ CORS OK:", data);
|
|
})
|
|
.catch((err) => {
|
|
console.error("❌ CORS Error:", err);
|
|
});
|
|
```
|
|
|
|
## 🔍 Troubleshooting
|
|
|
|
### Masalah: CORS headers tidak muncul di HTTP response
|
|
|
|
**Jika `php bin/test_cors.php` sudah menunjukkan CORS headers muncul, tapi curl/HTTP tidak:**
|
|
|
|
**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` ✅
|
|
2. Pastikan `.env` sudah ada konfigurasi CORS ✅
|
|
3. Pastikan `composer dump-autoload --optimize` sudah dijalankan ✅
|
|
4. Check error log: `tail -f /www/wwwlogs/api.btekno.cloud.error.log`
|
|
|
|
### Masalah: Preflight OPTIONS return 404
|
|
|
|
**Solusi:**
|
|
|
|
- Pastikan routing middleware sudah di-load setelah CORS middleware ✅
|
|
- Cek nginx config: pastikan `try_files` mengarah ke `index.php`
|
|
|
|
### Masalah: CORS hanya work untuk localhost
|
|
|
|
**Solusi:**
|
|
|
|
- Edit `.env` dan set `CORS_ALLOWED_ORIGINS` dengan domain yang benar
|
|
- Atau gunakan `*` untuk development (tidak recommended untuk production)
|
|
|
|
## 📝 Catatan Penting
|
|
|
|
1. **CORS middleware sudah otomatis handle:**
|
|
|
|
- Preflight OPTIONS request
|
|
- CORS headers di semua response
|
|
- Localhost detection (http://localhost, http://127.0.0.1 dengan port apapun)
|
|
|
|
2. **Tidak perlu konfigurasi nginx untuk CORS** - semua di-handle di PHP middleware
|
|
|
|
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
|