fix(ci): prod nginx re-resolves backend via Docker DNS; deploy starts nginx last
All checks were successful
Production — tag build, push, deploy / build-and-push (push) Successful in 58s
Production — tag build, push, deploy / deploy (push) Successful in 46s

This commit is contained in:
2026-09-19 09:26:06 +03:30
parent 0324589928
commit fae7e0cd53
5 changed files with 75 additions and 16 deletions

View File

@@ -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

View File

@@ -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"

View File

@@ -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';

View File

@@ -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

View File

@@ -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"