From fae7e0cd5346b6fb3e1c7fc5a4da11c0ef6fc805 Mon Sep 17 00:00:00 2001 From: rameen Date: Sat, 19 Sep 2026 09:26:06 +0330 Subject: [PATCH] fix(ci): prod nginx re-resolves backend via Docker DNS; deploy starts nginx last --- infrastructure/DEPLOY.md | 13 +++++++- infrastructure/docker-compose.prod.yml | 2 ++ infrastructure/nginx/nginx.ssl.conf.template | 23 +++++++------ infrastructure/scripts/deploy-prod.sh | 19 +++++++++++ infrastructure/scripts/prod-remote-deploy.sh | 34 ++++++++++++++++++-- 5 files changed, 75 insertions(+), 16 deletions(-) diff --git a/infrastructure/DEPLOY.md b/infrastructure/DEPLOY.md index d8afed9..79be5ca 100644 --- a/infrastructure/DEPLOY.md +++ b/infrastructure/DEPLOY.md @@ -472,7 +472,18 @@ cd /opt/dyolink/infrastructure ./scripts/prod-remote-deploy.sh v1.0.2 ``` -`prod-remote-deploy.sh` pulls backend then frontend with retries. If every attempt RSTs, check Gitea is up and `insecure-registries` includes `wixur.ir:3000`. +`prod-remote-deploy.sh` starts backend/frontend, waits until they are healthy, then recreates nginx. SSL nginx uses Docker DNS (`resolver 127.0.0.11 ipv6=off`) so `/api` and `/admin` follow new container IPs. + +### `/api` or `/admin` 502 after a tag while the landing page works + +Nginx still has a stale backend IP (old SSL config) or started before Nest listened. Recreate nginx only: + +```bash +cd /opt/dyolink/infrastructure +docker compose -f docker-compose.prod.yml --env-file .env up -d --force-recreate nginx +``` + +Then `https://nudentic.ir/api/health` should return JSON. ### View logs diff --git a/infrastructure/docker-compose.prod.yml b/infrastructure/docker-compose.prod.yml index a693c7c..7559e7f 100644 --- a/infrastructure/docker-compose.prod.yml +++ b/infrastructure/docker-compose.prod.yml @@ -101,6 +101,8 @@ services: nginx: image: ${NGINX_IMAGE:-nginx:alpine} container_name: dyolink_nginx_prod + # No depends_on: nginx must serve the ACME challenge and the landing page even + # when the backend is down. Deploy ordering lives in the deploy scripts. ports: - "80:80" - "443:443" diff --git a/infrastructure/nginx/nginx.ssl.conf.template b/infrastructure/nginx/nginx.ssl.conf.template index 5917064..db8b2a6 100644 --- a/infrastructure/nginx/nginx.ssl.conf.template +++ b/infrastructure/nginx/nginx.ssl.conf.template @@ -1,15 +1,11 @@ # Generated from nginx.ssl.conf.template — do not edit nginx/generated/default.conf by hand. # Re-run: ./scripts/render-nginx-ssl.sh +# +# Dynamic proxy_pass via Docker DNS (127.0.0.11) so nginx keeps working after +# backend/frontend containers get new IPs on a tag deploy. Static `upstream` +# blocks cache the old IP → 502 on /api and /admin. Staging: http-only.conf. -upstream dyolink_backend { - server backend:3000; - keepalive 32; -} - -upstream dyolink_frontend { - server frontend:3000; - keepalive 32; -} +resolver 127.0.0.11 valid=10s ipv6=off; server { listen 80; @@ -56,7 +52,8 @@ server { send_timeout 10; location / { - proxy_pass http://dyolink_frontend; + set $frontend_upstream http://frontend:3000; + proxy_pass $frontend_upstream; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; @@ -70,7 +67,8 @@ server { } location /api { - proxy_pass http://dyolink_backend; + set $backend_upstream http://backend:3000; + proxy_pass $backend_upstream; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; @@ -85,7 +83,8 @@ server { # AdminJS (Nest, not under /api) — https://nudentic.ir/admin location /admin { - proxy_pass http://dyolink_backend; + set $backend_upstream http://backend:3000; + proxy_pass $backend_upstream; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; diff --git a/infrastructure/scripts/deploy-prod.sh b/infrastructure/scripts/deploy-prod.sh index 2d7b699..0f32544 100755 --- a/infrastructure/scripts/deploy-prod.sh +++ b/infrastructure/scripts/deploy-prod.sh @@ -45,6 +45,25 @@ else echo -e "${YELLOW}Pulling images...${NC}" "${COMPOSE[@]}" pull backend frontend echo -e "${YELLOW}Starting stack...${NC}" + "${COMPOSE[@]}" up -d postgres backend frontend + echo -e "${YELLOW}Waiting for backend/frontend healthy before nginx...${NC}" + elapsed=0 + backend_status="missing" + frontend_status="missing" + while [ "$elapsed" -lt 300 ]; do + backend_status="$(docker inspect -f '{{if .State.Health}}{{.State.Health.Status}}{{else}}{{.State.Status}}{{end}}' dyolink_backend_prod 2>/dev/null || echo missing)" + frontend_status="$(docker inspect -f '{{if .State.Health}}{{.State.Health.Status}}{{else}}{{.State.Status}}{{end}}' dyolink_frontend_prod 2>/dev/null || echo missing)" + if [ "$backend_status" = "healthy" ] && [ "$frontend_status" = "healthy" ]; then + break + fi + sleep 3 + elapsed=$((elapsed + 3)) + done + if [ "$backend_status" != "healthy" ] || [ "$frontend_status" != "healthy" ]; then + echo -e "${RED}backend=${backend_status} frontend=${frontend_status} — not healthy in 300s${NC}" + docker logs dyolink_backend_prod --tail 50 || true + exit 1 + fi "${COMPOSE[@]}" up -d --force-recreate nginx "${COMPOSE[@]}" up -d fi diff --git a/infrastructure/scripts/prod-remote-deploy.sh b/infrastructure/scripts/prod-remote-deploy.sh index b0d9f26..49a5eb4 100755 --- a/infrastructure/scripts/prod-remote-deploy.sh +++ b/infrastructure/scripts/prod-remote-deploy.sh @@ -66,9 +66,37 @@ if [ -f nginx/nginx.ssl.conf.template ]; then ./scripts/render-nginx-ssl.sh fi -docker compose -f docker-compose.prod.yml --env-file .env up -d --force-recreate nginx -docker compose -f docker-compose.prod.yml --env-file .env up -d +COMPOSE=(docker compose -f docker-compose.prod.yml --env-file .env) + +wait_healthy() { + local name="$1" + local seconds="${2:-300}" + local elapsed=0 + local status="missing" + echo "Waiting for $name healthy (up to ${seconds}s)..." + while [ "$elapsed" -lt "$seconds" ]; do + status="$(docker inspect -f '{{if .State.Health}}{{.State.Health.Status}}{{else}}{{.State.Status}}{{end}}' "$name" 2>/dev/null || echo missing)" + if [ "$status" = "healthy" ]; then + echo "$name is healthy" + return 0 + fi + sleep 3 + elapsed=$((elapsed + 3)) + done + echo "$name not healthy after ${seconds}s (last status=${status})" + docker logs "$name" --tail 50 || true + return 1 +} + +# Backend/frontend first so nginx never caches a dead upstream IP (502 on /api and /admin). +echo "Starting postgres, backend, frontend..." +"${COMPOSE[@]}" up -d postgres backend frontend +wait_healthy dyolink_backend_prod 300 +wait_healthy dyolink_frontend_prod 180 +echo "Recreating nginx after backends are reachable" +"${COMPOSE[@]}" up -d --force-recreate nginx +"${COMPOSE[@]}" up -d echo "=== Status ===" -docker compose -f docker-compose.prod.yml --env-file .env ps +"${COMPOSE[@]}" ps echo "Health: https://nudentic.ir/api/health" echo "AdminJS: https://nudentic.ir/admin" -- 2.53.0.windows.1