Files
loki/scripts/hetzner/launch.sh.tmpl
CatoandClaude Opus 5.5 bb9e1a8920 fix(hetzner): launch.sh binds HOSTNAME from BIND_HOST, default stays 127.0.0.1 (#910)
Solon returned 500 for ~30 min: Next 16 standalone + a middleware rewrite
behind Caddy's X-Forwarded-Proto: https fails with 'Failed to proxy
https://localhost:4018/en ... EPROTO' when HOSTNAME=127.0.0.1, because Next
normalises 127.0.0.1 to localhost. An app can now set BIND_HOST in its .env.
The test renders and runs the template for both cases plus a negative control.

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-25 04:49:38 -07:00

34 lines
2.2 KiB
Cheetah

#!/usr/bin/env bash
# Generated onto the box by sync-infra.sh. __PORT__ is substituted per app.
# This template is the ONE definition of the launch script — do not paste a
# second copy into deploy.sh or patch the generated file after the fact.
set -euo pipefail
# `pwd -P`, not `pwd`: /opt/<name>/app is a SYMLINK into releases/, and find(1)
# does not descend a logical symlink path. With a plain `pwd` the server.js
# search below returns nothing and the app crash-loops on every deploy.
#
# This has now cost two outages. vitareba hit it on its first release-deploy
# (2026-07-17) and the fix was applied to deploy.sh — which sed-patched the
# already-generated file — instead of to the generator that keeps emitting the
# broken version. sbb-fundbuero hit the identical failure on 2026-09-08 the
# moment sync-infra.sh ran without a subsequent deploy. Fixing the output of a
# generator does not fix the generator.
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
if [ -f "$HERE/.env" ]; then set -a; source "$HERE/.env"; set +a; fi
# HOSTNAME defaults to loopback; an app sets BIND_HOST in its .env (sourced
# just above, so it wins) to bind elsewhere. Solon needed BIND_HOST=0.0.0.0
# (2026-09-25, ~30 min of 500s): Next 16 standalone plus any middleware rewrite
# (next-intl as-needed: /hire -> /en/hire) behind Caddy's X-Forwarded-Proto:
# https failed every rewritten page with "Failed to proxy
# https://localhost:4018/en ... EPROTO". Next builds the rewrite URL with host
# `localhost` but compares it to an origin built from HOSTNAME, and it
# normalises 127.0.0.1 -> localhost, so the app cannot fix this in its own
# code. 0.0.0.0 is safe ONLY because ufw on the box allows 22/80/443 alone:
# the app ports are not reachable publicly (verified for :4004 and :4018).
# Open an app port in ufw and this override becomes a public bind.
export NODE_ENV=production PORT=__PORT__ HOSTNAME="${BIND_HOST:-127.0.0.1}"
SERVER_JS=$(find "$HERE" -maxdepth 4 -name server.js -not -path "*node_modules*" | head -1)
if [ -n "$SERVER_JS" ]; then exec /usr/bin/node "$SERVER_JS"; fi
# Non-Next services define their own entrypoint via START_CMD in .env
exec bash -c "${START_CMD:?no server.js found and START_CMD unset}"