Document codebase: comments, per-area READMEs, deployment guide
Readability pass across all four areas of the stack. Comments explain why rather than what, concentrated at the points where behaviour is non-obvious: concurrency, caching, auth, scraping and nginx templating. Backend: doc comments on every exported declaration across handlers, services, models and resolvers. Resolver commentary lives inside function bodies so gqlgen does not strip it on regeneration; schema files use # comments rather than description blocks so introspection output is unchanged. Extracted the duplicated auth-cookie blocks into helpers. Frontend: header comments on every component, store and view, including STATUS: notes marking which of the parallel/legacy views are still live. Documented the non-reactive animation-state pattern, the homeData hub and the WebSocket stale-socket guard. Removed a few dead imports and named the reconnect-backoff and poll-interval constants. Python: module and function docstrings covering the Travelodge scraping contract, the deadline threading, the two-executor concurrency and the NDJSON streaming protocol. Infra: header blocks on every compose file, nginx template, Dockerfile and entrypoint, covering the config selection, envsubst allow-lists, rate-limit semantics and volume persistence. Hoisted the duplicated envsubst variable list in nginx/entrypoint.sh. New: backend/README.md, python/README.md, DEPLOYMENT.md, .env.example, backend/graph/doc.go. Refreshed vue/README.md. Corrected CLAUDE.md, which stated a 24h access token where main.go sets 7 days. No behaviour change. go build/vet, vite build, py_compile and docker compose config all pass. Co-Authored-By: Claude Opus 5 <[email protected]>
This commit is contained in:
@@ -1,3 +1,27 @@
|
||||
# =============================================================================
|
||||
# nginx image — the public edge. Stock nginx plus two tools and three templates.
|
||||
#
|
||||
# BASE: nginx:1.27, pinned to a minor version. Official nginx images are
|
||||
# multi-arch, so the same tag resolves to the arm64 build on the Raspberry Pi
|
||||
# with no platform pin needed — that is why there is no `--platform` anywhere
|
||||
# in this repo.
|
||||
#
|
||||
# WHY THE EXTRA PACKAGES
|
||||
# gettext-base provides `envsubst`, which entrypoint.sh uses to render a
|
||||
# config template. Not in the base image.
|
||||
# openssl used by entrypoint.sh to mint the self-signed localhost
|
||||
# certificate in DEV_MODE only. Installed unconditionally
|
||||
# because there is one image for both dev and production.
|
||||
#
|
||||
# `rm -rf /etc/nginx/html/*` clears the stock "Welcome to nginx" page, since
|
||||
# that directory is where the vue_dist volume gets mounted and a leftover
|
||||
# index.html there would be served if the Vue build ever failed.
|
||||
#
|
||||
# apt lists are deleted in the SAME RUN layer so they never end up in the image.
|
||||
#
|
||||
# NO nginx.conf IS COPIED IN. All three templates are, and entrypoint.sh picks
|
||||
# one at container start. See nginx/entrypoint.sh for that decision.
|
||||
# =============================================================================
|
||||
FROM nginx:1.27
|
||||
RUN rm -rf /etc/nginx/html/* && \
|
||||
apt-get update && apt-get install -y gettext-base openssl && \
|
||||
|
||||
+105
-7
@@ -1,14 +1,99 @@
|
||||
#!/bin/sh
|
||||
# =============================================================================
|
||||
# nginx entrypoint — picks one of three nginx configs at container start.
|
||||
#
|
||||
# WHAT IT DOES
|
||||
# nginx.conf is not shipped in the image. Instead three *templates* are baked
|
||||
# in (see nginx/Dockerfile) and this script renders exactly one of them to
|
||||
# /etc/nginx/nginx.conf before starting nginx in the foreground.
|
||||
#
|
||||
# WHEN IT RUNS
|
||||
# Every time the `nginx` container starts (ENTRYPOINT). It is therefore also
|
||||
# the thing that re-evaluates "do we have certificates yet?" on each restart.
|
||||
#
|
||||
# WHICH CONFIG IS CHOSEN (the three-way decision)
|
||||
# 1. DEV_MODE=true -> nginx_dev.conf.template
|
||||
# Local development. Certbot is disabled (docker-compose.dev.yml puts it
|
||||
# in the `disabled` profile), so there can never be a real certificate.
|
||||
# We mint a throwaway self-signed cert for CN=localhost so the :443
|
||||
# server block still has something to load, and proxy `/` to the Vite
|
||||
# dev server (vue:5173) instead of serving built files from disk.
|
||||
# 2. Real cert exists for $DOMAIN -> nginx.conf.template
|
||||
# Steady-state production: HTTP->HTTPS redirect, apex->www redirect,
|
||||
# SPA served from /etc/nginx/html, everything else reverse-proxied.
|
||||
# 3. No cert yet -> nginx_setup.conf.template
|
||||
# The bootstrap case. See CHICKEN-AND-EGG below.
|
||||
#
|
||||
# CHICKEN-AND-EGG: nginx_setup.conf vs certbot first issuance
|
||||
# Let's Encrypt HTTP-01 validation requires a working HTTP server on port 80
|
||||
# that serves /.well-known/acme-challenge/. But nginx.conf.template references
|
||||
# /etc/letsencrypt/live/$DOMAIN/fullchain.pem in its `ssl_certificate`
|
||||
# directives, and nginx refuses to start if that file is missing. So on a
|
||||
# brand-new host nginx cannot use the production config, and certbot cannot
|
||||
# get a certificate without nginx. The setup config breaks the loop: it is a
|
||||
# minimal port-80-only server that serves the ACME webroot and 404s
|
||||
# everything else — no TLS, no upstreams, nothing that can fail to load.
|
||||
# Order of events on a fresh machine:
|
||||
# a. nginx starts with nginx_setup.conf (port 80, ACME webroot only).
|
||||
# b. certbot container runs `certbot certonly --webroot`, writes the cert
|
||||
# into the shared ./certbot/conf volume.
|
||||
# c. nginx is restarted (manually, or by a redeploy) — this script re-runs,
|
||||
# now finds the cert, and renders the production config.
|
||||
# Step (c) is NOT automatic. A first deploy on a new domain needs one manual
|
||||
# `docker compose restart nginx` after certbot succeeds.
|
||||
#
|
||||
# THE envsubst TEMPLATING PATTERN
|
||||
# The templates are plain nginx configs containing $VARIABLES. Two different
|
||||
# kinds of $VAR live in the same file:
|
||||
# - build-time vars we want substituted: $DOMAIN, $BACKEND_HOST, ...
|
||||
# - runtime nginx vars that must survive: $host, $remote_addr, $uri,
|
||||
# $http_upgrade, $proxy_add_x_forwarded_for, $binary_remote_addr, ...
|
||||
# Bare `envsubst` would replace *every* $NAME it recognises and blank out the
|
||||
# nginx runtime variables, producing a config that silently forwards empty
|
||||
# headers. Passing an explicit SHELL-FORMAT list (the '${A} ${B} ...' string
|
||||
# below) restricts envsubst to exactly those names and leaves every other
|
||||
# $token untouched. That is why the list is in SINGLE quotes: it must reach
|
||||
# envsubst as a literal '${DOMAIN} ${BACKEND_HOST} ...' string, not be
|
||||
# expanded by the shell first.
|
||||
#
|
||||
# ENV VARS REQUIRED (all from ./.env via `env_file` in docker-compose.yml)
|
||||
# DOMAIN apex domain, e.g. example.com
|
||||
# BACKEND_HOST / BACKEND_PORT Go API container name and port
|
||||
# BACKEND_ENDPOINT URL prefix the API is mounted at (e.g. /api)
|
||||
# ICECAST_HOST / ICECAST_PORT radio stream upstream
|
||||
# GITEA_HOST / GITEA_PORT git server upstream
|
||||
# HASURA_HOST / HASURA_PORT Hasura console upstream
|
||||
# QUARTZ_HOST / QUARTZ_PORT notes site upstream
|
||||
# PYTHON_HOST / PYTHON_PORT FastAPI upstream (defaulted below)
|
||||
# UPTIMEKUMA_* / WALLABAG_* substituted but not currently referenced by
|
||||
# any template; kept so adding those services
|
||||
# back needs no entrypoint change.
|
||||
# DEV_MODE "true" only via docker-compose.dev.yml
|
||||
#
|
||||
# ASSUMES EXISTS
|
||||
# /etc/letsencrypt (bind mount of ./certbot/conf, shared with certbot)
|
||||
# /var/www/certbot (bind mount of ./certbot/www, the ACME webroot)
|
||||
# /uploads (named volume `uploads`, shared with the backend)
|
||||
# /etc/nginx/html (named volume `vue_dist`, written by the one-shot `vue`
|
||||
# build container in production)
|
||||
# =============================================================================
|
||||
set -e
|
||||
|
||||
# Defaults for optional services so envsubst never leaves an empty upstream
|
||||
export PYTHON_HOST="${PYTHON_HOST:-python}"
|
||||
export PYTHON_PORT="${PYTHON_PORT:-8000}"
|
||||
|
||||
# The envsubst allow-list, defined once and reused by both full-config branches
|
||||
# so the two lists cannot drift apart. It MUST stay single-quoted here and be
|
||||
# passed as "$ENVSUBST_VARS" (one argument) below.
|
||||
ENVSUBST_VARS='${DOMAIN} ${BACKEND_HOST} ${BACKEND_PORT} ${BACKEND_ENDPOINT} ${ICECAST_HOST} ${ICECAST_PORT} ${GITEA_HOST} ${GITEA_PORT} ${HASURA_HOST} ${HASURA_PORT} ${QUARTZ_HOST} ${QUARTZ_PORT} ${UPTIMEKUMA_HOST} ${UPTIMEKUMA_PORT} ${WALLABAG_HOST} ${WALLABAG_PORT} ${PYTHON_HOST} ${PYTHON_PORT}'
|
||||
|
||||
# Check if DEV_MODE
|
||||
if [ "$DEV_MODE" = "true" ]; then
|
||||
echo "Dev mode. Generating self-signed certificate for HTTPS."
|
||||
CERT_DIR="/etc/letsencrypt/live/localhost"
|
||||
# Generated once and kept in the certbot/conf bind mount, so restarts reuse
|
||||
# the same cert and the browser's "accept this exception" sticks.
|
||||
if [ ! -f "$CERT_DIR/fullchain.pem" ]; then
|
||||
mkdir -p "$CERT_DIR"
|
||||
openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
|
||||
@@ -17,38 +102,51 @@ if [ "$DEV_MODE" = "true" ]; then
|
||||
-subj "/CN=localhost" 2>/dev/null
|
||||
fi
|
||||
# In dev mode, so use nginx_dev.conf.template
|
||||
envsubst '${DOMAIN} ${BACKEND_HOST} ${BACKEND_PORT} ${BACKEND_ENDPOINT} ${ICECAST_HOST} ${ICECAST_PORT} ${GITEA_HOST} ${GITEA_PORT} ${HASURA_HOST} ${HASURA_PORT} ${QUARTZ_HOST} ${QUARTZ_PORT} ${UPTIMEKUMA_HOST} ${UPTIMEKUMA_PORT} ${WALLABAG_HOST} ${WALLABAG_PORT} ${PYTHON_HOST} ${PYTHON_PORT}' \
|
||||
envsubst "$ENVSUBST_VARS" \
|
||||
</etc/nginx/nginx_dev.conf.template \
|
||||
>/etc/nginx/nginx.conf
|
||||
elif [ -f "/etc/letsencrypt/live/$DOMAIN/fullchain.pem" ] && [ -f "/etc/letsencrypt/live/$DOMAIN/privkey.pem" ]; then
|
||||
echo "Certificates found. Using production nginx config."
|
||||
# In production with certificates already existing, so use nginx.conf.template
|
||||
envsubst '${DOMAIN} ${BACKEND_HOST} ${BACKEND_PORT} ${BACKEND_ENDPOINT} ${ICECAST_HOST} ${ICECAST_PORT} ${GITEA_HOST} ${GITEA_PORT} ${HASURA_HOST} ${HASURA_PORT} ${QUARTZ_HOST} ${QUARTZ_PORT} ${UPTIMEKUMA_HOST} ${UPTIMEKUMA_PORT} ${WALLABAG_HOST} ${WALLABAG_PORT} ${PYTHON_HOST} ${PYTHON_PORT}' \
|
||||
envsubst "$ENVSUBST_VARS" \
|
||||
</etc/nginx/nginx.conf.template \
|
||||
>/etc/nginx/nginx.conf
|
||||
else
|
||||
echo "Certificates NOT found. Using setup nginx config."
|
||||
# In production with no certificates, so use nginx_setup.conf.template and will need restart after generation
|
||||
# Only ${DOMAIN} is substituted: the setup config has no upstreams at all.
|
||||
envsubst '${DOMAIN}' </etc/nginx/nginx_setup.conf.template >/etc/nginx/nginx.conf
|
||||
fi
|
||||
|
||||
# Ensure upload directory is traversable by nginx worker
|
||||
# Ensure upload directory is traversable by nginx worker.
|
||||
# `uploads` is a named volume shared with the backend, which creates it with
|
||||
# the backend's umask; nginx runs its workers as a different user and needs
|
||||
# +x on the directory to serve files out of it via the /uploads/ alias.
|
||||
# `|| true` because on a read-only or foreign-owned mount this is not fatal.
|
||||
chmod 755 /uploads 2>/dev/null || true
|
||||
|
||||
# Wait for Vue assets in production mode
|
||||
# Wait for Vue assets in production mode.
|
||||
# The `vue` service is a ONE-SHOT build container: it runs `vite build` into the
|
||||
# shared `vue_dist` volume and exits. `depends_on: vue` only waits for it to
|
||||
# start, not to finish, so on a cold build nginx can come up before index.html
|
||||
# exists and would 404 the whole site. Poll for up to 120s, then start anyway
|
||||
# (a running nginx that 404s is still better than a crash loop, and the assets
|
||||
# appear moments later). In dev mode there are no built assets at all — `/` is
|
||||
# proxied to the Vite dev server — so the wait is skipped.
|
||||
if [ "$DEV_MODE" != "true" ]; then
|
||||
echo "Waiting for Vue assets..."
|
||||
elapsed=0
|
||||
while [ ! -f /etc/nginx/html/index.html ] && [ $elapsed -lt 120 ]; do
|
||||
vue_build_timeout=120
|
||||
while [ ! -f /etc/nginx/html/index.html ] && [ "$elapsed" -lt "$vue_build_timeout" ]; do
|
||||
sleep 1
|
||||
elapsed=$((elapsed + 1))
|
||||
done
|
||||
if [ ! -f /etc/nginx/html/index.html ]; then
|
||||
echo "WARNING: Vue assets not found after 120s, starting nginx anyway"
|
||||
echo "WARNING: Vue assets not found after ${vue_build_timeout}s, starting nginx anyway"
|
||||
else
|
||||
echo "Vue assets ready."
|
||||
fi
|
||||
fi
|
||||
|
||||
# Start nginx
|
||||
# Start nginx in the foreground so it is PID 1 and docker can signal/restart it.
|
||||
nginx -g 'daemon off;'
|
||||
|
||||
@@ -1,3 +1,31 @@
|
||||
# =============================================================================
|
||||
# nginx.conf.template — PRODUCTION config (steady state, certificates present).
|
||||
#
|
||||
# WHEN IT IS USED
|
||||
# Rendered to /etc/nginx/nginx.conf by nginx/entrypoint.sh when DEV_MODE is
|
||||
# not "true" AND a real certificate exists at
|
||||
# /etc/letsencrypt/live/<DOMAIN>/. The other two templates cover dev
|
||||
# (nginx_dev.conf.template) and first-boot bootstrap
|
||||
# (nginx_setup.conf.template).
|
||||
#
|
||||
# TEMPLATING
|
||||
# Only the names in the envsubst allow-list in nginx/entrypoint.sh are
|
||||
# substituted: DOMAIN, BACKEND_HOST/PORT/ENDPOINT, ICECAST_*, GITEA_*,
|
||||
# HASURA_*, QUARTZ_*, PYTHON_*, UPTIMEKUMA_*, WALLABAG_*. Every other dollar
|
||||
# token in this file is an nginx RUNTIME variable (host, remote_addr, uri,
|
||||
# http_upgrade, proxy_add_x_forwarded_for, binary_remote_addr, request_uri,
|
||||
# scheme, http_cookie) and must be left alone — that restriction is the whole
|
||||
# reason the allow-list exists.
|
||||
#
|
||||
# SERVER BLOCKS (three, in order)
|
||||
# 1. :80 any name -> serves the ACME challenge, 301s everything else to
|
||||
# https://www.DOMAIN. Certbot renewal depends on this
|
||||
# block staying reachable on plain HTTP.
|
||||
# 2. :443 apex -> TLS terminates, then 301s to the www host, so there
|
||||
# is exactly one canonical origin (important: cookies,
|
||||
# CORS and the SPA all assume www).
|
||||
# 3. :443 www -> the real site.
|
||||
# =============================================================================
|
||||
events {
|
||||
worker_connections 1024;
|
||||
}
|
||||
@@ -9,10 +37,58 @@ http {
|
||||
server_tokens off;
|
||||
charset utf-8;
|
||||
|
||||
# Docker's embedded DNS. Required because upstreams are referenced through
|
||||
# `set $upstream_* ...` variables below; using a variable in proxy_pass
|
||||
# makes nginx resolve the name PER REQUEST instead of once at startup.
|
||||
# That is the trick that lets nginx boot even when a backend container is
|
||||
# down or not yet created (it 502s until the container appears, rather than
|
||||
# refusing to start with "host not found in upstream"). valid=10s caps how
|
||||
# long a stale container IP can be cached after a redeploy.
|
||||
resolver 127.0.0.11 valid=10s;
|
||||
|
||||
# Caps request bodies; must be >= the largest allowed chat image upload or
|
||||
# nginx rejects with 413 before the backend ever sees the request.
|
||||
client_max_body_size 50M;
|
||||
|
||||
# ---- Rate limiting -------------------------------------------------------
|
||||
# Each zone is a shared-memory table keyed by $binary_remote_addr (the
|
||||
# client IP in packed 4/16-byte form rather than a string — that is why
|
||||
# "binary": it makes each entry ~64 bytes instead of ~128). 10m therefore
|
||||
# holds roughly 160k distinct IPs, far more than this site will ever see;
|
||||
# 10m is simply the safe round number, not a tuned value. If a zone fills,
|
||||
# nginx evicts the oldest entries and logs a warning.
|
||||
#
|
||||
# `rate` is enforced as a smoothed leaky bucket, not a per-minute counter:
|
||||
# 5r/m means one request allowed every 12 seconds, not 5 at once then
|
||||
# nothing. `burst=N` at the use site allows N requests to queue ahead of
|
||||
# that drip, and `nodelay` (used everywhere here) serves those N
|
||||
# immediately rather than spacing them out — so the practical behaviour is
|
||||
# "N instant requests, then throttled to the drip rate". Anything over the
|
||||
# burst gets 503 immediately.
|
||||
#
|
||||
# Chosen limits and why:
|
||||
# login 5r/m burst=3 - brute-force defence on password auth. A human
|
||||
# mistyping gets 3 instant tries, then one every
|
||||
# 12s. Painless for a real user, useless for a
|
||||
# credential-stuffing script.
|
||||
# api 30r/s burst=20 - generous; a normal SPA page load fans out to
|
||||
# several API calls and must never be throttled.
|
||||
# This is an abuse ceiling, not a quota.
|
||||
# graphql 10r/s burst=10 - tighter than REST because a single GraphQL
|
||||
# query can be arbitrarily expensive to resolve.
|
||||
# (Production only; the dev template omits it.)
|
||||
# upload 5r/m burst=3 - uploads are 50MB-capable and hit disk; this
|
||||
# bounds how fast one IP can fill the volume.
|
||||
# hotel_search 3r/m burst=2 - each request holds an upstream call to
|
||||
# Travelodge/TfL for up to a few minutes and the
|
||||
# Python side has a single search slot, so this is
|
||||
# really concurrency protection, not abuse
|
||||
# protection.
|
||||
#
|
||||
# NOTE: the key is the direct client IP. Since nginx is the edge here (no
|
||||
# CDN or upstream proxy in front), that is correct. If anything is ever put
|
||||
# in front, these zones must switch to a trusted X-Forwarded-For or every
|
||||
# visitor will share one bucket.
|
||||
limit_req_zone $binary_remote_addr zone=login:10m rate=5r/m;
|
||||
limit_req_zone $binary_remote_addr zone=api:10m rate=30r/s;
|
||||
limit_req_zone $binary_remote_addr zone=graphql:10m rate=10r/s;
|
||||
@@ -24,6 +100,9 @@ http {
|
||||
|
||||
access_log /var/log/nginx/access.log compact;
|
||||
|
||||
# nginx's bundled mime.types has no entry for .mjs, so ES modules would be
|
||||
# served as application/octet-stream and browsers refuse to execute them.
|
||||
# This adds the missing mapping without editing the shipped mime.types.
|
||||
types {
|
||||
text/javascript mjs;
|
||||
}
|
||||
@@ -110,6 +189,12 @@ http {
|
||||
add_header Cache-Control "public";
|
||||
}
|
||||
|
||||
# User-uploaded files, served straight off the shared `uploads` volume
|
||||
# so the Go process never has to stream them. Because these are
|
||||
# attacker-supplied bytes on our own origin, three headers neutralise
|
||||
# them: nosniff stops content-type guessing, Content-Disposition inline
|
||||
# keeps them from being treated as downloads, and the locked-down CSP
|
||||
# means even an uploaded .html can run no script and load no resource.
|
||||
location /uploads/ {
|
||||
alias /uploads/;
|
||||
add_header X-Content-Type-Options nosniff always;
|
||||
@@ -117,6 +202,9 @@ http {
|
||||
add_header Content-Security-Policy "default-src 'none'; img-src 'self'; style-src 'none'; script-src 'none'" always;
|
||||
}
|
||||
|
||||
# SPA fallback: serve the file if it exists, otherwise hand index.html
|
||||
# to the browser so Vue Router can resolve the path client-side. This
|
||||
# is what makes deep links like /notes-page or /admin work on refresh.
|
||||
location / {
|
||||
try_files $uri $uri/ /index.html;
|
||||
}
|
||||
@@ -127,10 +215,19 @@ http {
|
||||
access_log off;
|
||||
}
|
||||
|
||||
# Belt-and-braces: HTTP-01 validation always happens on port 80 (the
|
||||
# :80 block above), so this duplicate on :443 never actually serves a
|
||||
# challenge. Harmless, and kept in case a future challenge type or a
|
||||
# redirect chain lands here. Note it is a regex location (~), so it
|
||||
# matches the path anywhere in the URI, not just at the start.
|
||||
location ~ /.well-known/acme-challenge/ {
|
||||
root /var/www/certbot;
|
||||
}
|
||||
|
||||
# One-off: this GIF is a "web stamp" other people's sites hot-link, so
|
||||
# it needs an explicit CORS header. Ironically the origin allowed is our
|
||||
# own, which means cross-origin embedders are not actually permitted —
|
||||
# see the proposals in DEPLOYMENT.md before changing it.
|
||||
location = /img/stamps/mine.gif {
|
||||
add_header Access-Control-Allow-Origin "https://www.$DOMAIN";
|
||||
expires 7d;
|
||||
@@ -142,10 +239,49 @@ http {
|
||||
add_header Cache-Control "public";
|
||||
}
|
||||
|
||||
# ---- Backend API -----------------------------------------------------
|
||||
# Two patterns repeat through every proxied location below:
|
||||
#
|
||||
# 1. THE PREFIX-STRIP REWRITE
|
||||
# rewrite ^<PREFIX>/(.*)$ /$1 break;
|
||||
# The public URL carries a prefix (/api, /radio, /gitea, /notes,
|
||||
# /py) but the upstream service serves its routes from /. The
|
||||
# rewrite strips the prefix and `break` stops rewrite processing so
|
||||
# the request stays in this location and goes to proxy_pass with the
|
||||
# rewritten URI. (This is why proxy_pass here has no trailing path —
|
||||
# with a variable upstream, nginx would not do the strip for us.)
|
||||
# The bare `location <PREFIX>` blocks that 301 to `<PREFIX>/` exist
|
||||
# only so a user typing /gitea without the slash lands correctly.
|
||||
#
|
||||
# 2. THE FOUR FORWARDING HEADERS
|
||||
# Host preserve the browser's hostname so the app
|
||||
# builds correct absolute URLs and cookies.
|
||||
# X-Real-IP the true client IP (the upstream otherwise
|
||||
# sees only nginx's container IP).
|
||||
# X-Forwarded-For appends the client to any existing chain.
|
||||
# X-Forwarded-Proto tells the app the ORIGINAL request was
|
||||
# https even though this hop is plain http.
|
||||
# Without it the backend would issue
|
||||
# non-Secure cookies and http:// redirects,
|
||||
# breaking auth behind TLS termination.
|
||||
#
|
||||
# Ordering matters: nginx picks the longest matching prefix location,
|
||||
# so the specific /ws, /auth/login, /messages/upload and /graphql blocks
|
||||
# win over the catch-all <PREFIX>/ block regardless of file order.
|
||||
location $BACKEND_ENDPOINT {
|
||||
return 301 $BACKEND_ENDPOINT/;
|
||||
}
|
||||
|
||||
# WebSocket endpoint (live chat / activity feed).
|
||||
# The three lines that make an upgrade work, and why each is needed:
|
||||
# proxy_http_version 1.1 - HTTP/1.0 has no Upgrade mechanism at all.
|
||||
# Upgrade $http_upgrade - hop-by-hop headers are NOT forwarded by
|
||||
# Connection "upgrade" default, so they must be re-set by hand.
|
||||
# Note the Connection value is hardcoded rather than using the usual
|
||||
# $connection_upgrade map: this location only ever serves websockets, so
|
||||
# there is no plain-HTTP case to fall back to.
|
||||
# Deliberately NOT rate limited — a websocket is one long-lived request
|
||||
# and a limit_req here would drop reconnect storms, not abuse.
|
||||
location $BACKEND_ENDPOINT/ws {
|
||||
rewrite ^$BACKEND_ENDPOINT/(.*)$ /$1 break;
|
||||
proxy_pass $upstream_backend;
|
||||
@@ -295,6 +431,20 @@ http {
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
}
|
||||
|
||||
# ---- Admin gate for /hasura/ and /notes/ ------------------------------
|
||||
# `auth_request` makes nginx fire a subrequest here before serving the
|
||||
# real one; a 2xx lets it through, 401/403 triggers error_page ->
|
||||
# @auth_denied -> redirect to the SPA login. This puts the Go backend's
|
||||
# session cookie in front of two services that have no idea it exists.
|
||||
# internal; - unreachable from outside, subrequest only.
|
||||
# proxy_pass_request_body off + Content-Length ""
|
||||
# - the subrequest must not replay the
|
||||
# original body (it could be a 50MB
|
||||
# upload); clearing Content-Length stops
|
||||
# the backend waiting for a body that is
|
||||
# not coming.
|
||||
# Cookie $http_cookie - the whole point: forward the session
|
||||
# cookie so the backend can validate it.
|
||||
location = /internal/auth/admin-validate {
|
||||
internal;
|
||||
rewrite ^ /auth/validate-admin break;
|
||||
|
||||
@@ -1,3 +1,36 @@
|
||||
# =============================================================================
|
||||
# nginx_dev.conf.template — LOCAL DEVELOPMENT config.
|
||||
#
|
||||
# WHEN IT IS USED
|
||||
# Rendered by nginx/entrypoint.sh whenever DEV_MODE=true, which is set only
|
||||
# by docker-compose.dev.yml. Never used on the Pi.
|
||||
#
|
||||
# HOW IT DIFFERS FROM nginx.conf.template
|
||||
# - `/` is proxied to the Vite dev server at vue:5173 instead of serving
|
||||
# built files from disk. The Upgrade/Connection headers on that block are
|
||||
# for Vite's HMR websocket, not for the app.
|
||||
# - No HTTP->HTTPS redirect and no apex->www redirect: BOTH :80 and :443
|
||||
# serve the full site, so http://localhost just works. The two server
|
||||
# blocks are therefore near-identical copies of each other (see the
|
||||
# duplication note in DEPLOYMENT.md).
|
||||
# - TLS uses the self-signed /etc/letsencrypt/live/localhost/ certificate that
|
||||
# the entrypoint generates, not a Let's Encrypt one. Browsers will warn.
|
||||
# - No HSTS or other security headers — HSTS on localhost would poison the
|
||||
# browser for every other localhost project on the machine.
|
||||
# - No `graphql` rate-limit zone (the production template has one); GraphQL
|
||||
# falls under the general `api` limit here.
|
||||
# - No /assets/, /fonts/, /img/ cache-control blocks: Vite serves those and
|
||||
# long cache lifetimes would defeat hot reload.
|
||||
#
|
||||
# TEMPLATING
|
||||
# Same envsubst allow-list as production. Every other dollar token is an
|
||||
# nginx runtime variable and must survive substitution untouched.
|
||||
#
|
||||
# For the rate-limit reasoning, the prefix-strip rewrite, the four forwarding
|
||||
# headers, the websocket upgrade block and the auth_request admin gate, see the
|
||||
# fully-annotated equivalents in nginx.conf.template — the mechanics here are
|
||||
# identical.
|
||||
# =============================================================================
|
||||
events {
|
||||
worker_connections 1024;
|
||||
}
|
||||
|
||||
@@ -1,3 +1,33 @@
|
||||
# =============================================================================
|
||||
# nginx_setup.conf.template — BOOTSTRAP config, used exactly once per host.
|
||||
#
|
||||
# WHEN IT IS USED
|
||||
# Rendered by nginx/entrypoint.sh in production when NO certificate exists
|
||||
# yet at /etc/letsencrypt/live/<DOMAIN>/.
|
||||
#
|
||||
# WHY IT EXISTS (the chicken-and-egg)
|
||||
# The production config cannot start without a certificate file, and certbot
|
||||
# cannot obtain a certificate without an HTTP server answering Let's
|
||||
# Encrypt's HTTP-01 challenge on port 80. This config is the smallest thing
|
||||
# that breaks that loop: port 80 only, no TLS directives, no upstreams,
|
||||
# nothing that can fail to load. It serves the ACME webroot and 404s
|
||||
# everything else so the half-configured site is never partially exposed.
|
||||
#
|
||||
# LIFECYCLE
|
||||
# 1. nginx starts here.
|
||||
# 2. certbot succeeds and writes the certificate into the shared volume.
|
||||
# 3. nginx must be RESTARTED for the entrypoint to re-check and switch to
|
||||
# nginx.conf.template. This step is manual:
|
||||
# docker compose restart nginx
|
||||
# Nothing watches the cert directory and flips automatically.
|
||||
#
|
||||
# TEMPLATING
|
||||
# Only ${DOMAIN} is substituted for this file (a separate, shorter envsubst
|
||||
# call in the entrypoint) because there are no upstreams to fill in.
|
||||
#
|
||||
# ASSUMES: /var/www/certbot is bind-mounted and shared with the certbot
|
||||
# container; port 80 is reachable from the public internet.
|
||||
# =============================================================================
|
||||
events {
|
||||
worker_connections 1024; # required, can be minimal
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user