# ✅ Coolify Port 80 Fix - The Right Solution!

## The Insight

You correctly identified that **Caddy is hardcoded to reverse proxy to port 80** on each container's IP address:

```json
{
  "handler": "reverse_proxy",
  "upstreams": [{ "dial": "10.0.3.3:80" }]  // ← Hardcoded port 80
}
```

Since each container gets its own IP in Docker network, **the simplest solution is to make our applications listen on port 80 inside the containers**.

## What We Changed

### 1. Backend - Now Listens on Port 80

**docker-compose.yml:**
```yaml
backend:
  expose:
    - "80"  # Changed from 8000
  command: uvicorn app.main:app --host 0.0.0.0 --port 80 --reload
```

**backend/Dockerfile:**
```dockerfile
EXPOSE 80  # Changed from 8000
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "80", "--reload"]
```

### 2. Frontend - Now Listens on Port 80

**docker-compose.yml:**
```yaml
frontend:
  expose:
    - "80"  # Changed from 5173
  command: npm run dev -- --host --port 80
```

**frontend/Dockerfile:**
```dockerfile
EXPOSE 80  # Changed from 5173
CMD ["npm", "run", "dev", "--", "--host", "--port", "80"]
```

### 3. Updated API URLs to Use Production Domains

**docker-compose.yml:**
```yaml
frontend:
  environment:
    - VITE_API_URL=https://h2h-backend.host.letsgetnashty.com
    - VITE_WS_URL=wss://h2h-backend.host.letsgetnashty.com
```

Changed from `localhost:8000` to actual production URLs.

## How This Works

**Old approach (didn't work):**
```
Internet → Caddy (port 80) → Backend container IP:80 ❌ (app listening on 8000)
                                                        Connection refused!
```

**New approach (works!):**
```
Internet → Caddy (port 80) → Backend container IP:80 ✅ (app listening on 80)
                                                        Success!
```

## Why This Is Better

1. **No host port binding** - No conflicts with Coolify's port 8000
2. **No special labels needed** - Caddy just works with its default port 80
3. **Simpler configuration** - Containers adapt to what Caddy expects
4. **Standard practice** - Most reverse proxy setups use port 80 internally

## Network Flow

```
User Browser
  ↓
https://h2h-backend.host.letsgetnashty.com (port 443)
  ↓
Coolify's Caddy (SSL termination)
  ↓
Reverse proxy to 10.0.3.3:80 (backend container)
  ↓
Uvicorn listening on 0.0.0.0:80 inside container
  ✅ Connection succeeds!
```

Same flow for frontend:
```
https://h2h-frontend.host.letsgetnashty.com (port 443)
  ↓
Caddy
  ↓
10.0.3.4:80 (frontend container)
  ↓
Vite dev server on port 80
  ✅ Connection succeeds!
```

## Deploy Now

```bash
git add docker-compose.yml backend/Dockerfile frontend/Dockerfile
git commit -m "Fix Coolify: listen on port 80 to match Caddy reverse proxy"
git push
```

Then redeploy in Coolify.

## Expected Results

After deployment, the Caddy logs should show:
```
✅ Successful proxy to 10.0.3.3:80 (status 200)
```

Instead of:
```
❌ dial tcp 10.0.3.3:80: connect: connection refused (status 502)
```

## Port Summary

| Service | Container Internal Port | External Access | Caddy Routes To |
|---------|------------------------|-----------------|-----------------|
| Backend | 80 | https://h2h-backend.host.letsgetnashty.com | 10.0.3.3:80 ✅ |
| Frontend | 80 | https://h2h-frontend.host.letsgetnashty.com | 10.0.3.4:80 ✅ |
| Coolify | 8000 (on host) | https://coolify.host.letsgetnashty.com | N/A |

**No conflicts** - Coolify runs on host port 8000, containers use port 80 internally.

## Local Development

For local development, you can create `docker-compose.dev.yml` with port mappings:

```yaml
services:
  backend:
    build: ./backend
    ports:
      - "8000:80"  # Map host 8000 → container 80
    volumes:
      - ./backend:/app
      - sqlite_data:/app/data

  frontend:
    build: ./frontend
    ports:
      - "5173:80"  # Map host 5173 → container 80
    volumes:
      - ./frontend:/app
      - /app/node_modules
    environment:
      - VITE_API_URL=http://localhost:8000
      - VITE_WS_URL=ws://localhost:8000
```

**Usage:**
```bash
# Local development
docker compose -f docker-compose.dev.yml up

# Coolify production
docker compose up  # Uses main docker-compose.yml with port 80
```

## Files Changed

✅ `docker-compose.yml` - Both services now expose and listen on port 80
✅ `backend/Dockerfile` - EXPOSE 80, CMD uses --port 80
✅ `frontend/Dockerfile` - EXPOSE 80, CMD uses --port 80
✅ Frontend environment variables now use production URLs

---

**This should work perfectly now!** Containers listen on port 80, matching what Caddy expects. 🎯
