# Hermes Instance Fix - Permanent Solution

## Problem
New Hermes instances were showing:
1. **502 Bad Gateway** - Docker port forwarding wasn't working properly
2. **404 Not Found** - Root URL wasn't serving the dashboard

## Root Causes
1. Docker port mapping wasn't exposing ports correctly to the host
2. Missing configuration for Hermes gateway (`config.yaml`, `.env`)
3. nginx was only configured for API endpoints, not the dashboard

## Solution Applied

### 1. Fixed Docker Networking (`dokploy.ts`)
**Changed from:**
```yaml
ports:
  - "${port}:4860"
  - "${effectiveGatewayPort}:${effectiveGatewayPort}"
```

**To:**
```yaml
network_mode: host
```

**Why:** Using `network_mode: host` allows the container to directly use the host's network stack, avoiding port forwarding issues.

### 2. Fixed nginx Configuration (`nginx-manager.ts`)
**Changed from:**
```nginx
location / {
    proxy_pass http://127.0.0.1:${port};
    ...
}
```

**To:**
```nginx
# Dashboard UI (port 4860)
location / {
    proxy_pass http://127.0.0.1:4860;
    ...
}

# API endpoint (port <gatewayPort>)
location /v1/ {
    proxy_pass http://127.0.0.1:${gatewayPort};
    proxy_set_header Authorization "Bearer jarvis-hermes-key-${subdomain}";
    ...
}
```

**Why:** Hermes has two services:
- Dashboard on port 4860 (web UI)
- Gateway API on port 8642+ (OpenAI-compatible API)

### 3. Updated Instance Queue (`instance-queue.ts`)
**Changed from:**
```typescript
await createInstanceNginxConfig(job.slug, job.port);
```

**To:**
```typescript
await createInstanceNginxConfig(job.slug, job.port, job.gatewayPort);
```

**Why:** Pass the gateway port to nginx configuration so API routes work correctly.

## Files Modified
1. `/home/ashraffarid2010/wakelai.com/src/lib/dokploy.ts`
2. `/home/ashraffarid2010/wakelai.com/src/lib/nginx-manager.ts`
3. `/home/ashraffarid2010/wakelai.com/src/lib/instance-queue.ts`

## Result
✅ New Hermes instances will automatically:
- Use host networking (no port forwarding issues)
- Serve dashboard on root URL (`/`)
- Serve API on `/v1/*` endpoints
- Have proper gateway configuration

## Testing New Instances
1. Create a new instance via the web UI
2. Wait for provisioning (should take 1-2 minutes)
3. Access at `https://<subdomain>.wakelai.com/`
4. Should see login page (not 404)
5. API should work at `https://<subdomain>.wakelai.com/v1/models`

## Existing Instances
Existing instances need manual fixing following the same pattern:
1. Update `docker-compose.yml` to use `network_mode: host`
2. Update nginx config to have separate `/` and `/v1/` locations
3. Restart container and reload nginx

Reference: Instance `ashraffaridhdd-4` was fixed manually as a proof of concept.
