I have a VPS running services with docker. I use Caddy as a reverse proxy (not running in Docker).
My domain is configured on Cloudflare to point to my server IP. When a request hits my website, it is reverse proxied to my docker service on port 5000. Now, I ran docker compose down to switch off the service and to trigger a 5xx error to test if my maintenance page is working.
However, I get the Cloudflare 502 Bad Gateway page in my browser rather than the custom page from the handle_error Caddy directive.
2. Error messages and/or full log output:
3. Caddy version:
v2.8.4
4. How I installed and ran Caddy:
Installed Caddy via Ansible / apt-get install.
Running it with sudo systemctl start caddy
It sounds like Cloudflare is reverse proxying your website, not just serving DNS that points to your VPS. Cloudflare does their own rewriting/custom response for 5XX errors, see the Cloudflare Docs
If you run a DNS lookup for your domain, does it return with your VPS Public IP address or a Cloudflare IP address?
Additionally, I’m not sure that serving files from the /home/* folder is recommended on Ubuntu:
If you’re running Caddy as a systemd service, reading files from /home will not work, because the caddy user does not have “executable” permission on the /home directory (necessary for traversal). It’s recommended that you place your files in /srv or /var/www/html instead.
I’d recommend moving the file, and additionally, updating your Caddy config to do the rewrite a little differently to restrict access to only the files inside of your site1.com folder:
This is invalid syntax, you cannot use a path in the upstream address. Caddy retains the URL as-is when proxying, so there’s no need to specify it. Change it to this: