diff --git a/scripts/setup-origin.sh b/scripts/setup-origin.sh index 4cea467..5ff3755 100755 --- a/scripts/setup-origin.sh +++ b/scripts/setup-origin.sh @@ -282,7 +282,16 @@ fi # ------------------------------------------------------------------ nginx -step "writing the nginx server block" +# A name can bring its own server block: /etc/moshpit/templates/.conf +# (MOSHPIT_TEMPLATE_DIR). Without it, a name whose site is a proxy to an app +# (seo.rank, live.streaming) was rewritten back to the static webroot by the +# next renewal, which re-runs this script. MOSHPIT_TEMPLATE, when set, still +# wins, for a box whose every name is served the same way. +NAME_TEMPLATE="${MOSHPIT_TEMPLATE_DIR:-/etc/moshpit/templates}/$NAME.conf" +if [ -z "${MOSHPIT_TEMPLATE:-}" ] && [ -f "$NAME_TEMPLATE" ]; then + TEMPLATE="$NAME_TEMPLATE" +fi +step "writing the nginx server block from $TEMPLATE" CONF="$SITEDIR/$NAME" if [ "$DRY_RUN" = "0" ]; then mkdir -p "$SITEDIR" "$ENABLEDIR" "$WEBROOT" diff --git a/tests/setup-origin-registry.test.ts b/tests/setup-origin-registry.test.ts index bfd170a..87c39e4 100644 --- a/tests/setup-origin-registry.test.ts +++ b/tests/setup-origin-registry.test.ts @@ -92,3 +92,17 @@ describe("moshpit-renew.sh", () => { assert.match(r.stdout, /1 renewed, 0 failed/); }); }); + +describe("setup-origin.sh — a name's own server block", () => { + test("a template named for the site is used, so a renewal keeps its proxy", async () => { + const dir = await tempDir(); + const templates = join(dir, "templates"); + await mkdir(templates, { recursive: true }); + await writeFile(join(templates, "live.streaming.conf"), "server { server_name NAME; }\n"); + const own = await sh(setup, ["live.streaming", "--dry-run"], { MOSHPIT_TEMPLATE_DIR: templates, MOSHPIT_TEMPLATE: "" }); + assert.equal(own.code, 0, own.stderr); + assert.match(own.stderr + own.stdout, new RegExp(`server block from ${join(templates, "live.streaming.conf").replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}`)); + const other = await sh(setup, ["blue.eggs", "--dry-run"], { MOSHPIT_TEMPLATE_DIR: templates, MOSHPIT_TEMPLATE: "" }); + assert.match(other.stderr + other.stdout, /server block from .*moshpit-origin\.conf/); + }); +});