vienalatina/infra/caddy/Caddyfile
Claude 6acc8922b1
Phase D: public profiles, without risking the public site
A member fills in a bio, links and a photo, ticks a box, and has a page at
vienalatina.com/su-nombre. Until they tick it there is no page, and an
unpublished profile 404s exactly like a name that was never a member.

The routing was the dangerous part and it is not solved by a list. The
original plan reserved Hugo's paths in the Caddyfile, which is wrong the
first time the site grows one, and the failure is a member's page
silently shadowing part of vienalatina.com. Instead Caddy forwards a
request only when the path is a single segment, is not /comunidad, and
Hugo has built nothing there — `not file` with try_files. The static
site wins every collision because the filesystem answers the question,
so the rule cannot drift. A member called "de" shadows nothing; their
profile is merely unreachable, which is why such names are refused at
sign-up: to protect the member, not the site.

An unknown name 404s from the app and handle_response turns that into
the site's own 404.html, so the public site keeps its error page and the
app never needs a public-facing one. Both directives verified against
Caddy's documentation source, since caddyserver.com is unreachable here.

Links are the injection surface on a public page: anything whose scheme
is not http or https is dropped, which is asserted against javascript:,
data: and vbscript:, including a leading-space variant. They render with
rel="nofollow noopener".

The photo is public, which the existing upload code deliberately is not,
so it gets its own route that serves a file only while a published
profile points at it. Unpublishing takes the picture down with the page,
and a private board image cannot be fetched through it — both tested.

The page does not extend base.html: that is the members-area chrome, and
rendering a public page inside it would put "Área privada" and a Salir
button on something anybody can read. It repeats the brand colours
instead of sharing them, because Hugo fingerprints its stylesheet — a
deliberate duplicate, named as one, with the source of truth in a
comment.

Migration 3 adds the columns. 231 tests.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NizVpJ2dwzCbjCrTLCjeHn
2026-09-29 13:22:43 +00:00

108 lines
3.7 KiB
Caddyfile

# /etc/caddy/Caddyfile — copy this file there, then: sudo systemctl reload caddy
#
# Caddy fetches and renews Let's Encrypt certificates automatically the first
# time a domain's DNS points at this server. Order matters on a cutover: move
# the A record here first, then reload Caddy — the ACME HTTP-01 challenge for
# vienalatina.com only succeeds once the domain already resolves to this box.
git.vienalatina.com {
reverse_proxy 127.0.0.1:3000
}
ci.vienalatina.com {
reverse_proxy 127.0.0.1:8000
}
# ---------------------------------------------------------------------------
# Live since cutover (A record for vienalatina.com points at this server).
# ---------------------------------------------------------------------------
www.vienalatina.com {
redir https://vienalatina.com{uri} 301
}
vienalatina.com {
root * /var/www/vienalatina.com
encode zstd gzip
file_server
# Members area — the only part of this site that runs code. Both paths are
# matched: Flask redirects /comunidad to /comunidad/, and a matcher of
# /comunidad/* alone would let the bare path fall through to file_server
# and 404 before the app ever sees it.
#
# No `handle` wrapper is needed. Caddy runs reverse_proxy ahead of
# file_server in its directive order, and reverse_proxy is terminal, so a
# matched request never reaches the static tree.
@board path /comunidad /comunidad/*
reverse_proxy @board 127.0.0.1:8080
# Public profiles at vienalatina.com/su-nombre.
#
# This shares a namespace with the entire static site, so the rule is that
# the static site always wins — decided by asking the disk, not by keeping
# a list of reserved paths that would be wrong the first time Hugo grows
# one. Three conditions, each load-bearing:
#
# path_regexp one segment only, so /page/acerca/ never comes here
# not path /comunidad is the members area, matched above
# not file if Hugo built anything at this path, Hugo serves it
#
# A member called "de" therefore shadows nothing: /de/ is a real directory,
# so it is served, and their profile is simply unreachable. The app refuses
# such names at sign-up for that reason, but the site is safe either way.
#
# handle_response turns the app's 404 for an unknown name into the site's
# own 404 page, so a visitor sees the public site's error, not the members
# area's, and the app never needs a public-facing error template.
@profile {
path_regexp ^/([A-Za-z0-9][A-Za-z0-9._-]{0,38})/?$
not path /comunidad /comunidad/*
not file {
try_files {path} {path}/index.html
}
}
reverse_proxy @profile 127.0.0.1:8080 {
@nobody status 404
handle_response @nobody {
rewrite * /404.html
file_server
}
}
# llms.txt is markdown (matches the old WP behaviour)
header /llms.txt Content-Type "text/markdown; charset=utf-8"
# --- 301s for old WordPress URL patterns -----------------------------
# /?p=123 style permalinks → home (add specific mappings as GSC reports them)
@wp_query_permalink {
path /
query p=*
}
redir @wp_query_permalink / 301
# WP category base was /categoria/… (Spanish slugs); Hugo uses /categories/…
@old_category path_regexp oldcat ^/categoria/(.*)$
redir @old_category /categories/{re.oldcat.1} 301
# WP author archives have no Hugo equivalent → home
@old_author path /autor/*
redir @old_author / 301
# Old WP media library URLs → migrated uploads folder
@old_uploads path_regexp oldup ^/wp-content/uploads/(?:\d{4}/\d{2}/)?(.*)$
redir @old_uploads /uploads/{re.oldup.1} 301
# Anything else that starts with /wp- doesn't exist anymore
@wp_leftovers path /wp-admin/* /wp-login.php /wp-json/* /xmlrpc.php
redir @wp_leftovers / 301
# Custom 404 falls back to Hugo's 404 page
handle_errors {
@404 expression {err.status_code} == 404
rewrite @404 /404.html
file_server
}
}