← back to Dw Marketing Reels

deploy/add-reels-passthrough.sh

46 lines

#!/usr/bin/env bash
# add-reels-passthrough.sh — run AS ROOT on Kamatera.
# Adds a PUBLIC /reels/*.mp4 passthrough to the LIVE marketing.designerwallcoverings.com
# nginx vhost so Meta can fetch reel media for Instagram publishing. It ONLY inserts a
# `location` block into the already-working vhost — it reuses that vhost's existing TLS
# cert (no cert reference, no cert change), which sidesteps the missing-cert issue that
# stalled the earlier full cutover. Idempotent + safe: aborts on `nginx -t` failure.
set -euo pipefail

echo "→ finding the reels app port (public /privacy probe, no auth)…"
PORT=""
for p in 9848 9600 9662 9846; do
  if [ "$(curl -s -o /dev/null -w '%{http_code}' "http://127.0.0.1:$p/privacy" 2>/dev/null)" = "200" ]; then PORT=$p; break; fi
done
[ -z "$PORT" ] && { echo "✗ could not find the reels app (tried :9848/9600/9662/9846). Is pm2 dw-marketing-reels up?"; exit 1; }
echo "  reels app on 127.0.0.1:$PORT"

echo "→ locating live vhost for marketing.designerwallcoverings.com…"
V=$(grep -rl "server_name marketing.designerwallcoverings.com" /etc/nginx/sites-available /etc/nginx/conf.d 2>/dev/null | head -1)
[ -z "$V" ] && { echo "✗ no vhost found for marketing.designerwallcoverings.com"; exit 1; }
echo "  vhost: $V"

if grep -qE 'location[^{]*/reels/' "$V"; then
  echo "  /reels/ passthrough already present — nothing to insert."
else
  cp "$V" "$V.bak-$(date +%s)" && echo "  backed up vhost"
  # Insert a public reels location immediately after each server_name line (lands it
  # inside the server block that owns the cert). awk is used so no fragile brace-matching.
  awk -v port="$PORT" '
    { print }
    /server_name[[:space:]]+marketing\.designerwallcoverings\.com/ {
      print "    location ~ ^/reels/[^/]+\\.mp4$ { proxy_pass http://127.0.0.1:" port "; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; }"
    }
  ' "$V" > "$V.new" && mv "$V.new" "$V"
  echo "  inserted:  location ~ ^/reels/[^/]+\\.mp4\$  →  http://127.0.0.1:$PORT"
fi

echo "→ nginx -t"
nginx -t
echo "→ reload"
systemctl reload nginx
echo "✓ RELOADED"
echo "→ verify (expect 200):"
f=$(ls -t /root/public-projects/dw-marketing-reels/reels/*.mp4 2>/dev/null | head -1 | xargs -r basename)
[ -n "$f" ] && curl -s -o /dev/null -w "  https://marketing.designerwallcoverings.com/reels/$f -> %{http_code}\n" "https://marketing.designerwallcoverings.com/reels/$f" || echo "  (no reel file found to test — check manually)"