# API URL Fix for Nginx Proxy

## Problem
When accessing the application via nginx at `http://10.10.10.127:7000`, the frontend was making API calls to `http://10.10.10.127:3000/api/auth/login` instead of using the nginx proxy path `/api/auth/login`, resulting in 404 errors.

## Root Cause
The API URL detection logic was checking for `10.10.10.127` in the origin but not checking the port. When users accessed via port 3000 directly, it would use the wrong API URL.

## Solution
Updated the API URL detection to:
1. Check if accessing via nginx (port 7000 or no port)
2. Use relative path `/api` when behind nginx proxy
3. Use direct API URL when accessing locally on port 3000

## Changes Made

### `src/lib/api.ts` and `src/lib/dashboardHelpers.ts`
- Added proper port detection
- Uses `/api` when accessed via `10.10.10.127:7000`
- Uses direct API URL when accessed via `localhost:3000` or `10.10.10.127:3000`

## Testing

### Access via Nginx (Correct)
1. Open: `http://10.10.10.127:7000`
2. Check browser DevTools → Network tab
3. API calls should go to: `/api/auth/login` (relative path)
4. Should work correctly ✅

### Access Directly (Development)
1. Open: `http://localhost:3000` or `http://10.10.10.127:3000`
2. API calls should go to: `http://localhost:8001/auth/login`
3. Should work correctly ✅

## Verification

After rebuilding and restarting:

```bash
# Rebuild frontend
cd /home/plagis/workspace/plagis_aumentum/plagis-nextjs
npm run build

# Restart service
sudo systemctl restart plagis-nextjs

# Test
curl -I http://10.10.10.127:7000
curl -I http://10.10.10.127:7000/api/health
```

## Browser Console Check

When accessing via `http://10.10.10.127:7000`:
- ✅ API calls should show: `POST /api/auth/login`
- ❌ Should NOT show: `POST http://10.10.10.127:3000/api/auth/login`
- ❌ Should NOT show: `POST http://localhost:8001/auth/login`

## If Still Getting 404

1. **Clear browser cache:**
   - Hard refresh: Ctrl+Shift+R (or Cmd+Shift+R)
   - Or clear cache completely

2. **Check nginx is routing correctly:**
   ```bash
   curl -v http://10.10.10.127:7000/api/health
   ```

3. **Check backend is running:**
   ```bash
   curl http://localhost:8001/health
   ```

4. **Verify nginx config:**
   ```bash
   sudo nginx -t
   sudo systemctl status nginx
   ```

