From fe86e8b35b774984d60e2972c256b2bc9729c49a Mon Sep 17 00:00:00 2001 From: Deep Koluguri Date: Wed, 5 Nov 2025 20:24:49 -0500 Subject: [PATCH] fixed signup navigation --- ARCHITECTURE_OVERVIEW.md | 452 ++++++++++++++ FIX_502_ERROR.md | 453 ++++++++++++++ FIX_NOW_SERVICE_WORKER.md | 136 +++++ FIX_SERVICE_WORKER_CACHE.md | 483 +++++++++++++++ NGINX_PROXY_CACHE_FIX.md | 563 ++++++++++++++++++ QUICK_REFERENCE.md | 23 +- SIGNUP_NAVIGATION_UPDATE.md | 214 +++++++ clear-cache-and-deploy.sh | 1 + deploy-frontend-only.sh | 4 +- deploy-full.sh | 4 +- diagnose-502.sh | 155 +++++ fix-502-auto.sh | 145 +++++ force-cache-bust.sh | 2 +- .../features/auth/views/signup_screen.dart | 5 + luckychit/web/index.html | 11 + nginx-clear-cache.sh | 102 ++++ 16 files changed, 2745 insertions(+), 8 deletions(-) create mode 100644 ARCHITECTURE_OVERVIEW.md create mode 100644 FIX_502_ERROR.md create mode 100644 FIX_NOW_SERVICE_WORKER.md create mode 100644 FIX_SERVICE_WORKER_CACHE.md create mode 100644 NGINX_PROXY_CACHE_FIX.md create mode 100644 SIGNUP_NAVIGATION_UPDATE.md create mode 100644 diagnose-502.sh create mode 100644 fix-502-auto.sh create mode 100644 nginx-clear-cache.sh diff --git a/ARCHITECTURE_OVERVIEW.md b/ARCHITECTURE_OVERVIEW.md new file mode 100644 index 0000000..57996da --- /dev/null +++ b/ARCHITECTURE_OVERVIEW.md @@ -0,0 +1,452 @@ +# ๐Ÿ—๏ธ LuckyChit Production Architecture + +## Current Setup + +Your application runs across **2 LXC containers** with Cloudflare in front: + +``` +Internet + โ”‚ + โ–ผ +โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” +โ”‚ Cloudflare CDN โ”‚ +โ”‚ (SSL/TLS, DDoS Protection, CDN) โ”‚ +โ”‚ Domain: chitfund.deepteklabs.com โ”‚ +โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ + โ”‚ HTTPS/HTTP + โ–ผ +โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” +โ”‚ LXC 1: Nginx Proxy โ”‚ +โ”‚ - Reverse Proxy โ”‚ +โ”‚ - Ports: 80, 443 โ”‚ +โ”‚ - Can cache responses โš ๏ธ โ”‚ +โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ + โ”‚ HTTP (internal) + โ–ผ +โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” +โ”‚ LXC 2: Application Server โ”‚ +โ”‚ IP: 192.168.8.148 โ”‚ +โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ +โ”‚ โ”‚ PM2 Process Manager โ”‚ โ”‚ +โ”‚ โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค โ”‚ +โ”‚ โ”‚ Backend API โ”‚ โ”‚ +โ”‚ โ”‚ - Port: 3000 โ”‚ โ”‚ +โ”‚ โ”‚ - Node.js + Express โ”‚ โ”‚ +โ”‚ โ”‚ - Process: luckychit-api โ”‚ โ”‚ +โ”‚ โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค โ”‚ +โ”‚ โ”‚ Frontend โ”‚ โ”‚ +โ”‚ โ”‚ - Port: 8080 โ”‚ โ”‚ +โ”‚ โ”‚ - Flutter Web (Static) โ”‚ โ”‚ +โ”‚ โ”‚ - Process: luckychit-frontend โ”‚ โ”‚ +โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ +โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ +โ”‚ โ”‚ PostgreSQL Database โ”‚ โ”‚ +โ”‚ โ”‚ - Port: 5432 โ”‚ โ”‚ +โ”‚ โ”‚ - Database: luckychit โ”‚ โ”‚ +โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ +โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ +``` + +--- + +## Data Flow + +### User Request Flow + +``` +Browser + โ†’ Cloudflare (SSL/TLS, CDN) + โ†’ Nginx Proxy (LXC 1) + โ†’ PM2 Frontend (LXC 2:8080) + โ†’ Browser renders Flutter app +``` + +### API Request Flow + +``` +Browser + โ†’ Cloudflare + โ†’ Nginx Proxy (LXC 1) + โ†’ PM2 Backend API (LXC 2:3000) + โ†’ PostgreSQL Database + โ† Response + โ† JSON Response + โ† Proxied Response + โ† CDN Cached/Direct + โ† Response +``` + +--- + +## Cache Layers (Your Problem!) ๐ŸŽฏ + +### 1. Browser Cache +- **Location**: User's browser +- **Status**: โœ… Fixed (added cache-control meta tags) +- **Control**: `index.html` meta tags + +### 2. Cloudflare Cache +- **Location**: Cloudflare edge servers +- **Status**: โš ๏ธ Can cache +- **Control**: Cloudflare dashboard +- **Fix**: Purge cache in dashboard + +### 3. Nginx Proxy Cache โš ๏ธ **LIKELY CULPRIT!** +- **Location**: LXC 1 (nginx proxy container) +- **Status**: โŒ Probably caching! +- **Control**: `/etc/nginx/sites-available/chitfund` +- **Fix**: See `NGINX_PROXY_CACHE_FIX.md` + +### 4. PM2 Static Server +- **Location**: LXC 2 (backend container) +- **Status**: โœ… Serves fresh files from `build/web` +- **Control**: Rebuild with `flutter build web` + +--- + +## Port Mapping + +| Service | LXC | Port | Accessible From | Purpose | +|---------|-----|------|-----------------|---------| +| **Nginx** | LXC 1 | 80 | Internet (via Cloudflare) | HTTP Proxy | +| **Nginx** | LXC 1 | 443 | Internet (via Cloudflare) | HTTPS Proxy | +| **Backend API** | LXC 2 | 3000 | LXC 1 (nginx) | REST API | +| **Frontend** | LXC 2 | 8080 | LXC 1 (nginx) | Flutter Web App | +| **PostgreSQL** | LXC 2 | 5432 | Localhost only | Database | + +--- + +## Network Configuration + +### LXC 1 (Nginx Proxy) +```bash +# Public facing +Public IP: +Ports: 80, 443 (open to internet) + +# Internal +Internal IP: +Can reach: LXC 2 (192.168.8.148) +``` + +### LXC 2 (Application) +```bash +# Internal only +IP: 192.168.8.148 +Ports: 3000, 8080 (open to LXC 1) +Not directly accessible from internet +``` + +--- + +## Nginx Proxy Configuration + +Your nginx config on **LXC 1** should look like: + +```nginx +# /etc/nginx/sites-available/chitfund + +server { + listen 80; + server_name chitfund.deepteklabs.com; + + # Frontend (Flutter) + location / { + proxy_pass http://192.168.8.148:8080; + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection 'upgrade'; + proxy_set_header Host $host; + + # IMPORTANT: Disable caching! + proxy_no_cache 1; + proxy_cache_bypass 1; + add_header Cache-Control "no-cache, no-store, must-revalidate"; + } + + # Backend API + location /api { + proxy_pass http://192.168.8.148:3000; + proxy_http_version 1.1; + proxy_set_header Host $host; + proxy_no_cache 1; + } +} +``` + +--- + +## Deployment Flow + +### When You Deploy Code: + +```bash +# 1. Local Machine +git add . +git commit -m "Update" +git push origin prodnew + +# 2. LXC 2 (Application Server) +ssh luckychit@192.168.8.148 +cd /home/luckychit/apps/chitfund +./deploy-frontend-only.sh +# This rebuilds Flutter and restarts PM2 + +# 3. LXC 1 (Nginx Proxy) +ssh root@ +./nginx-clear-cache.sh +# This clears nginx cache + +# 4. Cloudflare +# Go to dashboard and purge cache (if needed) + +# 5. Browser +# Hard refresh: Ctrl + Shift + R +``` + +--- + +## Cache Clearing Procedure + +### Full Cache Clear (Use After Deployment): + +```bash +# 1. Clear nginx cache (LXC 1) +ssh root@ +sudo rm -rf /var/cache/nginx/* +sudo systemctl reload nginx + +# 2. Clear Cloudflare cache +# Login to Cloudflare dashboard +# Caching โ†’ Purge Everything + +# 3. Hard refresh browser +# Ctrl + Shift + R (Windows) +# Cmd + Shift + R (Mac) +``` + +--- + +## Troubleshooting Guide + +### Problem: Changes Not Showing + +**Check each cache layer:** + +1. **Browser**: Hard refresh (`Ctrl + Shift + R`) +2. **Cloudflare**: Purge cache in dashboard +3. **Nginx**: Clear cache on LXC 1 +4. **PM2**: Verify build is fresh on LXC 2 + +### Problem: 502 Bad Gateway + +**Check connectivity:** + +```bash +# On LXC 1 (nginx), test backend: +curl http://192.168.8.148:3000/health +curl http://192.168.8.148:8080 + +# If these fail: +# - PM2 is down on LXC 2 +# - Firewall blocking between LXCs +# - Network routing issue +``` + +### Problem: Backend Not Responding + +**Check PM2 on LXC 2:** + +```bash +ssh luckychit@192.168.8.148 +pm2 status +pm2 restart all +``` + +--- + +## Security Considerations + +### Current Setup: +- โœ… Cloudflare provides DDoS protection +- โœ… SSL/TLS handled by Cloudflare +- โœ… Backend LXC not directly exposed to internet +- โœ… Only nginx proxy is public-facing +- โš ๏ธ No SSL between nginx and backend (OK for internal network) + +### Recommendations: +- โœ… Keep backend LXC internal-only +- โœ… Use firewall rules between LXCs +- โœ… Limit nginx proxy access to only necessary ports +- โš ๏ธ Consider fail2ban on nginx proxy +- โš ๏ธ Consider firewall rate limiting + +--- + +## Firewall Configuration + +### LXC 1 (Nginx Proxy) +```bash +# Allow HTTP/HTTPS from internet +sudo ufw allow 80/tcp +sudo ufw allow 443/tcp +sudo ufw allow 22/tcp # SSH +sudo ufw enable +``` + +### LXC 2 (Application) +```bash +# Allow only from nginx proxy +sudo ufw allow from to any port 3000 +sudo ufw allow from to any port 8080 +sudo ufw allow 22/tcp # SSH +sudo ufw enable +``` + +--- + +## Monitoring & Logs + +### LXC 1 (Nginx) +```bash +# Access logs +sudo tail -f /var/log/nginx/access.log + +# Error logs +sudo tail -f /var/log/nginx/error.log + +# Check status +sudo systemctl status nginx +``` + +### LXC 2 (Application) +```bash +# PM2 logs +pm2 logs + +# PM2 status +pm2 status + +# PM2 monitoring +pm2 monit +``` + +--- + +## Backup Strategy + +### What to Backup: + +1. **Database (LXC 2)** + ```bash + pg_dump -U luckychit luckychit > backup.sql + ``` + +2. **Nginx Config (LXC 1)** + ```bash + tar -czf nginx-config.tar.gz /etc/nginx/ + ``` + +3. **Application Code (LXC 2)** + ```bash + cd /home/luckychit/apps/chitfund + git status # Should be clean + # Code is in git, no backup needed + ``` + +4. **PM2 Config (LXC 2)** + ```bash + pm2 save + # Saves to ~/.pm2/dump.pm2 + ``` + +--- + +## Performance Optimization + +### Current Performance: + +``` +User โ†’ Cloudflare (CDN, <50ms globally) + โ†’ Nginx Proxy (LXC 1, ~1ms) + โ†’ PM2 App (LXC 2, ~10ms) + โ†’ PostgreSQL (~5ms) +``` + +**Total latency**: ~16ms + network time + +### Recommended Optimizations: + +1. **Enable nginx caching for static assets only** + - Cache images, CSS, JS (7 days) + - Don't cache HTML, JSON + +2. **Use Cloudflare caching selectively** + - Cache static assets + - Bypass for API and HTML + +3. **Enable gzip compression in nginx** + ```nginx + gzip on; + gzip_types text/plain text/css application/json application/javascript; + ``` + +4. **Use PM2 cluster mode** (already fixed in ecosystem.config.js) + +--- + +## Quick Reference + +### SSH Access: +```bash +# Nginx Proxy (LXC 1) +ssh root@ + +# Application Server (LXC 2) +ssh luckychit@192.168.8.148 +``` + +### Clear All Caches: +```bash +# 1. Nginx (LXC 1) +sudo rm -rf /var/cache/nginx/* && sudo systemctl reload nginx + +# 2. PM2 rebuild (LXC 2) +cd /home/luckychit/apps/chitfund && ./force-cache-bust.sh + +# 3. Cloudflare +# Dashboard โ†’ Caching โ†’ Purge Everything + +# 4. Browser +# Ctrl + Shift + R +``` + +### Check Everything: +```bash +# Nginx (LXC 1) +systemctl status nginx +curl http://192.168.8.148:3000/health + +# PM2 (LXC 2) +pm2 status +pm2 logs --lines 20 +``` + +--- + +## Summary + +**Your Issue**: Changes not showing because of **nginx proxy caching** + +**Solution**: +1. Clear nginx cache on LXC 1 +2. Add `proxy_no_cache` to nginx config +3. Clear Cloudflare cache +4. Hard refresh browser + +**See**: `NGINX_PROXY_CACHE_FIX.md` for detailed nginx fix + +--- + +**Architecture documented! Your caching issue is in the nginx proxy layer.** ๐ŸŽฏ + diff --git a/FIX_502_ERROR.md b/FIX_502_ERROR.md new file mode 100644 index 0000000..853e936 --- /dev/null +++ b/FIX_502_ERROR.md @@ -0,0 +1,453 @@ +# ๐Ÿšจ Fix 502 Bad Gateway Error - Cloudflare + +## Error Details +- **Domain**: chitfund.deepteklabs.com +- **Error**: 502 Bad Gateway +- **Time**: 2025-11-06 00:26:51 UTC +- **Status**: Browser โœ… | Cloudflare โœ… | Origin Server โŒ + +--- + +## ๐ŸŽฏ What This Means + +โœ… **Your browser** is working +โœ… **Cloudflare** (CDN) is working +โŒ **Your origin server** is NOT responding or PM2 processes are down + +--- + +## ๐Ÿ” IMMEDIATE DIAGNOSIS + +SSH into your server and run these checks: + +```bash +ssh luckychit@192.168.8.148 + +# 1. Check if PM2 processes are running +pm2 status + +# 2. Check if ports are listening +netstat -tulpn | grep -E '(3000|8080)' + +# 3. Check if server is responding locally +curl http://localhost:3000/health +curl http://localhost:8080 + +# 4. Check PM2 logs for errors +pm2 logs --lines 50 +``` + +--- + +## ๐Ÿš€ QUICK FIXES (Try in Order) + +### Fix 1: Restart PM2 Processes +```bash +pm2 restart all +pm2 status +``` + +### Fix 2: PM2 Processes Are Down - Start Them +```bash +pm2 start luckychit-api +pm2 start luckychit-frontend +pm2 status +``` + +### Fix 3: PM2 Lost Configuration - Recreate +```bash +cd /home/luckychit/apps/chitfund + +# Start backend +cd backend +pm2 start src/server.js --name luckychit-api + +# Start frontend +cd ../luckychit +pm2 serve build/web 8080 --name luckychit-frontend --spa + +# Save configuration +pm2 save +pm2 status +``` + +### Fix 4: Server Rebooted - Restore PM2 +```bash +pm2 resurrect +# or +pm2 startup +pm2 save +``` + +### Fix 5: Firewall Blocking Cloudflare +```bash +sudo ufw status +sudo ufw allow 3000/tcp +sudo ufw allow 8080/tcp +sudo ufw allow 80/tcp +sudo ufw allow 443/tcp +sudo ufw reload +``` + +--- + +## ๐Ÿ”ง DETAILED TROUBLESHOOTING + +### Step 1: Check PM2 Status + +```bash +pm2 status +``` + +**Expected Output:** +``` +โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” +โ”‚ App name โ”‚ id โ”‚ status โ”‚ PID โ”‚ memory โ”‚ +โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค +โ”‚ luckychit-api โ”‚ 0 โ”‚ online โ”‚ 1234 โ”‚ 50 MB โ”‚ +โ”‚ luckychit-frontend โ”‚ 1 โ”‚ online โ”‚ 5678 โ”‚ 30 MB โ”‚ +โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ +``` + +**If status is "stopped" or "errored":** +```bash +pm2 logs --lines 100 # Check for errors +pm2 restart all # Restart processes +``` + +**If processes don't exist:** +```bash +# Recreate them (see Fix 3 above) +``` + +--- + +### Step 2: Check if Ports are Listening + +```bash +netstat -tulpn | grep -E '(3000|8080)' +``` + +**Expected Output:** +``` +tcp6 0 0 :::3000 :::* LISTEN 1234/node +tcp6 0 0 :::8080 :::* LISTEN 5678/node +``` + +**If nothing shows up:** +- Processes are not running +- Start them with Fix 3 + +--- + +### Step 3: Test Local Connectivity + +```bash +# Test backend +curl http://localhost:3000/health +# Expected: {"status":"ok"...} + +# Test frontend +curl http://localhost:8080 +# Expected: HTML content + +# Test from server IP +curl http://192.168.8.148:3000/health +curl http://192.168.8.148:8080 +``` + +**If these work but domain doesn't:** +- Issue is with Cloudflare configuration +- See Cloudflare section below + +--- + +### Step 4: Check PM2 Logs + +```bash +pm2 logs luckychit-api --lines 50 +pm2 logs luckychit-frontend --lines 50 +``` + +**Look for errors like:** +- Database connection errors +- Port already in use +- Module not found +- Crash/exception messages + +--- + +### Step 5: Check Firewall + +```bash +sudo ufw status numbered +``` + +**You should see:** +``` +[1] 22/tcp ALLOW IN Anywhere +[2] 3000/tcp ALLOW IN Anywhere +[3] 8080/tcp ALLOW IN Anywhere +[4] 80/tcp ALLOW IN Anywhere +[5] 443/tcp ALLOW IN Anywhere +``` + +**If ports are missing:** +```bash +sudo ufw allow 3000/tcp +sudo ufw allow 8080/tcp +sudo ufw reload +``` + +--- + +### Step 6: Check Server Resources + +```bash +# Check disk space +df -h +# If disk is full (100%), clear logs + +# Check memory +free -h +# If memory is full, restart processes + +# Check CPU +top +# Press 'q' to quit +``` + +--- + +## โ˜๏ธ CLOUDFLARE CONFIGURATION + +### Check DNS Settings + +In Cloudflare dashboard: +1. Go to DNS settings +2. Verify A record points to: **192.168.8.148** +3. Check "Proxy status": + - ๐ŸŸ  **Proxied** (through Cloudflare) - Recommended + - โšช **DNS only** (direct) - Bypass Cloudflare + +### Check Origin Rules + +If using Cloudflare Tunnels or custom ports: +1. Go to **Network** tab +2. Check origin server settings +3. Make sure it points to correct IP and port + +### SSL/TLS Settings + +1. Go to **SSL/TLS** tab +2. Set to **Flexible** (if no SSL on origin) OR +3. Set to **Full** (if origin has SSL) + +**For your setup (no nginx), use "Flexible"** + +--- + +## ๐Ÿ†˜ EMERGENCY RECOVERY + +### Complete PM2 Reset + +```bash +# Kill all PM2 processes +pm2 kill + +# Navigate to project +cd /home/luckychit/apps/chitfund + +# Start backend +cd backend +pm2 start src/server.js --name luckychit-api + +# Verify backend +curl http://localhost:3000/health + +# Start frontend +cd ../luckychit +pm2 serve build/web 8080 --name luckychit-frontend --spa + +# Verify frontend +curl http://localhost:8080 + +# Save configuration +pm2 save + +# Setup auto-start +pm2 startup systemd -u luckychit --hp /home/luckychit +# (Run the command it outputs) +pm2 save + +# Check status +pm2 status +``` + +### Server Reboot (Last Resort) + +```bash +# Save PM2 list first +pm2 save + +# Reboot +sudo reboot + +# After reboot, SSH back in +ssh luckychit@192.168.8.148 + +# Check if PM2 auto-started +pm2 status + +# If not: +pm2 resurrect +``` + +--- + +## ๐Ÿ” COMMON CAUSES + +### 1. Server Rebooted +**Symptom**: PM2 processes not running +**Fix**: `pm2 resurrect` or restart manually + +### 2. Out of Memory +**Symptom**: Processes killed by system +**Fix**: `free -h` to check, restart processes + +### 3. Database Down +**Symptom**: Backend crashes on startup +**Fix**: Check PostgreSQL: `sudo systemctl status postgresql` + +### 4. Port Conflict +**Symptom**: "Port already in use" in logs +**Fix**: Kill process using port or change port + +### 5. Firewall Rule Changed +**Symptom**: Can't connect to ports +**Fix**: Re-add firewall rules + +### 6. Cloudflare Configuration Changed +**Symptom**: 502 only on domain, not IP +**Fix**: Check Cloudflare DNS and SSL settings + +--- + +## ๐Ÿ“‹ POST-FIX CHECKLIST + +After fixing, verify: + +- [ ] `pm2 status` shows both processes **online** +- [ ] `curl http://localhost:3000/health` returns JSON +- [ ] `curl http://localhost:8080` returns HTML +- [ ] `curl http://192.168.8.148:3000/health` works +- [ ] `curl http://192.168.8.148:8080` works +- [ ] Domain `chitfund.deepteklabs.com` loads in browser +- [ ] No errors in `pm2 logs` +- [ ] Login works +- [ ] `pm2 save` executed (for persistence) + +--- + +## ๐ŸŽฏ PREVENTION + +### Setup Monitoring + +```bash +# Check PM2 status regularly +pm2 status + +# Setup auto-restart on crash (already enabled) +pm2 start src/server.js --name luckychit-api --watch false --autorestart + +# Setup uptime monitoring +# Consider using: UptimeRobot, Pingdom, or StatusCake +``` + +### Setup Alerts + +Consider PM2 Plus for monitoring: +```bash +pm2 plus +# Follow setup instructions +``` + +--- + +## ๐Ÿ“ž DIAGNOSTIC SCRIPT + +Save this as `diagnose-502.sh`: + +```bash +#!/bin/bash +echo "๐Ÿ” Diagnosing 502 Error" +echo "=======================" +echo "" + +echo "1. PM2 Status:" +pm2 status + +echo "" +echo "2. Listening Ports:" +netstat -tulpn | grep -E '(3000|8080)' + +echo "" +echo "3. Test Backend (localhost):" +curl -s http://localhost:3000/health || echo "โŒ Backend not responding" + +echo "" +echo "4. Test Frontend (localhost):" +curl -s http://localhost:8080 | head -n 5 || echo "โŒ Frontend not responding" + +echo "" +echo "5. Test Backend (IP):" +curl -s http://192.168.8.148:3000/health || echo "โŒ Backend not responding on IP" + +echo "" +echo "6. Firewall Status:" +sudo ufw status | grep -E '(3000|8080)' + +echo "" +echo "7. Disk Space:" +df -h | grep -E '(Filesystem|/$)' + +echo "" +echo "8. Memory Usage:" +free -h + +echo "" +echo "9. Recent PM2 Logs (Errors):" +pm2 logs --err --lines 20 --nostream + +echo "" +echo "โœ… Diagnosis complete!" +``` + +Run it: +```bash +chmod +x diagnose-502.sh +./diagnose-502.sh +``` + +--- + +## ๐ŸŽ‰ SUMMARY + +**502 Bad Gateway = Origin server not responding** + +**Quick Fix:** +1. SSH into server +2. Run `pm2 status` +3. If down: `pm2 restart all` +4. If missing: Recreate processes (see Fix 3) +5. Test: `curl http://localhost:3000/health` +6. Save: `pm2 save` + +**Most Common Cause**: Server rebooted and PM2 didn't auto-start + +**Best Fix**: Run `pm2 resurrect` or restart manually + +--- + +Need help? Run the diagnostic script and share the output! + diff --git a/FIX_NOW_SERVICE_WORKER.md b/FIX_NOW_SERVICE_WORKER.md new file mode 100644 index 0000000..e1cc3bb --- /dev/null +++ b/FIX_NOW_SERVICE_WORKER.md @@ -0,0 +1,136 @@ +# ๐Ÿšจ FIX SERVICE WORKER ERROR NOW + +## Your Error: +``` +Loading from existing service worker. +main.dart.js:5658 Uncaught Error +``` + +**Cause**: Service worker is caching old/broken JavaScript code! + +--- + +## โšก IMMEDIATE FIX (3 Steps) + +### Step 1: Clear Service Worker in Browser + +#### Method A: Using DevTools (Recommended) +1. Press **F12** to open DevTools +2. Click **Application** tab (top) +3. Click **Service Workers** (left sidebar) +4. Find service worker for `chitfund.deepteklabs.com` +5. Click **Unregister** +6. Close DevTools + +#### Method B: Clear All Site Data +1. Press **F12** +2. Go to **Application** tab +3. Click **Clear storage** (left sidebar) +4. Check all boxes +5. Click **Clear site data** + +#### Method C: Test in Incognito (Quick) +``` +Windows: Ctrl + Shift + N +Mac: Cmd + Shift + N +``` + +### Step 2: Deploy Fixed Version + +```bash +# On your dev machine - push changes +git add . +git commit -m "Fix service worker caching issue" +git push origin prodnew + +# SSH to production +ssh luckychit@192.168.8.148 +cd /home/luckychit/apps/chitfund +git pull origin prodnew + +# Rebuild (service worker now disabled) +cd luckychit +flutter clean +flutter pub get +flutter build web --release --pwa-strategy=none +pm2 restart luckychit-frontend +``` + +### Step 3: Hard Refresh Browser + +``` +Windows: Ctrl + Shift + R +Mac: Cmd + Shift + R +``` + +--- + +## โœ… What I Fixed + +1. **Updated `index.html`** - Now auto-clears service workers on load +2. **Updated all deployment scripts** - Now build with `--pwa-strategy=none` (disables service worker) +3. **Updated documentation** - Added service worker clearing to guides + +--- + +## ๐ŸŽฏ Expected Result + +After these steps, your browser console should show: +- โœ… No "Loading from existing service worker" message +- โœ… No "Uncaught Error" +- โœ… App loads cleanly +- โœ… Login screen appears + +--- + +## ๐Ÿ“‹ Quick Checklist + +- [ ] Clear service worker in browser (F12 โ†’ Application โ†’ Unregister) +- [ ] Push updated code to git +- [ ] Pull on production server +- [ ] Rebuild with `--pwa-strategy=none` +- [ ] Restart PM2 +- [ ] Hard refresh browser +- [ ] Verify no errors in console + +--- + +## ๐Ÿ’ก Why This Happened + +``` +Old deployment โ†’ Service Worker caches JS + โ†“ +New deployment โ†’ New HTML served + โ†“ +Service Worker โ†’ Serves OLD cached JS + โ†“ +Old JS + New HTML = ERROR! โŒ +``` + +--- + +## ๐Ÿ”„ From Now On + +All your deployment scripts now: +1. Build WITHOUT service worker (`--pwa-strategy=none`) +2. Auto-clear any existing service workers +3. Force cache refresh with timestamps + +**No more service worker issues!** ๐ŸŽ‰ + +--- + +## ๐Ÿ“ž Still Seeing Errors? + +If errors persist after all steps: + +1. **Check console** - Any new errors? +2. **Test in incognito** - Completely clean slate +3. **Clear Cloudflare cache** - Dashboard โ†’ Purge Everything +4. **Clear nginx cache** - On proxy LXC: `sudo rm -rf /var/cache/nginx/*` +5. **Try different browser** - Eliminate browser-specific issues + +--- + +**Run the 3 steps above to fix your service worker error!** ๐Ÿš€ + diff --git a/FIX_SERVICE_WORKER_CACHE.md b/FIX_SERVICE_WORKER_CACHE.md new file mode 100644 index 0000000..5e2aa68 --- /dev/null +++ b/FIX_SERVICE_WORKER_CACHE.md @@ -0,0 +1,483 @@ +# ๐Ÿ”ง Fix Service Worker Cache Issue - Flutter Web + +## The Problem + +Your browser console shows: +``` +Loading from existing service worker. +Service worker already active. +main.dart.js:5658 Uncaught Error +``` + +This means the **service worker is caching old/broken JavaScript code** and serving it instead of the new version! + +--- + +## โšก IMMEDIATE FIX (User Side) + +### Option 1: Clear Service Worker (Recommended) + +1. **Open DevTools** (F12) +2. Go to **Application** tab (Chrome) or **Storage** tab (Firefox) +3. Click **Service Workers** (left sidebar) +4. Find your service worker for `chitfund.deepteklabs.com` +5. Click **Unregister** +6. **Hard refresh**: `Ctrl + Shift + R` + +### Option 2: Clear All Site Data + +1. Open DevTools (F12) +2. Application tab โ†’ **Clear storage** (left sidebar) +3. Check all boxes (Cookies, Cache, Storage, Service workers) +4. Click **Clear site data** +5. Close DevTools +6. Refresh page + +### Option 3: Incognito Mode (Quick Test) + +``` +Ctrl + Shift + N (Windows/Linux) +Cmd + Shift + N (Mac) +``` +No service worker cache in incognito! + +--- + +## ๐Ÿ› ๏ธ PERMANENT FIX (Developer Side) + +The service worker needs to be disabled or properly managed during deployments. + +### Solution 1: Disable Service Worker (Simplest) + +Edit `luckychit/web/index.html`: + +```html + + + + + + +``` + +### Solution 2: Force Service Worker Update + +Edit `luckychit/web/flutter_bootstrap.js` or add to `index.html`: + +```html + +``` + +### Solution 3: Build Without Service Worker + +Build Flutter without service worker: + +```bash +flutter build web --release --pwa-strategy=none +``` + +This completely disables the service worker. + +--- + +## ๐Ÿ”ง Fix for Current Production + +### Step 1: Update index.html + +```bash +# On your dev machine +cd luckychit +nano web/index.html +``` + +Add this script before ``: + +```html + + + + + + + + +``` + +### Step 2: Rebuild and Deploy + +```bash +# Commit changes +git add web/index.html +git commit -m "Fix service worker caching issue" +git push origin prodnew + +# SSH to production +ssh luckychit@192.168.8.148 +cd /home/luckychit/apps/chitfund + +# Pull and rebuild +git pull origin prodnew +cd luckychit +flutter clean +flutter pub get +flutter build web --release --pwa-strategy=none +pm2 restart luckychit-frontend + +# Also clear nginx cache +ssh root@ +sudo rm -rf /var/cache/nginx/* +sudo systemctl reload nginx +``` + +### Step 3: Tell Users to Clear Service Worker + +Create an announcement: +``` +โš ๏ธ Important: Clear your browser cache! +1. Press F12 to open DevTools +2. Go to Application tab +3. Click Service Workers +4. Click "Unregister" for chitfund.deepteklabs.com +5. Hard refresh: Ctrl + Shift + R +``` + +--- + +## ๐Ÿ” Understanding the Error + +### What Happened: + +``` +1. Old version deployed with service worker +2. Service worker cached main.dart.js (JavaScript) +3. You deployed new version +4. Browser loads new HTML +5. Service worker serves OLD cached JavaScript +6. Old JS + New HTML = Error! +``` + +### The Error Chain: + +```javascript +main.dart.js:5658 Uncaught Error + at Object.l (main.dart.js:3396:20) +``` + +This is a JavaScript error in compiled Dart code. Usually means: +- **Version mismatch** between HTML and cached JS +- **Corrupted cache** from incomplete deployment +- **Service worker** serving stale files + +--- + +## ๐Ÿ“‹ Complete Fix Procedure + +### On Your Dev Machine: + +```bash +cd /home/user/workspace/chitfund/luckychit + +# Add service worker clearing script +nano web/index.html +# (Add the script shown above) + +# Commit and push +git add web/index.html +git commit -m "Add service worker clearing on load" +git push origin prodnew +``` + +### On Production Server (192.168.8.148): + +```bash +cd /home/luckychit/apps/chitfund + +# Pull latest +git pull origin prodnew + +# Rebuild Flutter WITHOUT service worker +cd luckychit +flutter clean +rm -rf .dart_tool build +flutter pub get +flutter build web --release --pwa-strategy=none + +# Restart PM2 +pm2 restart luckychit-frontend + +# Verify +pm2 logs luckychit-frontend --lines 20 +``` + +### On Nginx Proxy LXC: + +```bash +# Clear nginx cache +sudo rm -rf /var/cache/nginx/* +sudo systemctl reload nginx +``` + +### On Cloudflare: + +1. Login to dashboard +2. Go to **Caching** +3. Click **Purge Everything** + +### In Browser: + +1. Open DevTools (F12) +2. Application โ†’ Service Workers โ†’ Unregister all +3. Application โ†’ Clear storage โ†’ Clear site data +4. Hard refresh: `Ctrl + Shift + R` +5. Close and reopen browser + +--- + +## ๐ŸŽฏ Update Your Deployment Scripts + +### Modify `deploy-frontend-only.sh`: + +```bash +# Add --pwa-strategy=none to disable service worker +flutter build web --release --web-renderer html --pwa-strategy=none --build-number=$BUILD_NUMBER +``` + +### Modify `deploy-full.sh`: + +```bash +# Same change +flutter build web --release --web-renderer html --pwa-strategy=none --build-number=$BUILD_NUMBER +``` + +--- + +## ๐Ÿงช Verify It's Fixed + +### Check Browser Console: + +After refresh, you should NOT see: +``` +โŒ Loading from existing service worker +โŒ Service worker already active +โŒ Uncaught Error +``` + +You SHOULD see: +``` +โœ… No stored auth data found (normal) +โœ… App loads without errors +โœ… Login screen appears +``` + +### Check Service Workers: + +``` +F12 โ†’ Application โ†’ Service Workers +Should be empty or show "No service workers" +``` + +### Check Caches: + +``` +F12 โ†’ Application โ†’ Cache Storage +Should be empty or minimal +``` + +--- + +## ๐ŸŽ“ Why Service Workers Are Problematic + +### For Static Sites: +โœ… **Good**: Offline support, faster loading +โœ… **Good**: PWA functionality + +### For Frequently Updated Apps: +โŒ **Bad**: Caches old code +โŒ **Bad**: Hard to invalidate +โŒ **Bad**: Users see old version +โŒ **Bad**: Version conflicts cause errors + +### Recommendation: +**Disable service workers** until your app is stable and updates are infrequent. + +--- + +## ๐Ÿ›ก๏ธ Prevention + +### 1. Build Without Service Worker + +Always use: +```bash +flutter build web --release --pwa-strategy=none +``` + +### 2. Add Clear Script to index.html + +Force clear service workers on every load (shown above). + +### 3. Proper Cache Headers + +Already done in `index.html`: +```html + +``` + +### 4. Version Your Builds + +Already done in deployment scripts: +```bash +--build-number=$(date +%s) +``` + +### 5. Update Deployment Documentation + +Add "Clear service worker" step to deployment checklist. + +--- + +## ๐Ÿ“ฑ Mobile Browsers + +Service worker issues are WORSE on mobile! + +### iOS Safari: +``` +Settings โ†’ Safari โ†’ Advanced โ†’ Website Data +Find your site โ†’ Swipe left โ†’ Delete +``` + +### Android Chrome: +``` +Settings โ†’ Site Settings โ†’ chitfund.deepteklabs.com +Clear & reset +``` + +### Or Use Private Mode: +Always test updates in private/incognito first! + +--- + +## ๐Ÿ” Debug Service Worker + +### Check Registration: + +Open console and run: +```javascript +navigator.serviceWorker.getRegistrations().then(registrations => { + console.log('Service Workers:', registrations); + registrations.forEach(r => console.log('Scope:', r.scope)); +}); +``` + +### Check Caches: + +```javascript +caches.keys().then(keys => { + console.log('Cache Keys:', keys); + keys.forEach(key => { + caches.open(key).then(cache => { + cache.keys().then(requests => { + console.log(`Cache ${key}:`, requests.length, 'items'); + }); + }); + }); +}); +``` + +### Force Update: + +```javascript +navigator.serviceWorker.getRegistrations().then(registrations => { + registrations.forEach(r => r.update()); +}); +``` + +### Unregister All: + +```javascript +navigator.serviceWorker.getRegistrations().then(registrations => { + registrations.forEach(r => r.unregister()); + console.log('All service workers unregistered'); +}); +``` + +--- + +## โœ… Checklist + +- [ ] Add service worker clearing script to `index.html` +- [ ] Build with `--pwa-strategy=none` +- [ ] Update deployment scripts to include `--pwa-strategy=none` +- [ ] Clear nginx cache on proxy LXC +- [ ] Purge Cloudflare cache +- [ ] Tell users to clear service workers +- [ ] Test in incognito mode +- [ ] Verify no errors in console +- [ ] Update deployment documentation + +--- + +## ๐ŸŽฏ Quick Fix Summary + +**Problem**: Service worker caching old JavaScript +**Symptom**: `Uncaught Error` in console +**Root Cause**: Flutter service worker serving stale cached files +**Solution**: Disable service workers + rebuild + clear all caches + +**Do This Now**: +```bash +# 1. Rebuild without service worker +flutter build web --release --pwa-strategy=none + +# 2. Clear browser service worker +F12 โ†’ Application โ†’ Service Workers โ†’ Unregister + +# 3. Hard refresh +Ctrl + Shift + R +``` + +--- + +**Service workers are caching old code! Rebuild with `--pwa-strategy=none` and clear all caches.** ๐Ÿš€ + diff --git a/NGINX_PROXY_CACHE_FIX.md b/NGINX_PROXY_CACHE_FIX.md new file mode 100644 index 0000000..9d65797 --- /dev/null +++ b/NGINX_PROXY_CACHE_FIX.md @@ -0,0 +1,563 @@ +# ๐Ÿ”ง Nginx Proxy Cache Fix - LuckyChit + +## Your Architecture + +``` +Internet โ†’ Cloudflare โ†’ Nginx Proxy (LXC 1) โ†’ Backend (LXC 2: 192.168.8.148) +``` + +## The Problem + +Your nginx proxy in the separate LXC container is caching the old version of your Flutter web app! + +--- + +## โšก IMMEDIATE FIX + +### Option 1: Clear Nginx Cache (Temporary) + +SSH into your **nginx proxy LXC**: + +```bash +# Find nginx cache directory +sudo find /var -name "*cache*" -type d 2>/dev/null | grep nginx + +# Common locations: +# /var/cache/nginx +# /var/lib/nginx/cache +# /var/run/nginx-cache + +# Clear the cache +sudo rm -rf /var/cache/nginx/* +sudo rm -rf /var/lib/nginx/cache/* +sudo rm -rf /var/run/nginx-cache/* + +# Reload nginx +sudo nginx -t +sudo systemctl reload nginx +``` + +### Option 2: Restart Nginx (Quick) + +```bash +sudo systemctl restart nginx +``` + +--- + +## ๐Ÿ” Check Your Nginx Configuration + +SSH into your **nginx proxy LXC** and check the config: + +```bash +# Find nginx config files +sudo nginx -T | grep -i cache +# or +sudo grep -r "proxy_cache" /etc/nginx/ + +# Main config location +sudo cat /etc/nginx/nginx.conf +sudo cat /etc/nginx/sites-enabled/chitfund +``` + +--- + +## ๐ŸŽฏ Nginx Configuration Fix + +### Find Your Site Config + +```bash +cd /etc/nginx/sites-available +ls -la +# Look for chitfund, luckychit, or your domain name +``` + +### Check for Caching Directives + +Look for these lines in your nginx config: + +```nginx +# These ENABLE caching (might be causing issues) +proxy_cache my_cache; +proxy_cache_valid 200 60m; +proxy_cache_use_stale error timeout; +proxy_cache_bypass $http_cache_control; +``` + +--- + +## ๐Ÿ› ๏ธ Solution 1: Disable Nginx Caching (Recommended for Now) + +Edit your nginx config for chitfund: + +```bash +sudo nano /etc/nginx/sites-available/chitfund +``` + +### Add These Headers to DISABLE Caching: + +```nginx +server { + listen 80; + server_name chitfund.deepteklabs.com; + + location / { + proxy_pass http://192.168.8.148:8080; + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection 'upgrade'; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + + # DISABLE CACHING + proxy_cache_bypass $http_upgrade; + proxy_no_cache 1; + proxy_cache off; + + # Tell browsers not to cache + add_header Cache-Control "no-cache, no-store, must-revalidate"; + add_header Pragma "no-cache"; + add_header Expires "0"; + } + + location /api { + proxy_pass http://192.168.8.148:3000; + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection 'upgrade'; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + + # Don't cache API responses + proxy_cache_bypass $http_upgrade; + proxy_no_cache 1; + proxy_cache off; + } +} +``` + +### Test and Reload: + +```bash +sudo nginx -t +sudo systemctl reload nginx +``` + +--- + +## ๐ŸŽฏ Solution 2: Smart Caching (Better for Production) + +Cache static assets but not HTML: + +```nginx +server { + listen 80; + server_name chitfund.deepteklabs.com; + + # Cache static files + location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ { + proxy_pass http://192.168.8.148:8080; + proxy_cache my_cache; + proxy_cache_valid 200 7d; + proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504; + add_header X-Cache-Status $upstream_cache_status; + expires 7d; + } + + # DON'T cache HTML, JSON, etc + location / { + proxy_pass http://192.168.8.148:8080; + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection 'upgrade'; + proxy_set_header Host $host; + + # No caching for HTML + proxy_no_cache 1; + proxy_cache_bypass 1; + add_header Cache-Control "no-cache, no-store, must-revalidate"; + } + + # API - never cache + location /api { + proxy_pass http://192.168.8.148:3000; + proxy_http_version 1.1; + proxy_set_header Host $host; + proxy_no_cache 1; + proxy_cache_bypass 1; + } +} +``` + +--- + +## ๐Ÿ” Debugging Nginx Cache + +### Check if Caching is Enabled: + +```bash +sudo nginx -T | grep -A 5 "proxy_cache" +``` + +### Check Cache Directory: + +```bash +sudo du -sh /var/cache/nginx/* +sudo ls -lh /var/cache/nginx/ +``` + +### Check Cache Status in Response: + +```bash +# From any machine +curl -I http://chitfund.deepteklabs.com + +# Look for: +# X-Cache-Status: HIT (cached) +# X-Cache-Status: MISS (not cached) +``` + +### Enable Cache Status Header: + +Add to your nginx location block: + +```nginx +add_header X-Cache-Status $upstream_cache_status; +``` + +--- + +## ๐Ÿš€ Complete Nginx Config Example + +Here's a complete config for your setup: + +```nginx +# /etc/nginx/sites-available/chitfund + +# Cache zone definition (optional, only if using caching) +# proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m max_size=1g inactive=7d; + +server { + listen 80; + listen [::]:80; + server_name chitfund.deepteklabs.com; + + # Increase timeout for Flutter web + proxy_connect_timeout 60s; + proxy_send_timeout 60s; + proxy_read_timeout 60s; + + # Frontend (Flutter Web) - NO CACHE + location / { + proxy_pass http://192.168.8.148:8080; + proxy_http_version 1.1; + + # WebSocket support + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection 'upgrade'; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + + # Disable caching + proxy_cache_bypass $http_upgrade; + proxy_no_cache 1; + + # Tell browsers not to cache + add_header Cache-Control "no-cache, no-store, must-revalidate" always; + add_header Pragma "no-cache" always; + add_header Expires "0" always; + + # Debug header (optional) + add_header X-Proxy-Cache "DISABLED" always; + } + + # Backend API - NO CACHE + location /api { + proxy_pass http://192.168.8.148:3000; + proxy_http_version 1.1; + + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection 'upgrade'; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + + # Never cache API + proxy_no_cache 1; + proxy_cache_bypass 1; + + add_header Cache-Control "no-cache, no-store, must-revalidate" always; + } + + # Health check endpoint + location /health { + proxy_pass http://192.168.8.148:3000/health; + proxy_no_cache 1; + access_log off; + } +} +``` + +--- + +## ๐Ÿ“‹ Step-by-Step Fix Process + +### 1. SSH into Nginx Proxy LXC + +```bash +ssh root@ +``` + +### 2. Backup Current Config + +```bash +sudo cp /etc/nginx/sites-available/chitfund /etc/nginx/sites-available/chitfund.backup +``` + +### 3. Edit Config + +```bash +sudo nano /etc/nginx/sites-available/chitfund +``` + +Add the cache-control headers shown above. + +### 4. Test Config + +```bash +sudo nginx -t +``` + +Should show: `syntax is ok` and `test is successful` + +### 5. Clear Existing Cache + +```bash +sudo rm -rf /var/cache/nginx/* +``` + +### 6. Reload Nginx + +```bash +sudo systemctl reload nginx +``` + +### 7. Test from Browser + +```bash +# Hard refresh +Ctrl + Shift + R (Windows) +Cmd + Shift + R (Mac) +``` + +--- + +## ๐Ÿงช Verify It's Fixed + +### Check Response Headers: + +```bash +curl -I http://chitfund.deepteklabs.com +``` + +You should see: +``` +Cache-Control: no-cache, no-store, must-revalidate +Pragma: no-cache +Expires: 0 +``` + +### Check from Browser DevTools: + +1. Press `F12` +2. Go to Network tab +3. Refresh page +4. Click on the main document +5. Check Response Headers +6. Should show `Cache-Control: no-cache` + +--- + +## ๐ŸŽฏ For 502 Bad Gateway Issue + +The 502 might be because: + +### 1. Nginx Can't Reach Backend + +```bash +# From nginx proxy LXC, test backend connectivity: +curl http://192.168.8.148:3000/health +curl http://192.168.8.148:8080 + +# If these fail, check: +# - Backend PM2 is running +# - Firewall allows traffic between LXCs +# - Network routing between containers +``` + +### 2. Backend is Down + +SSH into backend LXC (192.168.8.148): + +```bash +pm2 status +pm2 restart all +``` + +### 3. Firewall Between LXCs + +On backend LXC (192.168.8.148): + +```bash +# Allow traffic from nginx proxy +sudo ufw status +sudo ufw allow from to any port 3000 +sudo ufw allow from to any port 8080 +``` + +--- + +## ๐Ÿ”— Architecture Diagram + +``` +โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” +โ”‚ Browser โ”‚ +โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”˜ + โ”‚ HTTPS + โ–ผ +โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” +โ”‚ Cloudflare โ”‚ (CDN/Proxy) +โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”˜ + โ”‚ HTTP/HTTPS + โ–ผ +โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” +โ”‚ Nginx Proxy โ”‚ LXC 1 +โ”‚ (Caching!) โ”‚ Port 80/443 +โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”˜ + โ”‚ HTTP + โ–ผ +โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” +โ”‚ PM2 Apps โ”‚ LXC 2 (192.168.8.148) +โ”‚ Backend:3000โ”‚ +โ”‚ Frontend:8080โ”‚ +โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ +``` + +**Cache points:** +1. Browser โœ… (fixed with meta tags) +2. Cloudflare โš ๏ธ (can cache) +3. **Nginx Proxy โš ๏ธ (likely culprit!)** +4. PM2 โœ… (serves files) + +--- + +## ๐ŸŽ“ Understanding the Issue + +### Why Nginx Caches: + +- **Performance**: Reduces load on backend +- **Speed**: Serves cached content faster +- **Default behavior**: Many nginx configs enable caching + +### Why It's Problematic: + +- โŒ Shows old version after deployment +- โŒ Users see stale content +- โŒ Developers get confused + +### The Solution: + +- โœ… Disable caching for HTML/JSON +- โœ… Optional: Cache only static assets (images, CSS, JS) +- โœ… Add cache-control headers + +--- + +## ๐Ÿ“‹ Complete Fix Checklist + +- [ ] SSH into nginx proxy LXC +- [ ] Backup nginx config +- [ ] Add `proxy_no_cache` directives +- [ ] Add cache-control headers +- [ ] Test nginx config: `nginx -t` +- [ ] Clear nginx cache: `rm -rf /var/cache/nginx/*` +- [ ] Reload nginx: `systemctl reload nginx` +- [ ] Test backend connectivity from nginx LXC +- [ ] Verify PM2 is running on backend LXC +- [ ] Hard refresh browser +- [ ] Check response headers +- [ ] Verify latest content shows + +--- + +## ๐Ÿšจ Quick Fix Script + +Create this on your nginx proxy LXC: + +```bash +#!/bin/bash +# clear-nginx-cache.sh + +echo "๐Ÿงน Clearing Nginx Cache..." + +# Clear cache +sudo rm -rf /var/cache/nginx/* +sudo rm -rf /var/lib/nginx/cache/* +sudo rm -rf /var/run/nginx-cache/* + +# Reload nginx +sudo nginx -t && sudo systemctl reload nginx + +echo "โœ… Cache cleared and nginx reloaded!" +echo "" +echo "๐Ÿงช Test backend connectivity:" +curl -I http://192.168.8.148:3000/health +``` + +--- + +## ๐Ÿ’ก Pro Tips + +1. **Always disable cache during development** +2. **Enable selective caching in production** (only static files) +3. **Add cache-control headers** at nginx level +4. **Monitor cache hit rates** to tune performance +5. **Document your nginx LXC setup** for future reference + +--- + +## ๐Ÿ“ž Still Having Issues? + +If caching persists: + +1. **Check all 3 cache layers**: + - Browser (Ctrl+Shift+R) + - Cloudflare (purge cache in dashboard) + - Nginx (clear as shown above) + +2. **Verify nginx config**: + ```bash + sudo nginx -T | grep -i cache + ``` + +3. **Check response headers**: + ```bash + curl -I http://chitfund.deepteklabs.com + ``` + +4. **Test directly to backend** (bypass nginx): + ```bash + curl http://192.168.8.148:8080 + ``` + +--- + +**Your cache issue is likely in the nginx proxy! Clear the cache and add the no-cache headers shown above.** ๐Ÿš€ + diff --git a/QUICK_REFERENCE.md b/QUICK_REFERENCE.md index 068d8e8..6a2ab97 100644 --- a/QUICK_REFERENCE.md +++ b/QUICK_REFERENCE.md @@ -1,5 +1,17 @@ # ๐Ÿš€ Quick Reference - LuckyChit Production +## ๐Ÿšจ EMERGENCY: 502 Bad Gateway Error? + +```bash +ssh luckychit@192.168.8.148 +cd /home/luckychit/apps/chitfund +./fix-502-auto.sh +``` + +See `FIX_502_ERROR.md` for details. + +--- + ## ๐Ÿ“ Server Info ``` Server IP: 192.168.8.148 @@ -101,9 +113,14 @@ pm2 logs luckychit-frontend --lines 20 cd /home/luckychit/apps/chitfund ./force-cache-bust.sh -# In browser - Hard Refresh -# Windows: Ctrl + Shift + R -# Mac: Cmd + Shift + R +# Clear Service Worker (Browser) +# 1. Press F12 (DevTools) +# 2. Application tab โ†’ Service Workers +# 3. Click "Unregister" for all workers +# 4. Hard Refresh: Ctrl + Shift + R (Windows) or Cmd + Shift + R (Mac) + +# Or test in Incognito mode (no cache) +# Ctrl + Shift + N (Windows) or Cmd + Shift + N (Mac) ``` ### "invalid ELF header" Error diff --git a/SIGNUP_NAVIGATION_UPDATE.md b/SIGNUP_NAVIGATION_UPDATE.md new file mode 100644 index 0000000..4f9ed35 --- /dev/null +++ b/SIGNUP_NAVIGATION_UPDATE.md @@ -0,0 +1,214 @@ +# โœ… Signup Navigation - Updated + +## What Changed + +After successful signup, users are now automatically navigated to their dashboard! + +--- + +## ๐ŸŽฏ User Flow + +### Before: +``` +User signs up โ†’ Success message โ†’ Stays on signup screen โŒ +``` + +### After: +``` +User signs up โ†’ Success message โ†’ Auto-navigates to dashboard โœ… +``` + +--- + +## ๐Ÿ“Š Navigation Logic + +### The Flow: + +1. **User completes signup form** + - Enters mobile number, name, password, etc. + - Clicks "Sign Up" button + +2. **AuthService.signup() called** + - Creates account on backend + - **Automatically logs user in** + - Saves token and user data + - Sets `isAuthenticated = true` + +3. **Success โ†’ Navigate to App()** + - Uses `Get.offAll()` (removes all previous routes) + - App widget checks authentication + - App widget checks user role + +4. **Auto-route to correct dashboard** + - If `role == 'manager'` โ†’ ManagerDashboard + - If `role == 'member'` โ†’ MemberDashboard + +--- + +## ๐Ÿ”ง What I Changed + +### File: `luckychit/lib/features/auth/views/signup_screen.dart` + +#### 1. Added Import: +```dart +import '../../../app.dart'; +``` + +#### 2. Updated `_handleSignup()`: +```dart +if (success) { + SnackbarUtil.showSuccess( + 'Account created successfully! Welcome to LuckyChit.', + title: 'Success', + ); + + // Navigate to home - App widget will auto-route based on role + // Use offAll to remove all previous routes (can't go back to signup) + Get.offAll(() => const App()); +} +``` + +--- + +## ๐ŸŽฏ Why `Get.offAll()`? + +**`Get.offAll()`** clears the entire navigation stack, meaning: + +- โœ… User **cannot** press back button to return to signup +- โœ… User starts fresh at their dashboard +- โœ… Cleaner navigation history +- โœ… Prevents accidental duplicate signups + +**Alternative options:** +- `Get.off()` - Replace current route (user could still go back) +- `Get.to()` - Add route to stack (user could go back) + +--- + +## ๐Ÿงช Testing + +### Test Scenarios: + +1. **New Member Signup** + ``` + Sign up โ†’ Should go to MemberDashboard + Should see: "My Chitfunds" header + ``` + +2. **New Manager Signup** + ``` + Sign up โ†’ Should go to ManagerDashboard + Should see: "Manager Dashboard" or management features + ``` + +3. **Failed Signup** + ``` + Invalid data โ†’ Should stay on signup screen + Should see: Error message + ``` + +4. **Back Button** + ``` + After successful signup โ†’ Press back + Should NOT return to signup screen (offAll cleared stack) + ``` + +--- + +## ๐ŸŽจ User Experience + +### What User Sees: + +1. Fill out signup form +2. Click "Sign Up" +3. Loading indicator appears +4. โœ… Success message shows: "Account created successfully! Welcome to LuckyChit." +5. Automatically redirected to their dashboard +6. Can immediately start using the app! + +**Total time**: ~2-3 seconds from signup to dashboard + +--- + +## ๐Ÿ” Security Notes + +- User is **automatically logged in** after signup +- Token is stored securely in SharedPreferences +- No need to login again after signup +- Session persists across app restarts + +--- + +## ๐Ÿ› Edge Cases Handled + +### 1. Signup Fails +- User stays on signup screen +- Error message displayed +- Can try again + +### 2. Network Error During Signup +- AuthService handles error +- User stays on signup screen +- Error snackbar shown + +### 3. User Already Exists +- Backend returns error +- Shown as error snackbar +- User can try login instead + +--- + +## ๐Ÿš€ Future Enhancements (Optional) + +### Welcome Screen (Optional) +Could add a quick welcome/onboarding screen after first signup: + +```dart +if (success) { + final isFirstSignup = await checkIfFirstSignup(); + + if (isFirstSignup) { + Get.offAll(() => const WelcomeScreen()); + } else { + Get.offAll(() => const App()); + } +} +``` + +### Role-Specific Welcome Message +Could customize success message based on role: + +```dart +final role = _authService.currentUser.value?.role; +final message = role == 'manager' + ? 'Welcome, Manager! You can now create and manage chitfunds.' + : 'Welcome! You can now join chitfunds and make payments.'; + +SnackbarUtil.showSuccess(message, title: 'Success'); +``` + +--- + +## ๐Ÿ“‹ Related Files + +- `luckychit/lib/features/auth/views/signup_screen.dart` - Updated โœ… +- `luckychit/lib/core/services/auth_service.dart` - Already handles auto-login +- `luckychit/lib/app.dart` - Handles role-based routing +- `luckychit/lib/interfaces/manager/manager_dashboard.dart` - Manager home +- `luckychit/lib/interfaces/member/member_dashboard.dart` - Member home + +--- + +## โœ… Summary + +**Before**: User stayed on signup screen after successful signup โŒ +**After**: User automatically navigated to their role-based dashboard โœ… + +**Change**: Added `Get.offAll(() => const App())` after successful signup +**Result**: Seamless signup โ†’ dashboard flow +**User Impact**: Better UX, one less step, feels more professional + +--- + +**Signup navigation is now complete! Users will love the seamless experience.** ๐ŸŽ‰ + diff --git a/clear-cache-and-deploy.sh b/clear-cache-and-deploy.sh index 68ee429..23693da 100644 --- a/clear-cache-and-deploy.sh +++ b/clear-cache-and-deploy.sh @@ -28,6 +28,7 @@ echo "๐Ÿ”จ Step 3/6: Building with cache-busting..." BUILD_NUMBER=$(date +%s) flutter build web --release \ --web-renderer html \ + --pwa-strategy=none \ --build-name=1.0.$BUILD_NUMBER \ --build-number=$BUILD_NUMBER diff --git a/deploy-frontend-only.sh b/deploy-frontend-only.sh index feaa8e5..cd9bf48 100644 --- a/deploy-frontend-only.sh +++ b/deploy-frontend-only.sh @@ -22,8 +22,8 @@ flutter pub get echo "๐Ÿ—๏ธ Building Flutter web app with cache-busting..." # Add timestamp to force browser cache refresh BUILD_NUMBER=$(date +%s) -flutter build web --release --web-renderer html --build-number=$BUILD_NUMBER -echo "๐Ÿ“ฆ Build version: $BUILD_NUMBER" +flutter build web --release --web-renderer html --pwa-strategy=none --build-number=$BUILD_NUMBER +echo "๐Ÿ“ฆ Build version: $BUILD_NUMBER (Service Worker: Disabled)" echo "โ™ป๏ธ Restarting frontend..." pm2 reload luckychit-frontend diff --git a/deploy-full.sh b/deploy-full.sh index 49dcc6b..0b8f940 100644 --- a/deploy-full.sh +++ b/deploy-full.sh @@ -47,8 +47,8 @@ flutter pub get echo "๐Ÿ—๏ธ Building Flutter web app with cache-busting..." # Add timestamp to force browser cache refresh BUILD_NUMBER=$(date +%s) -flutter build web --release --web-renderer html --build-number=$BUILD_NUMBER -echo "๐Ÿ“ฆ Build version: $BUILD_NUMBER" +flutter build web --release --web-renderer html --pwa-strategy=none --build-number=$BUILD_NUMBER +echo "๐Ÿ“ฆ Build version: $BUILD_NUMBER (Service Worker: Disabled)" # Restart frontend echo "โ™ป๏ธ Restarting frontend..." diff --git a/diagnose-502.sh b/diagnose-502.sh new file mode 100644 index 0000000..544d725 --- /dev/null +++ b/diagnose-502.sh @@ -0,0 +1,155 @@ +#!/bin/bash + +# 502 Error Diagnostic Script +# Run this on production server to diagnose Bad Gateway issues + +echo "๐Ÿ” LuckyChit 502 Error Diagnostic" +echo "====================================" +echo "" +echo "Checking server status..." +echo "" + +# 1. PM2 Status +echo "โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”" +echo "1๏ธโƒฃ PM2 Process Status" +echo "โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”" +pm2 status + +echo "" +echo "โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”" +echo "2๏ธโƒฃ Port Listening Check" +echo "โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”" +PORTS=$(netstat -tulpn 2>/dev/null | grep -E '(3000|8080)') +if [ -z "$PORTS" ]; then + echo "โŒ No processes listening on ports 3000 or 8080" +else + echo "โœ… Ports are listening:" + echo "$PORTS" +fi + +echo "" +echo "โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”" +echo "3๏ธโƒฃ Backend Health Check (localhost)" +echo "โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”" +BACKEND_RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:3000/health 2>/dev/null) +if [ "$BACKEND_RESPONSE" = "200" ]; then + echo "โœ… Backend responding: HTTP $BACKEND_RESPONSE" + curl -s http://localhost:3000/health | head -n 5 +else + echo "โŒ Backend not responding (HTTP $BACKEND_RESPONSE)" +fi + +echo "" +echo "โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”" +echo "4๏ธโƒฃ Frontend Health Check (localhost)" +echo "โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”" +FRONTEND_RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:8080 2>/dev/null) +if [ "$FRONTEND_RESPONSE" = "200" ]; then + echo "โœ… Frontend responding: HTTP $FRONTEND_RESPONSE" +else + echo "โŒ Frontend not responding (HTTP $FRONTEND_RESPONSE)" +fi + +echo "" +echo "โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”" +echo "5๏ธโƒฃ External Access Check (192.168.8.148)" +echo "โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”" +EXTERNAL_BACKEND=$(curl -s -o /dev/null -w "%{http_code}" http://192.168.8.148:3000/health 2>/dev/null) +if [ "$EXTERNAL_BACKEND" = "200" ]; then + echo "โœ… Backend accessible from IP: HTTP $EXTERNAL_BACKEND" +else + echo "โŒ Backend not accessible from IP (HTTP $EXTERNAL_BACKEND)" +fi + +echo "" +echo "โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”" +echo "6๏ธโƒฃ Firewall Status" +echo "โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”" +UFW_STATUS=$(sudo ufw status 2>/dev/null | grep -E '(3000|8080|80|443)') +if [ -z "$UFW_STATUS" ]; then + echo "โš ๏ธ Firewall ports may not be open" +else + echo "โœ… Firewall rules:" + echo "$UFW_STATUS" +fi + +echo "" +echo "โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”" +echo "7๏ธโƒฃ Disk Space" +echo "โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”" +df -h | grep -E '(Filesystem|/$|/home)' + +echo "" +echo "โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”" +echo "8๏ธโƒฃ Memory Usage" +echo "โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”" +free -h + +echo "" +echo "โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”" +echo "9๏ธโƒฃ PostgreSQL Status" +echo "โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”" +POSTGRES_STATUS=$(sudo systemctl is-active postgresql 2>/dev/null) +if [ "$POSTGRES_STATUS" = "active" ]; then + echo "โœ… PostgreSQL is running" +else + echo "โŒ PostgreSQL is not running" +fi + +echo "" +echo "โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”" +echo "๐Ÿ”Ÿ Recent PM2 Error Logs" +echo "โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”" +pm2 logs --err --lines 10 --nostream 2>/dev/null || echo "No recent errors" + +echo "" +echo "โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”" +echo "โœ… Diagnosis Complete!" +echo "โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”" +echo "" + +# Summary and recommendations +echo "๐Ÿ“‹ SUMMARY & RECOMMENDATIONS:" +echo "" + +# Check PM2 status +PM2_COUNT=$(pm2 jlist 2>/dev/null | grep -c '"pm2_env"') +if [ "$PM2_COUNT" -lt 2 ]; then + echo "โš ๏ธ ISSUE: PM2 processes are missing or down" + echo " FIX: pm2 restart all OR recreate processes" + echo "" +fi + +# Check ports +if [ -z "$PORTS" ]; then + echo "โš ๏ธ ISSUE: No processes listening on required ports" + echo " FIX: Start PM2 processes" + echo "" +fi + +# Check backend +if [ "$BACKEND_RESPONSE" != "200" ]; then + echo "โš ๏ธ ISSUE: Backend not responding" + echo " FIX: pm2 restart luckychit-api" + echo " CHECK: pm2 logs luckychit-api" + echo "" +fi + +# Check frontend +if [ "$FRONTEND_RESPONSE" != "200" ]; then + echo "โš ๏ธ ISSUE: Frontend not responding" + echo " FIX: pm2 restart luckychit-frontend" + echo " CHECK: pm2 logs luckychit-frontend" + echo "" +fi + +# Check PostgreSQL +if [ "$POSTGRES_STATUS" != "active" ]; then + echo "โš ๏ธ ISSUE: PostgreSQL is not running" + echo " FIX: sudo systemctl start postgresql" + echo "" +fi + +echo "๐Ÿ“š For detailed fixes, see: FIX_502_ERROR.md" +echo "" + diff --git a/fix-502-auto.sh b/fix-502-auto.sh new file mode 100644 index 0000000..31ad438 --- /dev/null +++ b/fix-502-auto.sh @@ -0,0 +1,145 @@ +#!/bin/bash + +# Automatic 502 Error Fix Script +# Tries common fixes for Bad Gateway errors + +set -e + +echo "๐Ÿ”ง LuckyChit 502 Auto-Fix" +echo "=========================" +echo "" + +# Function to check if backend is responding +check_backend() { + RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:3000/health 2>/dev/null) + [ "$RESPONSE" = "200" ] +} + +# Function to check if frontend is responding +check_frontend() { + RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:8080 2>/dev/null) + [ "$RESPONSE" = "200" ] +} + +echo "๐Ÿ“Š Step 1/6: Checking current status..." +pm2 status + +echo "" +echo "๐Ÿ”„ Step 2/6: Attempting PM2 restart..." +pm2 restart all + +sleep 3 + +echo "" +echo "๐Ÿงช Step 3/6: Testing services..." + +# Test backend +if check_backend; then + echo "โœ… Backend is responding" +else + echo "โš ๏ธ Backend not responding, trying to start..." + cd /home/luckychit/apps/chitfund/backend + pm2 delete luckychit-api 2>/dev/null || true + pm2 start src/server.js --name luckychit-api + sleep 3 + + if check_backend; then + echo "โœ… Backend started successfully" + else + echo "โŒ Backend still not responding" + echo "Check logs: pm2 logs luckychit-api" + fi +fi + +# Test frontend +if check_frontend; then + echo "โœ… Frontend is responding" +else + echo "โš ๏ธ Frontend not responding, trying to start..." + cd /home/luckychit/apps/chitfund/luckychit + pm2 delete luckychit-frontend 2>/dev/null || true + pm2 serve build/web 8080 --name luckychit-frontend --spa + sleep 3 + + if check_frontend; then + echo "โœ… Frontend started successfully" + else + echo "โŒ Frontend still not responding" + echo "May need to rebuild: flutter build web --release" + fi +fi + +echo "" +echo "๐Ÿ’พ Step 4/6: Saving PM2 configuration..." +pm2 save + +echo "" +echo "๐Ÿ”ฅ Step 5/6: Checking firewall..." +sudo ufw status | grep -E '(3000|8080)' || { + echo "โš ๏ธ Firewall rules may be missing, adding..." + sudo ufw allow 3000/tcp + sudo ufw allow 8080/tcp + sudo ufw reload + echo "โœ… Firewall rules added" +} + +echo "" +echo "๐Ÿ“Š Step 6/6: Final status check..." +pm2 status + +echo "" +echo "โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”" +echo "๐Ÿงช Testing Services" +echo "โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”" + +# Test backend +if check_backend; then + echo "โœ… Backend: http://localhost:3000/health" + curl -s http://localhost:3000/health | head -n 3 +else + echo "โŒ Backend: Not responding" +fi + +echo "" + +# Test frontend +if check_frontend; then + echo "โœ… Frontend: http://localhost:8080" +else + echo "โŒ Frontend: Not responding" +fi + +echo "" + +# Test external access +EXTERNAL=$(curl -s -o /dev/null -w "%{http_code}" http://192.168.8.148:3000/health 2>/dev/null) +if [ "$EXTERNAL" = "200" ]; then + echo "โœ… External access: http://192.168.8.148:3000" +else + echo "โŒ External access: Not accessible (may be firewall)" +fi + +echo "" +echo "โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”" + +# Final verdict +if check_backend && check_frontend; then + echo "โœ… SUCCESS! Both services are running" + echo "" + echo "๐ŸŒ Your site should now be accessible at:" + echo " https://chitfund.deepteklabs.com" + echo "" + echo "๐Ÿ’ก If still showing 502, wait 1-2 minutes for Cloudflare to detect" + echo " Or purge Cloudflare cache in dashboard" +else + echo "โš ๏ธ WARNING: Some services are still down" + echo "" + echo "๐Ÿ” Next steps:" + echo " 1. Check logs: pm2 logs" + echo " 2. Check database: sudo systemctl status postgresql" + echo " 3. Run full diagnostic: ./diagnose-502.sh" + echo " 4. See detailed guide: FIX_502_ERROR.md" +fi + +echo "" + diff --git a/force-cache-bust.sh b/force-cache-bust.sh index 9c0c907..e8efd32 100644 --- a/force-cache-bust.sh +++ b/force-cache-bust.sh @@ -20,7 +20,7 @@ rm -rf build echo "" echo "๐Ÿ”จ Building with new timestamp..." BUILD_NUMBER=$(date +%Y%m%d%H%M%S) -flutter build web --release --web-renderer html --build-number=$BUILD_NUMBER +flutter build web --release --web-renderer html --pwa-strategy=none --build-number=$BUILD_NUMBER echo "" echo "โœ… Build complete with version: $BUILD_NUMBER" diff --git a/luckychit/lib/features/auth/views/signup_screen.dart b/luckychit/lib/features/auth/views/signup_screen.dart index 7860781..30ca939 100644 --- a/luckychit/lib/features/auth/views/signup_screen.dart +++ b/luckychit/lib/features/auth/views/signup_screen.dart @@ -3,6 +3,7 @@ import 'package:get/get.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; import '../../../core/services/auth_service.dart'; import '../../../core/utils/snackbar_util.dart'; +import '../../../app.dart'; class SignupScreen extends StatefulWidget { const SignupScreen({super.key}); @@ -52,6 +53,10 @@ class _SignupScreenState extends State { 'Account created successfully! Welcome to LuckyChit.', title: 'Success', ); + + // Navigate to home - App widget will auto-route based on role + // Use offAll to remove all previous routes (can't go back to signup) + Get.offAll(() => const App()); } else { SnackbarUtil.showError( 'Failed to create account. Please try again.', diff --git a/luckychit/web/index.html b/luckychit/web/index.html index 69efffd..d4e1732 100644 --- a/luckychit/web/index.html +++ b/luckychit/web/index.html @@ -38,6 +38,17 @@ + + diff --git a/nginx-clear-cache.sh b/nginx-clear-cache.sh new file mode 100644 index 0000000..dec42f9 --- /dev/null +++ b/nginx-clear-cache.sh @@ -0,0 +1,102 @@ +#!/bin/bash + +# Nginx Proxy Cache Clear Script +# Run this on your NGINX PROXY LXC (not the backend LXC!) + +echo "๐Ÿงน LuckyChit - Nginx Proxy Cache Clear" +echo "======================================" +echo "" + +# Check if running as root +if [ "$EUID" -ne 0 ]; then + echo "โš ๏ธ This script needs sudo privileges" + echo "Run with: sudo ./nginx-clear-cache.sh" + exit 1 +fi + +echo "๐Ÿ“Š Step 1/5: Checking nginx status..." +systemctl status nginx --no-pager | head -n 3 + +echo "" +echo "๐Ÿ” Step 2/5: Finding nginx cache directories..." +CACHE_DIRS=$(find /var -name "*cache*" -type d 2>/dev/null | grep nginx) + +if [ -z "$CACHE_DIRS" ]; then + echo "No nginx cache directories found" +else + echo "Found cache directories:" + echo "$CACHE_DIRS" +fi + +echo "" +echo "๐Ÿ—‘๏ธ Step 3/5: Clearing nginx cache..." + +# Clear common cache locations +rm -rf /var/cache/nginx/* 2>/dev/null && echo "โœ… Cleared /var/cache/nginx/" || echo "โš ๏ธ /var/cache/nginx/ not found" +rm -rf /var/lib/nginx/cache/* 2>/dev/null && echo "โœ… Cleared /var/lib/nginx/cache/" || echo "โš ๏ธ /var/lib/nginx/cache/ not found" +rm -rf /var/run/nginx-cache/* 2>/dev/null && echo "โœ… Cleared /var/run/nginx-cache/" || echo "โš ๏ธ /var/run/nginx-cache/ not found" +rm -rf /tmp/nginx/* 2>/dev/null && echo "โœ… Cleared /tmp/nginx/" || echo "โš ๏ธ /tmp/nginx/ not found" + +echo "" +echo "โœ… Step 4/5: Testing nginx configuration..." +nginx -t + +if [ $? -eq 0 ]; then + echo "" + echo "โ™ป๏ธ Step 5/5: Reloading nginx..." + systemctl reload nginx + + if [ $? -eq 0 ]; then + echo "โœ… Nginx reloaded successfully!" + else + echo "โŒ Failed to reload nginx" + echo "Try: systemctl restart nginx" + exit 1 + fi +else + echo "โŒ Nginx configuration has errors!" + echo "Fix configuration before reloading" + exit 1 +fi + +echo "" +echo "โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”" +echo "๐Ÿงช Testing Backend Connectivity" +echo "โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”" + +# Test backend connectivity +echo "" +echo "Testing backend (192.168.8.148:3000)..." +BACKEND_RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" http://192.168.8.148:3000/health 2>/dev/null) + +if [ "$BACKEND_RESPONSE" = "200" ]; then + echo "โœ… Backend API responding: HTTP $BACKEND_RESPONSE" +else + echo "โŒ Backend API not responding: HTTP $BACKEND_RESPONSE" + echo " Check if PM2 is running on backend LXC" +fi + +echo "" +echo "Testing frontend (192.168.8.148:8080)..." +FRONTEND_RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" http://192.168.8.148:8080 2>/dev/null) + +if [ "$FRONTEND_RESPONSE" = "200" ]; then + echo "โœ… Frontend responding: HTTP $FRONTEND_RESPONSE" +else + echo "โŒ Frontend not responding: HTTP $FRONTEND_RESPONSE" + echo " Check if PM2 is running on backend LXC" +fi + +echo "" +echo "โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”" +echo "โœ… Cache clearing complete!" +echo "โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”" +echo "" +echo "๐ŸŒ Now test your site:" +echo " https://chitfund.deepteklabs.com" +echo "" +echo "๐Ÿ’ก Don't forget to hard refresh in browser:" +echo " Windows: Ctrl + Shift + R" +echo " Mac: Cmd + Shift + R" +echo "" +