Product
Four Dollar Signs Broke My Redirect. I Chased the Wrong Bug for Five Days.
Every request to the www host returned 502 while the backend was healthy. The cause was four dollar signs in a generated label, and the log I trusted pointed the wrong way.
coolify www redirect 502traefik 502 bad gateway redirectcoolify redirect non-www
For five days, every request to the www version of my site returned 502 Bad Gateway, and for five days I was debugging the wrong thing. The non-www host worked. The app was fine. The cause turned out to be four dollar signs in a generated config file.
What was happening: one host 502s, its siblings return 200
The setup is a site deployed through Coolify, with its redirect setting on non-www, meaning www should forward to the bare domain. Instead, the www host gave a 502 on every request while the main host answered normally.
A 502 makes you think the backend is down. It was not. Traefik, the proxy in front, answered the www request itself and failed doing so. The backend never saw the request.
I did not know that yet, and the log sent me somewhere else.
The red herring: “Router defined multiple times”
The Traefik log had a line complaining that a router was defined multiple times with different configurations. It looked like the answer. Two definitions of one router, a conflict, a 502. I spent days on it.
It was a red herring. That message fires briefly whenever two containers overlap during a rolling deploy, so it shows up around any redeploy and tells you very little. It cost me five days because it was the only suspicious thing in the log, and a single suspicious line is very persuasive when you have nothing else.
If you see it, do not treat it as a diagnosis on its own.
The real cause: $$$${1} turns into a literal ${1}
The redirect rule uses capture groups, so it needs ${1} and ${2} in its replacement. Coolify’s own source emits the correct escaped form for that, $${1}://$${2}. But a later escaping step writes four dollar signs into the generated compose file: $$$${1}://$$$${2}.
Then the value gets unescaped twice on its way to the proxy:
| Stage | Value |
|---|---|
| Coolify’s generated compose file | $$$${1}://$$$${2} |
After docker compose collapses $$ to $ | $${1}://$${2} (the live container label) |
After Traefik’s Go regexp collapses $$ to $ | ${1}://${2} (literal text) |
The last row is the problem. That is the plain text ${1}://${2}, not the capture groups it was meant to stand for. Traefik cannot parse that as a URL, so the redirect middleware fails, and every request that goes through it gets a 502.
I proved this in an isolated Traefik sandbox that read its config from a file. With $${1} in the file, I got a 502. With ${1}, I got a clean 301.
How to diagnose it in two minutes
If one host on an app returns 502 while its siblings return 200, stop reading the Traefik log and compare the middleware chains on the routers instead. The labels are on the container:
docker inspect <container> --format '{{range $k,$v := .Config.Labels}}{{$k}}={{$v}}
{{end}}' | grep traefik
Look at the chain on the failing router. A to-non-www middleware in it is the tell. If you see that on a host that 502s, you are looking at this bug, and the backend is innocent. I wish I had run this on day one.
A fix that survives Coolify redeploys
My first instinct was to edit the compose file, and that cannot work. Coolify regenerates it on every deploy. My site redeploys every night on a schedule, so any hand edit would be gone by the next morning.
Patching Coolify’s source is no better. I run the latest image, which updates itself and overwrites a patch.
What works is going around Coolify rather than through it: a Traefik dynamic-config override, a file that Traefik’s file provider watches, entirely outside the generated labels. In mine, I define my own redirect router with these properties:
- Priority 1000. Coolify’s generated routers get a priority based on rule length, around 50 in my case, so the override wins.
- HTTPS entrypoint only. I deliberately left the HTTP entrypoint to Coolify. If my override catches plain HTTP too, it swallows requests to
/.well-known/acme-challenge/, and certificate renewal breaks. That would be a nasty second bug to find, and I’d only learn about it when a cert expired.
The www host now redirects with a 301 and nothing in Coolify’s generation path has to change.
Is it fixed in Coolify yet?
I do not know. I found this in September, on the version I was running then. Coolify moves fast, and my guess is that this gets fixed upstream at some point, at which point my override becomes redundant. Check your version and test with a quick curl -I against the www host before you build the workaround. If your www host already gives a clean 301, you do not have this bug.
Also note that the same bad middleware can sit latent on another app of mine, hidden only because its www variant is an unused host. A broken config that nobody requests is still a broken config, so I check apps with the redirect setting even when they look healthy.
Checklist if your www host returns 502
- Confirm the non-www host works and the backend answers directly.
- Run the
docker inspectcommand above and look for a redirect middleware on the failing router. - Read the redirect label for four dollar signs.
- Ignore “Router defined multiple times” unless the problem outlasts a deploy.
- Put the fix in a Traefik dynamic-config file, not the compose file and not Coolify’s code.
- Keep the override off the HTTP entrypoint so ACME challenges still pass.
- Re-test after the next redeploy, because that is what the fix has to survive.