Nginx · Redis · SSL · Lua

Overview
Main server acts as orchestrator only — authentication, token generation, CDN routing. It never touches stream data. Streams flow directly from the stream servers through CDN proxies to clients. Each client network has a local CDN for low latency. A public CDN-Z serves as fallback.
Auth, tokens, orchestration. No stream data.
Token validation via Redis, proxy to the stream server.
Source stream servers (HLS/MPEG-TS).
IPTV player (VLC, STB, mobile app).
Client authenticates (B2C API / play link)
Main creates token in Redis (source URL, 24h TTL)
Main redirects client to best CDN (302)
Client follows redirect to CDN
CDN validates token via Main's Redis (Net X)
CDN proxies stream from the stream server to client
fallback
public internet
CdnRouter checks CDN health every 30s via /health endpoint. If the best CDN for client's subnet is down, it falls back to the next matching CDN (sorted by CIDR prefix length, most specific first).
No CDN healthy → local /cdn/ on Main (safety net)
Format
Redirect URL
CdnToken automatically detects and handles different source URL formats. For MPEG-TS or bare stream paths, it probes the server for HLS availability via HEAD request.
HLS playlist — used directly, master playlist filtered to single video variant + all audio tracks
Raw MPEG-TS — auto-converted to HLS if the server supports it, otherwise proxied raw with buffering off
File (mp4, m4s, ts, mkv, etc.) — proxied directly as-is, no conversion
DASH (mpd) — proxied directly as-is
For HLS streams, nginx body filter rewrites the master playlist (index.m3u8) to keep only the first video variant while preserving all audio/subtitle tracks. This prevents players from downloading multiple video qualities simultaneously.
Before (from stream server)
After (delivered to client)
Generates 32-char hex tokens, auto-detects format (HLS/MPEG-TS/file), normalizes URL, stores in Redis with SETEX (24h TTL).
Finds best healthy CDN by client IP (CIDR match + health check). Fallback chain.
Auto-installs nginx + lua-resty-redis + Redis on CDN servers. Injects /cdn/ location into main nginx.
DB model: domain, ip_scope (CIDR), redis_host (Main IP on Net X), health status.