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.apphostnames, multiplexing raw mTLS IoT device telemetry and MQTT traffic on ports443/8883alongside 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-converterRP2350 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 runningscripts/update-allowed-ip.sh.
All Features & End-to-End Flows
- L4 TCP SNI Multiplexing on Port 443 (
proxy/nginx.conf:115-137):- Trigger: TCP connection arrives on host port
443. - Validation: Nginx
streamreads TLS ClientHello SNI viassl_preread on;without terminating TLS. - Routing:
dev-telemetry.planovi.app->191.218.165.149:8443(withproxy_protocol on;),telemetry.planovi.app->191.218.165.149:8444(withproxy_protocol on;), all other SNIs (default) ->127.0.0.1:8445(internal HTTP/2 TLS termination withproxy_protocol on;). - Failure: If upstream
:8443/:8444is down, TCP connection drops at L4 (logged in/var/log/nginx/access.logwithstream_routingformat).
- Trigger: TCP connection arrives on host port
- 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:9discard blackhole). - Failure: Non-whitelisted IP rejected at TCP level; unknown SNI forwarded to discard port
9.
- Trigger: TCP connection on port
- 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_cloudflarechecks 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-standardreturn 444;(silent TCP close, zero bytes returned). Real client IP is extracted fromCF-Connecting-IP(real_ip_recursive on;).
- Trigger: HTTP/2 request arrives on internal listener
- 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) anddev-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(addsX-Studio-Env: DEV;/functions/v1/proxies directly tohttp://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 Pageshttps://dev-docs-arq.pages.devwithproxy_ssl_server_name on;and external DNS resolver1.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_serveron80and8445serves/.well-known/mojecertpl/verification(200 text/plain) and drops non-Cloudflare traffic (444) or returns404rendered via/_fallback/404.html(apps.conf:353-383).
- Marketing webpage:
- 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 code1. - State Change: Overwrites
proxy/allowed_ips.confwithallow <IP>; deny all;, resets UFW (default deny incoming, allows80/tcp,443/tcp,22/tcp), and runs zero-downtimedocker compose exec -T proxy nginx -s reload.
- Trigger: Admin invokes
Hard Business Rules, Validations, Calculations & Edge Cases
- BR-EDGE-01 (Origin Bypass Drop): Every HTTPS vhost on port
8445MUST evaluateif ($is_cloudflare = 0) { return 444; }(proxy/nginx.conf:15-44,proxy/conf.d/*.conf). Direct public connections to191.218.165.149:443outside 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(1dtimeout),server_tokens off,client_max_body_size 500M,client_body_buffer_size 128k,worker_connections 8192,worker_rlimit_nofile 65535, and Docker embedded DNSresolver 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.appanddev-panel.planovi.appMUST servemain.dart.js,flutter_bootstrap.js,flutter.js,flutter_service_worker.js,version.json, andmanifest.jsonwithCache-Control: no-cache, while other static assets (.css,.js,.png,.wasm,.woff2, etc.) getexpires 7d; Cache-Control: public, no-transform(proxy/conf.d/apps.conf:88-95,136-143). - BR-EDGE-04 (Converter Admin Cache Policy):
cloud.planovi.appanddev-cloud.planovi.appforceexpires -1; Cache-Control: no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0on 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 prefixlocation /,proxy/conf.d/converters.conf:33-36,94-97serves JS/CSS/WASM files withoutinclude /etc/nginx/allowed_ips.conf;, bypassing the IP restriction applied tolocation /. - BR-EDGE-06 (Sensitive File Blocking):
planovi.app/dev-webpage.planovi.appblock hidden dotfiles (except/.well-known/),/docs/,/includes/, and.sh|.md|.lock|.yml|.yaml|.env|.bak|.logwith HTTP404(proxy/conf.d/apps.conf:37-39).dealflow/dev-dealflowandscheduler/dev-scheduleradditionally block/data/,/vendor/,*log*.txt,composer.(json|lock),/backend/*.json, and*(client_secret|credentials)*.jsonwith HTTP404(proxy/conf.d/apps.conf:162-168,179-185,220-226,237-243).agreementsandoffersvhosts omit these block rules. - BR-EDGE-07 (CERT Polska Verification Endpoint):
/.well-known/mojecertpl/verificationreturns HTTP200text/plainon both port80and8445forplanovi.app/dev-webpage.planovi.appanddefault_server(proxy/conf.d/apps.conf:8-11,31-34,363-366), and195.187.239.0/24is permitted in$is_cloudflare(proxy/nginx.conf:19). - BR-EDGE-08 (Internal Email Templates Restriction): Port
8080is NOT published indocker-compose.ymland enforcesallow 172.16.0.0/12; deny all;(proxy/conf.d/internal-email-templates.conf:4-8). Only/email-templates/returnstext/html; charset=utf-8; all other paths return404. - BR-EDGE-09 (Dynamic Upstream Resolution for Converters & Docs):
converters.conf:40,101anddocs.conf:34assign upstream hosts to Nginx variables (set $converters_prod ...,set $docs_upstream ...) so Nginx resolves DNS at request time viaresolverrather than crashing at startup or caching stale container IPs after container rebuilds (commit650a7dd). - BR-EDGE-10 (Single-IP Overwrite Behavior):
scripts/update-allowed-ip.sh:31-36replaces the entireproxy/allowed_ips.conffile with one IPv4 entry (allow $NEW_IP; deny all;), overwriting any manually added IPs, and does not open port8883/tcpin UFW (scripts/update-allowed-ip.sh:45-48). - BR-EDGE-11 (HTTP-to-HTTPS Redirect Exception):
api.planovi.appshares port80redirect insupabase.conf:7, whiledev-api.planovi.apphas its own port80redirect (dev-api.conf:5-9). Every non-verification path on port80redirects301 https://$host$request_uri. - BR-EDGE-12 (Bilingual Fallback Error Pages): Custom
/_fallback/404.htmland/_fallback/50x.htmlare markedinternal;and provide client-side PL/EN toggling (defaulting to PL) with CTA back tohttps://planovi.apporwindow.location.reload()(proxy/html/404.html:178-221,proxy/html/50x.html:198-248).proxy/html/default.htmlis 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(Dockerjson-filedriver,max-size: 20m,max-file: 3).update-allowed-ip.shmutatesproxy/allowed_ips.confand host UFW rules.
Cross-Repository Dependencies
Cloudflare Edge -> vps-proxy:443| HTTPS +CF-Connecting-IP| All public*.planovi.appdomains.planovi-converter (RP2350 hardware) -> vps-proxy:443| Raw TCP + TLS SNI (telemetry.planovi.app/dev-telemetry.planovi.app) | Forwarded viaproxy_protocolto host:8444/:8443(externalplanovi-convertermTLS ingress container).planovi-converter (MQTT clients) -> vps-proxy:8883| Raw TCP + TLS SNI (mqtt.planovi.app/dev-mqtt.planovi.app) | Forwarded tomosquitto-dev:8883(IP-whitelisted).vps-proxy -> planovi-docs (Cloudflare Pages)| HTTPS (dev-docs-arq.pages.dev) | Proxied fordocs.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(fromplanovi-microservices).vps-proxy -> vps-deployer:9000| Internal HTTP |POST https://api.planovi.app/deploy-webhook.
Why It Works This Way
- L4
streamSNI +127.0.0.1:8445loopback: 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 port443(proxy/nginx.conf:115-137, commitfe9bc08). - Internal port
8080email template server: Comment inproxy/conf.d/internal-email-templates.conf:1explicitly states GoTrue fetches templates internally to bypass Cloudflare. - Dynamic DNS variable in
converters.conf: Commit650a7ddswitchedconverters.confto variable-basedproxy_passsovps-proxydoes not cache stale container IPs whenconverters-api-*is recreated byvps-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 forstatus 444andreal_ip) -> Fix: Route DNS through Cloudflare orange-cloud proxy or add trusted scanner CIDR toproxy/nginx.conf:15-44. - Symptom:
403 Forbiddenonsupabase-studio.planovi.app,dev-supabase-studio.planovi.app,docs.planovi.app,cloud.planovi.app, ordev-cloud.planovi.app-> Root Cause: Client public IP not inproxy/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 appendallow <IP>;abovedeny all;to preserve multiple IPs and rundocker exec vps-proxy nginx -s reload). - Symptom:
502 Bad Gatewayon/rest/v1/,/functions/v1/, or/api/v1/after container recreate -> Root Cause: Staticproxy_passcached old container IP or target container crashed -> Verify:docker ps -aanddocker exec vps-proxy nginx -t-> Fix:docker exec vps-proxy nginx -s reload. - Symptom:
50x.htmldisplays Chinese character厺on refresh button -> Root Cause: Typo HTML entity厺inproxy/html/50x.html:217,231,237-> Fix: Replace with↻.