Retry failed reverse proxy request

1. The problem I’m having:

Q: Hi, Is it possible to make caddy retry reverse proxy calls to the application server if they fail the first time?
I have something like this:

main.localhost {
	reverse_proxy http://main:80

But this returns a blank page when the server goes down for even a couple seconds.

What I would like to happen is when app goes down, caddy attempts to reverse proxy the request about 3 to 5 times with a delay of 1 second between the retries, before finally returning 5xx to the client(the client would just have a loading circle in the meantime), but any other way to achieve a similar thing is ok too.

I need this for hot reloading. My setup is single-node, but if the solution necessitates running multiple replicas it’s okay too.

So far I’ve tried setting lb_try_duration, didn’t seem to work. Please point me in the right direction.

2. Error messages and/or full log output:

No errors. Blank page is returned

3. Caddy version:

v2.7.6 (caddy:latest)

4. How I installed and ran Caddy:

docker compose.

a. System environment:

I omitted the rest of the template as it has almost nothing to do with my current configuration or compose.

Simon Willison had similar requirement and published his solution on his blog

1 Like

Also, since that blog post, we added lb_retries which allows you to retry that amount of times instead of depending on a try duration. Depending on your usecase, that might be better for you. See reverse_proxy (Caddyfile directive) — Caddy Documentation

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.