Skip to content

fix: Varnish VCL no longer crashes with multiple projects - #127

Closed
jakwinkler wants to merge 1 commit into
mainfrom
fix/varnish-vcl-multiproject
Closed

jakwinkler wants to merge 1 commit into
mainfrom
fix/varnish-vcl-multiproject

Conversation

@jakwinkler

Copy link
Copy Markdown
Contributor

What's wrong

With two or more registered MageBox projects, the shared magebox-varnish container crash-loops and the storefront returns 502 / 503. Two bugs in VCL generation:

1. Unused backends → fatal Varnish compile error

buildVCLConfig (internal/varnish/vcl.go) emitted one backend per project, but the template (lib/templates/varnish/default.vcl.tmpl) only ever references DefaultBackend:

set req.backend_hint = {{.DefaultBackend}};

The "Set backend based on Host header" loop emitted only comments. Varnish 7.x treats a defined-but-unused backend as a fatal compile error (Unused backend <name>), so the VCL fails to compile and the container crash-loops → port 6081 dead → nginx 502.

Every generated backend was identical (host.docker.internal:8080) anyway, because Nginx already routes to the right project by Host header — the per-project loop was pointless.

2. Wrong health probe

The template hardcoded HEAD / expecting .expected_response = 301, but Magento returns 302 on HEAD / → backend marked sick → 503. The BackendConfig.ProbeURL field (already /health_check.php) was ignored.

Observed in the wild on a host with ~40 MageBox projects (40 backends in the VCL). Recurs on every magebox start / restart, overwriting any manual patch to ~/.magebox/varnish/default.vcl.

The fix

  • Single backend. buildVCLConfig now emits exactly one backend named magento, regardless of project count. Nginx does the per-project routing.
  • Correct probe. Template now probes GET /health_check.php expecting 200 (uses ProbeURL).
  • Removed the now-dead sanitizeName helper + its test (only used for per-project backend names).
  • Cleaned the dead comment-only range block in vcl_recv.

Tests

  • TestVCLGenerator_GenerateMultipleProjects rewritten as a regression test: asserts exactly one backend definition, presence of backend magento, absence of per-project backends, and set req.backend_hint = magento.
  • TestVCLGenerator_Generate now asserts the probe (GET /health_check.php + .expected_response = 200).
  • buildVCLConfig / empty-configs tests updated to expect the single magento backend.

go test ./... green (22 packages), golangci-lint run 0 issues, go vet clean.

Release

Bumps VERSION → 1.18.1 and adds a CHANGELOG.md Fixed entry. After merge, CI auto-tags from VERSION and ships the binaries (magebox self-update), so the fix survives restarts — no more manual VCL patching.

🤖 Generated with Claude Code

The shared Varnish container generated one backend per registered project,
but the template only ever referenced DefaultBackend. Varnish 7.x treats a
defined-but-unused backend as a fatal compile error (Unused backend ...), so
with 2+ projects the container failed to compile its VCL and crash-looped (502).
The health probe also used HEAD / expecting 301 while Magento returns 302,
marking the backend sick (503).

Nginx already routes to the right project by Host header, so all per-project
backends were identical. Emit a single 'magento' backend and probe
GET /health_check.php expecting 200.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@github-actions

Copy link
Copy Markdown
Contributor

📦 PR build artifacts

Built from 7076a14 — view run. Artifacts expire in 14 days.

Note: artifact download links require being signed in to GitHub with access to this repository.

@jakwinkler

Copy link
Copy Markdown
Contributor Author

Superseded by the v2 PR — #127 patched only the unused lib template; the embedded template stayed broken. See the new PR.

@jakwinkler jakwinkler closed this Jul 14, 2026
peterjaap pushed a commit that referenced this pull request Aug 10, 2026
buildVCLConfig emitted one backend per project but the template only
referenced DefaultBackend, so Varnish 7.x aborted on the first unused
backend and crash-looped (502). The probe also used HEAD / expecting 301
while Magento returns 302 (503). Emit a single 'magento' backend (Nginx
routes per project by Host) and probe GET /health_check.php expecting 200.

Fixes both the embedded template (internal/varnish/templates) and the lib
copy. Supersedes #127 (which patched only the unused lib template).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
peterjaap pushed a commit that referenced this pull request Aug 10, 2026
buildVCLConfig emitted one backend per project but the template only
referenced DefaultBackend, so Varnish 7.x aborted on the first unused
backend and crash-looped (502). The probe also used HEAD / expecting 301
while Magento returns 302 (503). Emit a single 'magento' backend (Nginx
routes per project by Host) and probe GET /health_check.php expecting 200.

Fixes both the embedded template (internal/varnish/templates) and the lib
copy. Supersedes #127 (which patched only the unused lib template).

Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant