Files
stpweb/nginx/nginx.conf.template
T
adamfandClaude Opus 5.5 b832dd68b4 Gate deploys on tests and share the nginx route table
Makefile: make check runs the backend, Python, Vue and compose checks
that previously lived only in local notes.

deploy.yaml: a test job runs the backend and Python suites in containers
on every push, and deploy needs it. Deploy also refuses to run if main
has moved past the tested commit. maintenance.yaml takes the env-apply
lock around its compose steps, as deploy already did.

nginx: the 24 proxied locations that production and dev both carried now
live once in nginx_routes.conf.template, rendered by the entrypoint on
every boot path. The effective config is unchanged for both.

Remove the root package.json and lockfile, which nothing referenced.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-12 14:39:19 +01:00

325 lines
15 KiB
Plaintext

# =============================================================================
# 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_*,
# SILVERBULLET_*, PYTHON_*. 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.
#
# WHERE THE PROXY ROUTES ARE
# Not in this file. The reverse-proxied locations (/api, /radio, /gitea,
# /notes, /sb, /py and the admin-gate subrequest) and the `set $upstream_*`
# variables are shared with dev and live in nginx_routes.conf.template,
# which the www server block includes. ADD NEW PROXIED ROUTES THERE. What
# stays here is what only production has: TLS, the redirects, the security
# headers, static-asset caching, the SPA fallback, the /uploads/ blocks
# (which carry the security headers), the `graphql` rate-limit zone and its
# location, and the limit_req_zone declarations the shared routes refer to.
# =============================================================================
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
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.
# Its location is at the bottom of this file,
# not in the shared route table.)
# 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.
#
# login, api, upload and hotel_search are USED from
# nginx_routes.conf.template, which dev includes too — so
# nginx_dev.conf.template must declare the same four zones, or the dev
# config fails to load with "zero size shared memory zone".
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;
limit_req_zone $binary_remote_addr zone=upload:10m rate=5r/m;
limit_req_zone $binary_remote_addr zone=hotel_search:10m rate=3r/m;
log_format compact
'$remote_addr "$request" $status rt=$request_time';
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;
}
# TLS protocol versions, pinned: nginx's defaults still include TLSv1 and
# TLSv1.1, which are deprecated and fail modern compliance scans.
ssl_protocols TLSv1.2 TLSv1.3;
ssl_session_cache shared:SSL:10m;
ssl_session_tickets off;
gzip on;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_min_length 256;
gzip_types
text/plain
text/css
text/javascript
application/javascript
application/json
application/xml
image/svg+xml
font/woff2
application/font-woff2;
server {
listen 80;
server_name $DOMAIN www.$DOMAIN;
# Allow Certbot to access the ACME challenge
location /.well-known/acme-challenge/ {
root /var/www/certbot;
}
# Redirect everything else to HTTPS
location / {
return 301 https://www.$DOMAIN$request_uri;
}
}
server {
listen 443 ssl;
server_name $DOMAIN;
ssl_certificate /etc/letsencrypt/live/$DOMAIN/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/$DOMAIN/privkey.pem;
add_header Strict-Transport-Security "max-age=63072000; includeSubDomains" always;
return 301 https://www.$DOMAIN$request_uri;
}
server {
listen 443 ssl;
http2 on;
server_name www.$DOMAIN;
# The `set $upstream_*` variables used by proxy_pass are declared in
# nginx_routes.conf (included below), alongside the routes that use
# them.
root /etc/nginx/html;
index index.html;
ssl_certificate /etc/letsencrypt/live/$DOMAIN/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/$DOMAIN/privkey.pem;
# Security headers live in nginx/security_headers.conf and are
# included there instead of here, because any location that declares
# so much as one add_header of its own otherwise inherits none of
# these (nginx's add_header inheritance is all-or-nothing).
# The shared proxy routes in nginx_routes.conf rely on exactly this
# server-level include: none of them declares an add_header, so they
# all inherit these headers here, and inherit nothing in dev.
include /etc/nginx/security_headers.conf;
# Vite hashed assets - immutable, cache 1 year
location /assets/ {
expires 1y;
add_header Cache-Control "public, immutable";
include /etc/nginx/security_headers.conf;
}
# Fonts - cache 30 days
location /fonts/ {
expires 30d;
add_header Cache-Control "public";
include /etc/nginx/security_headers.conf;
}
# Images - cache 7 days
location /img/ {
expires 7d;
add_header Cache-Control "public";
include /etc/nginx/security_headers.conf;
}
# PRIVATE attachments (admin-only). Attachments on *private* chat
# messages are written by the backend to /backend/uploads/private/ and
# must not be readable by the public, so this subpath is put behind the
# same admin `auth_request` used for /sb/. nginx picks
# the LONGEST matching prefix location, so this block wins over the
# public /uploads/ block below for /uploads/private/* regardless of
# ordering. Same three hardening headers as the public block, because
# the bytes are just as attacker-supplied.
location /uploads/private/ {
auth_request /internal/auth/admin-validate;
error_page 401 403 = @auth_denied;
alias /uploads/private/;
include /etc/nginx/security_headers.conf;
add_header X-Content-Type-Options nosniff always;
add_header Content-Disposition "inline" always;
add_header Content-Security-Policy "default-src 'none'; img-src 'self'; style-src 'none'; script-src 'none'" always;
}
# 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/;
include /etc/nginx/security_headers.conf;
add_header X-Content-Type-Options nosniff always;
add_header Content-Disposition "inline" always;
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;
}
location = /robots.txt {
allow all;
log_not_found off;
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. It is a public, deliberately
# hot-linkable image, so the allowed origin is `*` — it used to be our
# own origin, which defeated the point (no cross-origin embedder was
# actually permitted). Nothing here is credentialed, so `*` is safe.
location = /img/stamps/mine.gif {
add_header Access-Control-Allow-Origin "*";
expires 7d;
add_header Cache-Control "public";
include /etc/nginx/security_headers.conf;
}
location /sound/ {
expires 7d;
add_header Cache-Control "public";
include /etc/nginx/security_headers.conf;
}
# ---- Shared proxy route table ----------------------------------------
# Every reverse-proxied location (/api, /radio, /gitea, /notes, /sb,
# /py), the admin auth_request gate and the `set $upstream_*` variables
# come from nginx_routes.conf.template, rendered by the entrypoint to
# this path and shared with the dev config. The prefix-strip rewrite,
# the forwarding headers and the websocket upgrade are annotated there.
#
# Position matters for one thing only: regex locations are tried in
# file order, so the ACME regex above stays ahead of the /notes and
# /py/hotels regexes inside this include, as it always was.
include /etc/nginx/nginx_routes.conf;
# GraphQL, PRODUCTION ONLY — which is why it is here and not in the
# shared table. Longest-prefix matching makes it win over the shared
# $BACKEND_ENDPOINT/ catch-all, swapping the general `api` limit for
# the tighter `graphql` zone declared at the top of this file. Dev has
# no such location, so GraphQL there falls under `api`. Apart from the
# limit_req line the body must stay identical to $BACKEND_ENDPOINT/ in
# nginx_routes.conf.template.
location $BACKEND_ENDPOINT/graphql {
limit_req zone=graphql burst=10 nodelay;
rewrite ^$BACKEND_ENDPOINT/(.*)$ /$1 break;
proxy_pass $upstream_backend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
}