hue-portal-backend / README_ngrok_fix.md
Davidtran99
chore: sync with main repo
3718c84

Fix Ngrok Tunnel với PostgreSQL

Vấn đề

Ngrok TCP tunnel không kết nối được PostgreSQL trong Docker container mặc dù:

  • ✅ PostgreSQL đang listen trên * (tất cả interfaces)
  • ✅ pg_hba.conf đã có rule cho 0.0.0.0/0
  • ✅ Docker port mapping đúng (0.0.0.0:5543->5432)
  • ✅ Ngrok tunnel đang chạy và forward đến localhost:5543

Nguyên nhân có thể

  1. Ngrok TCP tunnel không forward đúng PostgreSQL protocol handshake
  2. Docker network isolation - PostgreSQL trong container không accept connection từ ngrok IP range
  3. PostgreSQL trong Docker chỉ bind đến localhost bên trong container

Giải pháp đã thử

1. Fix PostgreSQL config

  • ✅ Set listen_addresses = '*'
  • ✅ Thêm rule vào pg_hba.conf: host all all 0.0.0.0/0 md5
  • ✅ Restart PostgreSQL container

2. Fix Docker config

  • ✅ Set port mapping: 0.0.0.0:5543:5432
  • ✅ Thêm command: postgres -c listen_addresses='*'

3. Scripts đã tạo

  • khoidongDB.sh - Khởi động ngrok và update DATABASE_URL
  • fix_ngrok_postgres.sh - Fix và test ngrok tunnel

Giải pháp thay thế

Option 1: SSH Tunnel (Khuyến nghị)

# Trên server có PostgreSQL
ssh -N -L 5543:localhost:5543 user@your-server

# Hoặc reverse SSH tunnel
ssh -R 5543:localhost:5543 user@your-server

Option 2: Cloudflare Tunnel

# Cài đặt cloudflared
brew install cloudflared  # macOS
# hoặc
sudo apt install cloudflared  # Linux

# Tạo tunnel
cloudflared tunnel --url tcp://localhost:5543

Option 3: Tailscale

# Cài đặt Tailscale
# Tạo mesh network
# PostgreSQL sẽ accessible qua Tailscale IP

Option 4: Docker network_mode: host

# docker-compose.yml
db:
  network_mode: host
  # ... config khác

Lưu ý: network_mode: host chỉ hoạt động trên Linux, không hoạt động trên macOS/Windows.

Workaround hiện tại

Space đang dùng SQLite fallback:

  • ✅ App chạy được
  • ✅ Migration pass
  • ✅ Chatbot hoạt động (không có data)
  • ⚠️ Không có data để trả lời câu hỏi phức tạp

Next Steps

  1. Test SSH tunnel thay vì ngrok
  2. Hoặc dùng Cloudflare Tunnel (miễn phí, ổn định hơn)
  3. Hoặc chấp nhận SQLite fallback cho Space (chỉ để test)

Scripts

  • khoidongDB.sh - Khởi động ngrok và update DATABASE_URL
  • fix_ngrok_postgres.sh - Fix và test ngrok tunnel (chưa fix được)

Cách sử dụng

# Khởi động ngrok và update DATABASE_URL
./hue-portal-backend/khoidongDB.sh

# Fix và test ngrok tunnel
./hue-portal-backend/fix_ngrok_postgres.sh