Skip to content
📦 ARCHIVED SNAPSHOT (v1.0) You are viewing historical business logic and documentation for VPS Infrastructure v1.0 (frozen on 2026-11-23, commit b3a7ad3). Switch to Latest →

1. Edge Reverse Proxy, SNI Stream Routing, IP Access Control & Error Pages

Repository: VPS Infrastructure (planovi-vps) • Rule Prefix: BR-EDGE • Documented Rules: 12 • Open [TEAM_WHY_NEEDED] Questions: 3

Business Purpose & Actors

  • Problem solved: Single network ingress gateway (vps-proxy) terminating TLS for all 21+ *.planovi.app hostnames, multiplexing raw mTLS IoT device telemetry and MQTT traffic on ports 443/8883 alongside Cloudflare-proxied HTTPS web/API traffic, blocking origin-IP bypass attacks, restricting admin/docs surfaces to approved IP addresses, and serving internal GoTrue email templates without transiting Cloudflare.
  • Actors: End users (browsers/mobile apps via Cloudflare), IoT hardware gateways (planovi-converter RP2350 devices over direct mTLS / MQTT), GitHub webhook delivery servers (via Cloudflare), internal Docker containers (supabase-auth/supabase-auth-dev), CERT Polska scanner (moje.cert.pl), and VPS administrators running scripts/update-allowed-ip.sh.

All Features & End-to-End Flows

  1. L4 TCP SNI Multiplexing on Port 443 (proxy/nginx.conf:115-137):
    • Trigger: TCP connection arrives on host port 443.
    • Validation: Nginx stream reads TLS ClientHello SNI via ssl_preread on; without terminating TLS.
    • Routing: dev-telemetry.planovi.app -> 191.218.165.149:8443 (with proxy_protocol on;), telemetry.planovi.app -> 191.218.165.149:8444 (with proxy_protocol on;), all other SNIs (default) -> 127.0.0.1:8445 (internal HTTP/2 TLS termination with proxy_protocol on;).
    • Failure: If upstream :8443/:8444 is down, TCP connection drops at L4 (logged in /var/log/nginx/access.log with stream_routing format).
  2. L4 MQTT-over-TLS SNI Routing on Port 8883 (proxy/nginx.conf:126-147):
    • Trigger: TCP connection on port 8883.
    • Validation: IP allowlist (127.0.0.1, 172.18.0.1, /etc/nginx/allowed_ips.conf), then SNI lookup (mqtt.planovi.app & dev-mqtt.planovi.app -> mosquitto-dev:8883; default -> 127.0.0.1:9 discard blackhole).
    • Failure: Non-whitelisted IP rejected at TCP level; unknown SNI forwarded to discard port 9.
  3. Cloudflare Origin Guard & Real-IP Extraction (proxy/nginx.conf:15-80):
    • Trigger: HTTP/2 request arrives on internal listener 8445 ssl proxy_protocol.
    • Validation: geo $proxy_protocol_addr $is_cloudflare checks whether the TCP peer passed via PROXY protocol belongs to Cloudflare IPv4/IPv6 ranges, Docker/loopback (127.0.0.1/32, 172.16.0.0/12, 10.0.0.0/8), VPS self IP (191.218.165.149/32), or CERT Polska (195.187.239.0/24).
    • Behavior: If $is_cloudflare = 0, vhost immediately terminates connection with Nginx non-standard return 444; (silent TCP close, zero bytes returned). Real client IP is extracted from CF-Connecting-IP (real_ip_recursive on;).
  4. Domain Virtual Host Routing (proxy/conf.d/*.conf):
    • Marketing webpage: planovi.app, www.planovi.app, dev-webpage.planovi.app -> http://php-app:80 (apps.conf:1-56).
    • Flutter Web SPAs: panel.planovi.app -> static root /opt/vps-apps/flutter-app/prod/build/web; dev-panel.planovi.app -> /opt/vps-apps/flutter-app/dev/build/web (apps.conf:58-152).
    • PHP Microservices: dealflow/dev-dealflow, scheduler/dev-scheduler, agreements/dev-agreements, offers/dev-offers -> http://php-app:80 (apps.conf:154-348).
    • Converters Admin & API: cloud.planovi.app (SPA /opt/vps-apps/converters/prod/admin/build/web + /api/v1/ -> http://converters-api-prod:8000) and dev-cloud.planovi.app (SPA /opt/vps-apps/converters/dev/admin/build/web + /api/v1/ -> http://converters-api-dev:8000) (converters.conf:1-121).
    • Supabase PROD API & Webhook: api.planovi.app -> /deploy-webhook -> http://vps-deployer:9000/deploy-webhook; / -> http://supabase-kong:8000/ (supabase.conf:144-175).
    • Supabase DEV API: dev-api.planovi.app -> /rest/v1/, /auth/v1/, /storage/v1/, /functions/v1/, / -> http://supabase-kong-dev:8000 (dev-api.conf:11-69).
    • Supabase Studio (PROD & DEV): supabase-studio.planovi.app / supabase.planovi.app -> http://supabase-studio:3000 (IP-whitelisted on /, public on /rest/v1/, /auth/v1/, /storage/v1/, /functions/v1/); dev-supabase-studio.planovi.app -> http://supabase-studio-dev:3000 (adds X-Studio-Env: DEV; /functions/v1/ proxies directly to http://supabase-edge-runtime-dev:9000/ bypassing Kong) (supabase.conf:12-138).
    • Docs Portal: docs.planovi.app, dev-docs.planovi.app -> IP-whitelisted reverse proxy to Cloudflare Pages https://dev-docs-arq.pages.dev with proxy_ssl_server_name on; and external DNS resolver 1.1.1.1 8.8.8.8 valid=300s (docs.conf:13-43).
    • Internal Email Templates: internal-only port 8080 (allow 172.16.0.0/12; deny all;) serving /email-templates/ from /opt/vps-apps/microservices/dev/dealflow/email-templates/ (internal-email-templates.conf:3-19).
    • Unmapped / Direct-IP Fallback: default_server on 80 and 8445 serves /.well-known/mojecertpl/verification (200 text/plain) and drops non-Cloudflare traffic (444) or returns 404 rendered via /_fallback/404.html (apps.conf:353-383).
  5. Dynamic IP Whitelist Update (scripts/update-allowed-ip.sh):
    • Trigger: Admin invokes ./scripts/update-allowed-ip.sh <IPv4>.
    • Validation: Rejects missing arg, "auto", or strings failing regex ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ with exit code 1.
    • State Change: Overwrites proxy/allowed_ips.conf with allow <IP>; deny all;, resets UFW (default deny incoming, allows 80/tcp, 443/tcp, 22/tcp), and runs zero-downtime docker compose exec -T proxy nginx -s reload.

Hard Business Rules, Validations, Calculations & Edge Cases

  • BR-EDGE-01 (Origin Bypass Drop): Every HTTPS vhost on port 8445 MUST evaluate if ($is_cloudflare = 0) { return 444; } (proxy/nginx.conf:15-44, proxy/conf.d/*.conf). Direct public connections to 191.218.165.149:443 outside Cloudflare/allowlisted CIDRs receive an immediate TCP drop with no HTTP status or TLS leak beyond Cloudflare Origin cert.
  • BR-EDGE-02 (TLS & Upload Limits): HTTP layer enforces TLSv1.2/TLSv1.3 only, ssl_session_tickets off, ssl_session_cache shared:SSL:50m (1d timeout), server_tokens off, client_max_body_size 500M, client_body_buffer_size 128k, worker_connections 8192, worker_rlimit_nofile 65535, and Docker embedded DNS resolver 127.0.0.11 valid=5s ipv6=off (proxy/nginx.conf:3,8,46,88,91-92,105-110).
  • BR-EDGE-03 (Flutter SPA Cache Policy): panel.planovi.app and dev-panel.planovi.app MUST serve main.dart.js, flutter_bootstrap.js, flutter.js, flutter_service_worker.js, version.json, and manifest.json with Cache-Control: no-cache, while other static assets (.css, .js, .png, .wasm, .woff2, etc.) get expires 7d; Cache-Control: public, no-transform (proxy/conf.d/apps.conf:88-95,136-143).
  • BR-EDGE-04 (Converter Admin Cache Policy): cloud.planovi.app and dev-cloud.planovi.app force expires -1; Cache-Control: no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0 on all static asset extensions (proxy/conf.d/converters.conf:33-36,94-97).
  • BR-EDGE-05 (Static Asset Whitelist Bypass — Anomaly): Because Nginx evaluates regex location ~* before prefix location /, proxy/conf.d/converters.conf:33-36,94-97 serves JS/CSS/WASM files without include /etc/nginx/allowed_ips.conf;, bypassing the IP restriction applied to location /.
  • BR-EDGE-06 (Sensitive File Blocking): planovi.app/dev-webpage.planovi.app block hidden dotfiles (except /.well-known/), /docs/, /includes/, and .sh|.md|.lock|.yml|.yaml|.env|.bak|.log with HTTP 404 (proxy/conf.d/apps.conf:37-39). dealflow/dev-dealflow and scheduler/dev-scheduler additionally block /data/, /vendor/, *log*.txt, composer.(json|lock), /backend/*.json, and *(client_secret|credentials)*.json with HTTP 404 (proxy/conf.d/apps.conf:162-168,179-185,220-226,237-243). agreements and offers vhosts omit these block rules.
  • BR-EDGE-07 (CERT Polska Verification Endpoint): /.well-known/mojecertpl/verification returns HTTP 200 text/plain on both port 80 and 8445 for planovi.app/dev-webpage.planovi.app and default_server (proxy/conf.d/apps.conf:8-11,31-34,363-366), and 195.187.239.0/24 is permitted in $is_cloudflare (proxy/nginx.conf:19).
  • BR-EDGE-08 (Internal Email Templates Restriction): Port 8080 is NOT published in docker-compose.yml and enforces allow 172.16.0.0/12; deny all; (proxy/conf.d/internal-email-templates.conf:4-8). Only /email-templates/ returns text/html; charset=utf-8; all other paths return 404.
  • BR-EDGE-09 (Dynamic Upstream Resolution for Converters & Docs): converters.conf:40,101 and docs.conf:34 assign upstream hosts to Nginx variables (set $converters_prod ..., set $docs_upstream ...) so Nginx resolves DNS at request time via resolver rather than crashing at startup or caching stale container IPs after container rebuilds (commit 650a7dd).
  • BR-EDGE-10 (Single-IP Overwrite Behavior): scripts/update-allowed-ip.sh:31-36 replaces the entire proxy/allowed_ips.conf file with one IPv4 entry (allow $NEW_IP; deny all;), overwriting any manually added IPs, and does not open port 8883/tcp in UFW (scripts/update-allowed-ip.sh:45-48).
  • BR-EDGE-11 (HTTP-to-HTTPS Redirect Exception): api.planovi.app shares port 80 redirect in supabase.conf:7, while dev-api.planovi.app has its own port 80 redirect (dev-api.conf:5-9). Every non-verification path on port 80 redirects 301 https://$host$request_uri.
  • BR-EDGE-12 (Bilingual Fallback Error Pages): Custom /_fallback/404.html and /_fallback/50x.html are marked internal; and provide client-side PL/EN toggling (defaulting to PL) with CTA back to https://planovi.app or window.location.reload() (proxy/html/404.html:178-221, proxy/html/50x.html:198-248). proxy/html/default.html is unreferenced by any vhost.

State Machines & Data Lifecycle

Not a multi-state transactional workflow (stateless packet/HTTP routing with file-backed config reload via nginx -s reload).

Technical Contract

  • Inputs: TCP :80, :443, :8883 (host-exposed); internal Docker TCP :8080 (vps-proxy:8080).
  • Local Files Read (Read-Only Mounts): ./proxy/nginx.conf, ./proxy/allowed_ips.conf, ./proxy/conf.d/*.conf, ./proxy/html/*.html, ./cloudflare-ssl/fullchain.pem & privkey.pem, /opt/vps-apps/*.
  • Side Effects: Access & error logs to /var/log/nginx/{access,error}.log (Docker json-file driver, max-size: 20m, max-file: 3). update-allowed-ip.sh mutates proxy/allowed_ips.conf and host UFW rules.

Cross-Repository Dependencies

  • Cloudflare Edge -> vps-proxy:443 | HTTPS + CF-Connecting-IP | All public *.planovi.app domains.
  • planovi-converter (RP2350 hardware) -> vps-proxy:443 | Raw TCP + TLS SNI (telemetry.planovi.app / dev-telemetry.planovi.app) | Forwarded via proxy_protocol to host :8444 / :8443 (external planovi-converter mTLS ingress container).
  • planovi-converter (MQTT clients) -> vps-proxy:8883 | Raw TCP + TLS SNI (mqtt.planovi.app / dev-mqtt.planovi.app) | Forwarded to mosquitto-dev:8883 (IP-whitelisted).
  • vps-proxy -> planovi-docs (Cloudflare Pages) | HTTPS (dev-docs-arq.pages.dev) | Proxied for docs.planovi.app & dev-docs.planovi.app.
  • supabase-auth / supabase-auth-dev -> vps-proxy:8080 | Internal HTTP (172.16.0.0/12) | Reads /opt/vps-apps/microservices/dev/dealflow/email-templates/*.html (from planovi-microservices).
  • vps-proxy -> vps-deployer:9000 | Internal HTTP | POST https://api.planovi.app/deploy-webhook.

Why It Works This Way

  • L4 stream SNI + 127.0.0.1:8445 loopback: Allows RP2350 IoT gateways to perform end-to-end mTLS directly against the converter ingress container on :8443/:8444 (bypassing Cloudflare, which does not pass custom device CA mTLS on free/standard plans) while keeping standard HTTPS websites on port 443 (proxy/nginx.conf:115-137, commit fe9bc08).
  • Internal port 8080 email template server: Comment in proxy/conf.d/internal-email-templates.conf:1 explicitly states GoTrue fetches templates internally to bypass Cloudflare.
  • Dynamic DNS variable in converters.conf: Commit 650a7dd switched converters.conf to variable-based proxy_pass so vps-proxy does not cache stale container IPs when converters-api-* is recreated by vps-deployer.
  • [TEAM_WHY_NEEDED: Why does internal-email-templates.conf:11 serve GoTrue templates for BOTH PROD (supabase-auth) and DEV (supabase-auth-dev) from /opt/vps-apps/microservices/dev/dealflow/email-templates/ instead of separating PROD templates?]
  • [TEAM_WHY_NEEDED: Why do proxy/conf.d/supabase.conf lines 188-247 still define HTTPS server blocks on port 8445 for telemetry.planovi.app and dev-telemetry.planovi.app when proxy/nginx.conf stream SNI routes them exclusively to 191.218.165.149:8444 and :8443?]
  • [TEAM_WHY_NEEDED: Why does mqtt.planovi.app route to mosquitto-dev:8883 instead of a dedicated mosquitto-prod broker, and is port 8883 intentionally restricted by allowed_ips.conf?]

Debugging, Logs & Common Failure Modes

  • Symptom: Browser/API gets Empty reply from server / connection closed immediately -> Root Cause: Client hit VPS IP directly bypassing Cloudflare ($is_cloudflare = 0 -> return 444) -> Verify: docker logs --tail 50 vps-proxy (look for status 444 and real_ip) -> Fix: Route DNS through Cloudflare orange-cloud proxy or add trusted scanner CIDR to proxy/nginx.conf:15-44.
  • Symptom: 403 Forbidden on supabase-studio.planovi.app, dev-supabase-studio.planovi.app, docs.planovi.app, cloud.planovi.app, or dev-cloud.planovi.app -> Root Cause: Client public IP not in proxy/allowed_ips.conf -> Verify: cat /opt/vps-stack/proxy/allowed_ips.conf & docker logs --tail 20 vps-proxy -> Fix: Run /opt/vps-stack/scripts/update-allowed-ip.sh <IP> (or manually append allow <IP>; above deny all; to preserve multiple IPs and run docker exec vps-proxy nginx -s reload).
  • Symptom: 502 Bad Gateway on /rest/v1/, /functions/v1/, or /api/v1/ after container recreate -> Root Cause: Static proxy_pass cached old container IP or target container crashed -> Verify: docker ps -a and docker exec vps-proxy nginx -t -> Fix: docker exec vps-proxy nginx -s reload.
  • Symptom: 50x.html displays Chinese character 厺 on refresh button -> Root Cause: Typo HTML entity &#21434; in proxy/html/50x.html:217,231,237 -> Fix: Replace with &#8635;.