π host networking so the proxy reaches the backend on loopback
Browse files at this commitf643e6af0a1b2b2993e7b35970a6082f32f369f3Parent: 8970f61- fix
- π host networking so the proxy reaches the backend on loopback
5 changed files +51 -47
- README.md +2 -2
- compose.yaml +11 -12
- deploy/hezarfen_frontend.env.example +25 -26
- deploy/hezarfen_frontend_compose.service +7 -5
- server.ts +6 -2
README.mdmodified+2 -2
| @@ -63,9 +63,9 @@ podman compose logs -f frontend | ||
| 63 | 63 | podman compose down # stop |
| 64 | 64 | ``` |
| 65 | 65 | |
| 66 | -The `Containerfile` is a two-stage build: `bun install --frozen-lockfile` plus `bun run build` produce `dist/`, and `oven/bun:1-slim` serves it with `server.ts` β which also reverse-proxies `/api/*` (plain HTTP and the exam-room WebSocket) to `BACKEND_ORIGIN`. No nginx, no node_modules at runtime, no state: one container and no volumes. The port is published on loopback only β `127.0.0.1:<PORT>:<PORT>`, 5173 by default β so a server deployment terminates TLS in a reverse proxy in front of it. | |
| 66 | +The `Containerfile` is a two-stage build: `bun install --frozen-lockfile` plus `bun run build` produce `dist/`, and `oven/bun:1-slim` serves it with `server.ts` β which also reverse-proxies `/api/*` (plain HTTP and the exam-room WebSocket) to `BACKEND_ORIGIN`. No nginx, no node_modules at runtime, no state: one container and no volumes. It runs with `network_mode: host`, so it shares the host's loopback and reaches a backend published on `127.0.0.1:7656` exactly as a host process would (a bridge network would resolve `127.0.0.1` to the container itself). Host networking means no port mapping: the app binds the host's interface directly, so set `HOST=127.0.0.1` and let the reverse proxy on the same loopback be the only door. | |
| 67 | 67 | |
| 68 | -**Knobs live in two places and are never mixed.** Locally the `${VAR:-default}` interpolations in `compose.yaml` are enough: the `up` above works with zero extra files. On a server the operator copies `deploy/hezarfen_frontend.env.example` to `$HOME/hezarfen_frontend/hezarfen_frontend.env`, `chmod 0600` it and edits it β nothing automated creates, overwrites or uploads that file. The service declares it as `env_file:`, so **every key it carries reaches the container with no extra flag**; `BACKEND_ORIGIN` (the proxy target) is one of them, and any further key is passed through untouched. Only `PORT` is also interpolated by compose β it has to be, since the published port and the container listener come from one value β so give it `--env-file` if you change it (`podman compose --env-file hezarfen_frontend.env up -d --build`); the systemd unit and the deploy always do. | |
| 68 | +**Knobs live in two places and are never mixed.** On a laptop nothing is required: with no env file the app falls back to its own defaults (`BACKEND_ORIGIN=https://hezarfen.dizey.sh`, `HOST=0.0.0.0`, `PORT=5173`), so the `up` above works with zero extra files. On a server the operator copies `deploy/hezarfen_frontend.env.example` to `$HOME/hezarfen_frontend/hezarfen_frontend.env`, `chmod 0600` it and edits it β nothing automated creates, overwrites or uploads that file. The service declares it as `env_file:`, so **every key it carries reaches the container with no extra flag**; nothing else is interpolated from it, and the only compose-level variable is `HEZARFEN_TAG`, which the deploy owns in `stack.env`. | |
| 69 | 69 | |
| 70 | 70 | Environment values are read when the container **starts**: after editing the file, recreate (`podman compose up -d`, or `systemctl --user restart hezarfen_frontend_compose`). `podman restart` keeps the old environment. |
| 71 | 71 |
compose.yamlmodified+11 -12
| @@ -30,22 +30,21 @@ services: | ||
| 30 | 30 | # latest local build on a laptop. |
| 31 | 31 | image: localhost/hezarfen_frontend:${HEZARFEN_TAG:-latest} |
| 32 | 32 | container_name: hezarfen_frontend |
| 33 | - ports: | |
| 34 | - # Loopback only: terminate TLS on a reverse proxy in front of this. Host | |
| 35 | - # and container port both follow PORT, so the publish can never drift | |
| 36 | - # from what the server listens on. | |
| 37 | - - "127.0.0.1:${PORT:-5173}:${PORT:-5173}" | |
| 33 | + # Host networking, on purpose: the process then shares the host's loopback, | |
| 34 | + # so BACKEND_ORIGIN=http://127.0.0.1:7656 (the backend's published loopback | |
| 35 | + # port) is reachable from inside the container exactly as it would be from | |
| 36 | + # a host process β a bridge network would resolve 127.0.0.1 to the | |
| 37 | + # container itself and refuse the connection. | |
| 38 | + # | |
| 39 | + # There is no `ports:` mapping under host networking: the app binds the | |
| 40 | + # host's interface directly. Keep it off the LAN with HOST=127.0.0.1 in | |
| 41 | + # hezarfen_frontend.env (server.ts binds what HOST says, 0.0.0.0 by | |
| 42 | + # default), leaving the reverse proxy on the same loopback as the only door. | |
| 43 | + network_mode: host | |
| 38 | 44 | env_file: |
| 39 | 45 | # Written once by the operator on the server (0600); absent on a laptop. |
| 40 | 46 | - path: hezarfen_frontend.env |
| 41 | 47 | required: false |
| 42 | - environment: | |
| 43 | - # Listen port, read from hezarfen_frontend.env (passed as --env-file by | |
| 44 | - # the systemd unit and the deploy). server.ts reads PORT, so this one | |
| 45 | - # value moves the container, the publish above and the healthcheck | |
| 46 | - # together β and keeping it here is what stops the publish and the | |
| 47 | - # listener from drifting when no --env-file is given. | |
| 48 | - PORT: "${PORT:-5173}" | |
| 49 | 48 | restart: unless-stopped |
| 50 | 49 | healthcheck: |
| 51 | 50 | # Bun-based: the slim runtime image ships no curl. Reads the container's |
deploy/hezarfen_frontend.env.examplemodified+25 -26
| @@ -8,41 +8,40 @@ | ||
| 8 | 8 | # |
| 9 | 9 | # Nothing automated creates, overwrites or uploads this file β the operator |
| 10 | 10 | # owns it; compose only reads it (`podman compose --env-file`, plus `env_file` |
| 11 | -# into the frontend container). Plain KEY=value lines: no quotes, no `export`. | |
| 11 | +# into the frontend container). Plain KEY=value lines: no quotes, no `export`, | |
| 12 | +# no trailing comment. | |
| 12 | 13 | # |
| 13 | -# One file, two paths into the stack: | |
| 14 | -# * `env_file:` passes this file into the container as-is β keys the compose | |
| 15 | -# file does not list under `environment:` (BACKEND_ORIGIN among them) apply | |
| 16 | -# with or without `--env-file`, and are read when the container STARTS. | |
| 17 | -# Change a value, then recreate: `podman compose up -d` (or | |
| 18 | -# `systemctl --user restart hezarfen_frontend_compose`). `podman restart` | |
| 19 | -# keeps the old environment. | |
| 20 | -# * `--env-file` additionally feeds compose's own ${VAR:-default} | |
| 21 | -# interpolation. Only PORT needs that (the publish and the container | |
| 22 | -# listener are derived from one value); compose.yaml passes no other knob | |
| 23 | -# through it, so nothing here is shadowed by a compose default. | |
| 14 | +# The service declares this file as `env_file:`, so every key below reaches the | |
| 15 | +# container with no extra flag and nothing is shadowed by a compose default. | |
| 16 | +# Values are read when the container STARTS: after editing, recreate | |
| 17 | +# (`podman compose up -d`, or `systemctl --user restart | |
| 18 | +# hezarfen_frontend_compose`). `podman restart` keeps the old environment. | |
| 24 | 19 | # |
| 25 | -# The app reads exactly two keys today (server.ts): BACKEND_ORIGIN and PORT. | |
| 26 | -# Any other key here is carried into the container untouched, unread. | |
| 20 | +# The app reads three keys today (server.ts): BACKEND_ORIGIN, PORT, HOST. | |
| 27 | 21 | |
| 28 | 22 | # ββ Backend origin βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ |
| 29 | 23 | # Where the container proxies /api/* β plain HTTP and the exam-room WebSocket |
| 30 | 24 | # alike. The browser only ever talks to this frontend, so session cookies stay |
| 31 | -# same-origin. Must be reachable FROM INSIDE the container. | |
| 25 | +# same-origin. | |
| 32 | 26 | # |
| 33 | -# The default below is the public origin and always works. A | |
| 34 | -# container-to-container origin such as http://hezarfen_backend:7656 is NOT | |
| 35 | -# wired today: neither compose stack declares a shared podman network, and the | |
| 36 | -# backend publishes on host loopback only (127.0.0.1:7656). | |
| 37 | -BACKEND_ORIGIN=https://hezarfen.dizey.sh | |
| 27 | +# The frontend runs with host networking (`network_mode: host` in compose.yaml), | |
| 28 | +# so it shares the host's loopback and this is simply where the backend | |
| 29 | +# listens on this machine: the backend's compose publishes 127.0.0.1:7656. | |
| 30 | +# A bridge network would resolve 127.0.0.1 to the container itself and refuse | |
| 31 | +# the connection. | |
| 32 | +BACKEND_ORIGIN=http://127.0.0.1:7656 | |
| 33 | + | |
| 34 | +# ββ Listen address βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| 35 | +# HOST=127.0.0.1 keeps the app on loopback: with host networking there is no | |
| 36 | +# port mapping in front of it, so 0.0.0.0 would put it on every interface and | |
| 37 | +# bypass the reverse proxy. The proxy (Ferron et al.) proxies to | |
| 38 | +# http://127.0.0.1:<PORT> from this same machine. | |
| 39 | +HOST=127.0.0.1 | |
| 38 | 40 | |
| 39 | 41 | # ββ Listen port ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ |
| 40 | -# One value moves three things: what server.ts listens on, the host-loopback | |
| 41 | -# publish (127.0.0.1:<PORT>:<PORT> in compose.yaml) and the deploy's health | |
| 42 | -# gate, which reads this line back out of this file. Unlike BACKEND_ORIGIN this | |
| 43 | -# one is interpolated by compose, so it needs `--env-file` to take effect (the | |
| 44 | -# systemd unit and the deploy both pass it). Leave it at 5173 unless something | |
| 45 | -# else already owns that port on the server. | |
| 42 | +# The port the app binds (and the port the reverse proxy must target). The | |
| 43 | +# deploy's health gate reads this line back out of this file, so the two cannot | |
| 44 | +# drift. | |
| 46 | 45 | PORT=5173 |
| 47 | 46 | |
| 48 | 47 | # ββ Image tag ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ |
deploy/hezarfen_frontend_compose.servicemodified+7 -5
| @@ -12,11 +12,13 @@ | ||
| 12 | 12 | # |
| 13 | 13 | # Keys read from hezarfen_frontend.env (full docs in |
| 14 | 14 | # deploy/hezarfen_frontend.env.example): |
| 15 | -# BACKEND_ORIGIN the origin /api/* is proxied to; reaches | |
| 16 | -# the container through env_file, so it needs | |
| 17 | -# no flag β but only on (re)start | |
| 18 | -# PORT listen port + published port (interpolated, | |
| 19 | -# hence the --env-file below), default 5173 | |
| 15 | +# BACKEND_ORIGIN the origin /api/* is proxied to; on this | |
| 16 | +# host that is http://127.0.0.1:7656, which | |
| 17 | +# the service can reach because compose runs | |
| 18 | +# it with host networking | |
| 19 | +# HOST bind address; 127.0.0.1 keeps the app on | |
| 20 | +# loopback behind the reverse proxy | |
| 21 | +# PORT listen port, default 5173 | |
| 20 | 22 | # |
| 21 | 23 | # Env-only restart (re-runs `compose up -d`, recreating the container when its |
| 22 | 24 | # environment changed): |
server.tsmodified+6 -2
| @@ -7,6 +7,10 @@ | ||
| 7 | 7 | // container's runtime after `vite build`. |
| 8 | 8 | |
| 9 | 9 | const PORT = Number(Bun.env.PORT ?? 5173); |
| 10 | +// Interface to bind. 0.0.0.0 is what a published container port needs; under | |
| 11 | +// host networking the process sits directly on the host, so a server sets | |
| 12 | +// HOST=127.0.0.1 and only the reverse proxy beside it can reach the app. | |
| 13 | +const HOST = Bun.env.HOST ?? "0.0.0.0"; | |
| 10 | 14 | // Where the backend lives on the compose network. Override in compose. |
| 11 | 15 | const BACKEND_ORIGIN = Bun.env.BACKEND_ORIGIN ?? "https://hezarfen.dizey.sh"; |
| 12 | 16 | const BACKEND_HTTP = BACKEND_ORIGIN.replace(/\/+$/, ""); |
| @@ -70,7 +74,7 @@ async function proxyHttp(req: Request, url: URL): Promise<Response> { | ||
| 70 | 74 | |
| 71 | 75 | const server = Bun.serve<Bridge, {}>({ |
| 72 | 76 | port: PORT, |
| 73 | - hostname: "0.0.0.0", | |
| 77 | + hostname: HOST, | |
| 74 | 78 | // Long-running exam sockets must not be reaped by an idle timeout; the |
| 75 | 79 | // backend sends its own periodic state ticks but a quiet client can still |
| 76 | 80 | // sit for minutes between frames. |
| @@ -135,4 +139,4 @@ const server = Bun.serve<Bridge, {}>({ | ||
| 135 | 139 | }, |
| 136 | 140 | }); |
| 137 | 141 | |
| 138 | -console.log(`serving dist/ on :${server.port}, proxying /api -> ${BACKEND_HTTP}`); | |
| 142 | +console.log(`serving dist/ on ${HOST}:${server.port}, proxying /api -> ${BACKEND_HTTP}`); |