# Deploy AlterFlixHub (static export) ด้วย Docker / Dokploy repo นี้เก็บ **ไฟล์ static export ที่ราก repo** — ไม่มี build step, image คือ nginx + ไฟล์เว็บ ## ไฟล์ที่เกี่ยวข้อง | ไฟล์ | หน้าที่ | |---|---| | `Dockerfile` | `nginx:1.27-alpine` + ไฟล์เว็บ → `/usr/share/nginx/html` · healthcheck `/healthz` | | `docker/default.conf.template` | nginx config (template) — clean URL, cache, redirect http→https | | `docker/security-headers.conf` | header ความปลอดภัยชุดเดียว ใช้ `include` ทุก location | | `docker/30-origin.sh` | ตอน start: แทน origin ใน canonical/OG/JSON-LD/sitemap/robots ตาม `PUBLIC_ORIGIN` | | `docker-compose.yml` | base สำหรับ Dokploy — **ไม่ publish พอร์ตออก host** (Traefik ต่อผ่าน network) | | `docker-compose.override.yml` | สำหรับรันในเครื่อง — แมป `127.0.0.1:${HOST_PORT}` | | `.dockerignore` | ตัด `.git`, compose, docs, ไฟล์สำรองออกจาก build context | ## env | ตัวแปร | ค่า default | ใช้ทำอะไร | |---|---|---| | `PORT` | `30004` | พอร์ตที่ nginx ฟังในคอนเทนเนอร์ (ตั้งใน Dokploy = `30004`) | | `PUBLIC_ORIGIN` | `https://alterflixhub.hobbypj.com` | origin ที่เขียนลง canonical / og:url / JSON-LD / sitemap / robots | | `BAKED_ORIGIN` | `https://alterflixhub.hobbypj.com` | origin ที่อบมากับไฟล์ — ต้นทางของการแทนที่ (ปกติไม่ต้องแตะ) | | `HOST_PORT` | `30004` | (เฉพาะในเครื่อง) พอร์ตที่แมปออก host | | `TZ` | `Asia/Bangkok` | timezone ของ log | เปลี่ยนโดเมน = ตั้ง `PUBLIC_ORIGIN=https://โดเมนใหม่` แล้ว redeploy (สคริปต์แทนให้เองตอน start, 36 ไฟล์) หรือแก้ `BAKED_ORIGIN` ให้ตรงกับที่อบไว้ถ้าเคยอบจากโดเมนอื่น ## http → https nginx ดู `X-Forwarded-Proto` ที่ Traefik/Dokploy ส่งมา — ถ้าเป็น `http` จะตอบ **301 ไป `https://$host$request_uri`** (คง path + query) `/healthz` เป็นข้อยกเว้น (healthcheck วิ่ง http ภายในได้) · เปิด HSTS `max-age=15552000` ให้แล้ว ## ตั้งใน Dokploy 1. สร้าง Application → Provider **Docker** / Build type **Dockerfile** (path `./Dockerfile`), Compose file `./docker-compose.yml` 2. Environment: `PORT=30004` (`PUBLIC_ORIGIN` ตั้งเมื่อโดเมนไม่ใช่ค่า default) 3. Domains: `alterflixhub.hobbypj.com` → container port **30004**, เปิด **HTTPS** (Let's Encrypt) + redirect 4. Deploy — healthcheck ต้องผ่าน (`wget -qO- http://127.0.0.1:$PORT/healthz`) > Dokploy เรียก `docker compose -f ./docker-compose.yml` → ไม่โหลด `docker-compose.override.yml` (พอร์ตบน server จึงไม่ชน) ## เพิ่มโดเมนผ่าน Traefik File System (Dokploy) Dokploy → เมนูซ้าย **Web Server → Traefik File System** (URL `/dashboard/traefik`) → โฟลเดอร์ `dynamic/` → **New File** ตั้งชื่อ `alterflixhub-hobbypj-com.yml` แล้ววางเนื้อหาจาก [`docker/traefik-dynamic.example.yml`](../docker/traefik-dynamic.example.yml) (แก้ชื่อ container ให้ตรงจริง) ```yaml http: routers: alterflixhub-web: rule: "Host(`alterflixhub.hobbypj.com`)" entryPoints: [web] middlewares: [alterflixhub-redirect-https] service: alterflixhub-service alterflixhub-websecure: rule: "Host(`alterflixhub.hobbypj.com`)" entryPoints: [websecure] service: alterflixhub-service tls: certResolver: letsencrypt middlewares: alterflixhub-redirect-https: redirectScheme: {scheme: https, permanent: true} services: alterflixhub-service: loadBalancer: servers: - url: "http://web-alterflixhub-web-1:30004" # ชื่อ container : พอร์ตในคอนเทนเนอร์ passHostHeader: true healthCheck: {path: /healthz, interval: 30s, timeout: 3s} ``` เงื่อนไขที่ต้องครบ ไม่งั้นได้ 404 / 502 1. **DNS** `alterflixhub.hobbypj.com` → IP เครื่องที่รัน Dokploy (พอร์ต 80 เปิด เพื่อ Let's Encrypt HTTP-01) 2. **Traefik ต่อถึง container ได้** — container ต้องอยู่ใน `dokploy-network` และชื่อต้องตรงกับ `url:` - หาชื่อจริงบน server: `docker ps --format '{{.Names}}\t{{.Ports}}' | grep alterflix` - ถ้ายังไม่เข้า network: `docker network connect dokploy-network ` - ทางเลือกที่ยั่งยืนกว่า: ให้ compose ต่อ `dokploy-network` เอง (เพิ่ม `networks: [dokploy-network]` + `networks: dokploy-network: external: true`) แล้ว redeploy 3. **entrypoints ต้องมีจริง** — `web` = :80, `websecure` = :443 คือค่าที่ Dokploy สร้างให้ใน `traefik.yml` อยู่แล้ว (ห้ามลบ/เปลี่ยนชื่อ) ไฟล์ใน `dynamic/` ถูก watch → save แล้วมีผลทันที ไม่ต้อง restart Traefik ตรวจหลัง save ```bash curl -I https://alterflixhub.hobbypj.com/ # 200 curl -I http://alterflixhub.hobbypj.com/cinema/kku-cinema # 301 → https docker logs --tail=40 dokploy-traefik | grep -i -E 'alterflix|error' ``` > ทางที่ Dokploy ทำให้เอง: ใส่ domain ในแท็บ **Domains** ของ application (Dokploy เขียนไฟล์ dynamic ให้เอง > — สำหรับ Compose app จะใช้ Traefik labels ซึ่งต้อง redeploy ก่อนมีผล) · ถ้าใช้ Files System ด้วยมือ > ให้ลบ domain เดียวกันออกจากแท็บ Domains ก่อน ไม่งั้นจะได้ router ซ้ำกัน ## รันในเครื่อง (preview) ```bash docker compose up -d --build # http://127.0.0.1:30004/ docker compose logs -f web docker compose down PORT=31004 HOST_PORT=31004 PUBLIC_ORIGIN=https://staging.example.com docker compose up -d # ทดสอบ env อื่น ``` ## Cache ที่ตั้งไว้ | path | Cache-Control | |---|---| | `/assets/*` (JS/CSS) | `no-cache` — แคชได้แต่ revalidate ทุกครั้ง | | รูป/ฟอนต์ | `public, max-age=2592000` | | HTML ทุกหน้า | `no-cache` (ต้อง revalidate) | | `sw.js`, `manifest.json` | `no-cache` | > **ทำไม `/assets/` ไม่ใช้ `immutable`:** ในโหมด publish static export เราแก้ไฟล์ CSS/JS ด้วยมือโดยชื่อเดิมบ่อย > ถ้า cache ยาว เครื่องที่เคยโหลดจะค้างไฟล์เก่าเป็นปี (เคสจริง: fix scroll/wheel ไม่ถึงผู้ใช้) > เวลาแก้ CSS/JS ให้ **เปลี่ยนชื่อไฟล์เป็น hash ใหม่ + แก้ `` ใน HTML** ด้วย แล้ว bump `VERSION` ใน `sw.js` `sw.js` เป็น cache-first สำหรับ `/assets/*` → **ทุกครั้งที่แก้ CSS/JS ในไฟล์ export ต้อง bump `const VERSION` ใน `sw.js`** ## แก้โดเมน/ค่าในไฟล์ export - canonical/OG/sitemap/robots: ตั้ง `PUBLIC_ORIGIN` (runtime) หรือแทนสตริงในไฟล์ตอน publish - อย่าแก้ attribute ใน HTML ด้วยมือหลังจากที่ไฟล์ถูก hydrate แล้ว — จะ hydration mismatch - หน้า `/cinema/` ไม่มี trailing slash (canonical เป็นแบบนั้น) — nginx `try_files $uri $uri/index.html` เสิร์ฟตรง ไม่ redirect