How to Set Up a Reverse Proxy with Caddy on a Linux VPS
Some links on this page are affiliate links. If you sign up through them we may earn a commission at no extra cost to you. We only link to services we have actually used, and commission never decides what we recommend.
You got Caddy installed, pointed a domain at the box, and either the certificate never issued or the browser now shows a 502. Both failures have narrow, boring causes, and neither means Caddy is broken. This walks the order of operations that avoids them, then covers the one that bites almost everyone on the second day.
Quick verdict
For a single VPS running a handful of services, Caddy is the right pick and it isn't close: one binary, a four-line config, and TLS that renews itself without a cron job you'll forget to monitor. The tradeoff that matters is that Caddy hides the certificate machinery behind defaults, so when something does go wrong you're reading journalctl instead of a well-known certbot error message. If you already have a working nginx setup with tuned buffers and a dozen location blocks, keep it — migrating buys you very little.
What you are actually choosing between
| Option | Cost shape and ops burden | Performance ceiling and lock-in |
|---|---|---|
| Caddy on the VPS | Free. One static binary, config is a handful of lines, certs renew automatically with no extra timer. | Far more than a small VPS can saturate. Config is plain text, portable, no vendor. |
| nginx + certbot | Free. You own the renewal timer, the ACME webroot, and a second config surface for TLS. | Higher ceiling if you need specific modules or heavy tuning. Still no lock-in. |
| Traefik | Free, but expects container labels or a provider to be useful. Real overhead for one box. | Good when containers churn constantly; more moving parts to misconfigure. |
| Managed edge / tunnel | Metered, with a free tier that covers small sites. TLS and routing terminate off-box. | Removes the need to expose ports, but your config lives in someone's dashboard and their outage is yours. |
Step by step
1. Fix DNS before you install anything. Create the A record, and if you also create an AAAA record, make sure the VPS actually has working IPv6. A stale AAAA pointing at a host that doesn't answer is the single most common reason certificate issuance fails while curl from the box itself works fine. If you're unsure, publish only the A record and add IPv6 later.
2. Open 80 and 443 — in both places. The VPS firewall (ufw, nftables, whatever you run) and the provider's own security group or cloud firewall. People fix the first and forget the second, then spend an hour wondering why ACME times out. Port 80 must be reachable for the HTTP challenge, even though the final site is HTTPS-only.
3. Install Caddy from the official repository, not the distro package. The version in older distro repos lags, and you want the current config syntax. On Debian and Ubuntu the documented sequence is:
sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https curl
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' \
| sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' \
| sudo tee /etc/apt/sources.list.d/caddy-stable.list
sudo apt update && sudo apt install caddy
4. Write the Caddyfile. Edit /etc/caddy/Caddyfile. The site address is the domain; Caddy handles the redirect from HTTP and the certificate on its own.
app.example.com {
encode gzip zstd
reverse_proxy 127.0.0.1:3000
}
Note the explicit 127.0.0.1. That is deliberate — see step 6.
5. Validate, then reload. Never restart blind.
sudo caddy validate --config /etc/caddy/Caddyfile
sudo systemctl reload caddy
sudo journalctl -u caddy -f
systemctl reload re-reads the config without dropping connections. Watch the journal while you hit the site in a browser; certificate issuance and upstream dial errors both show up there in plain language.
6. The failure you will hit next: 502 with a backend that is clearly running. You curl 127.0.0.1:3000 on the box and get a response. Caddy returns 502 Bad Gateway anyway. The cause is name resolution: reverse_proxy localhost:3000 resolves to both 127.0.0.1 and ::1, and Caddy may dial the IPv6 address first while your app only listens on IPv4. Write the literal address — 127.0.0.1:3000 — and the problem disappears. This is also why you should not bind the app to 0.0.0.0 as a shortcut; keep it on loopback and let Caddy be the only thing exposed.
7. If it's a Unix socket instead of a port, the 502 becomes a permission error. Caddy runs as the caddy user and needs write access to the socket file. Set the socket group to caddy in the app's unit file and make sure the directory is traversable. Same class of problem applies to static files: if you're serving a directory, caddy must be able to read every path component, not just the file.
8. Add the rest of your apps as separate site blocks. One Caddyfile can hold all of them, and each block gets its own certificate independently. If one domain's DNS is wrong, the others still work.
app.example.com {
reverse_proxy 127.0.0.1:3000
}
api.example.com {
reverse_proxy 127.0.0.1:8080
}
9. Know where the state lives. Certificates and ACME account keys sit under /var/lib/caddy/.local/share/caddy. Back that directory up if re-issuing would hit rate limits, and don't wipe it while "cleaning up" the server. If you change the Caddyfile's structure substantially, run caddy fmt --overwrite /etc/caddy/Caddyfile so the file stays readable for the next person.
Where this falls short
Caddy is the wrong tool when you cannot open port 80 and cannot use a DNS challenge. The DNS challenge requires a provider plugin, which means building Caddy yourself with xcaddy rather than using the package — a real step up in maintenance for a small site. If your host blocks inbound 80, plan for that before you start.
It's also the wrong choice behind an existing TLS-terminating load balancer or CDN in "full" mode. In that setup Caddy's automatic HTTPS fights the edge, and you want to either run it on an internal port with auto_https off or accept the edge's certificate and skip the origin one.
Skip Caddy if you need nginx-specific behaviour — map directives, complex try_files chains, third-party modules — or if a control plane already manages your routing. Traefik or a Kubernetes ingress controller earns its complexity when services come and go without you editing files. And if you're running dozens of vhosts across several boxes with shared config, a single Caddyfile per host stops scaling and you want config management, not a better proxy.
One more limit worth naming: Caddy's defaults are opinionated about security headers and protocol versions. That's usually what you want, but if a legacy client breaks, you'll be reading documentation to turn something off rather than adding something on.
FAQ
How do I fix a 502 Bad Gateway in Caddy?
Check the upstream address first. Run curl -v against the exact host and port in your reverse_proxy line from the VPS itself, and replace localhost with 127.0.0.1. If that works and Caddy still fails, read journalctl -u caddy — the dial error names the address it tried.
Does Caddy get HTTPS certificates automatically on a VPS?
Yes, provided the domain resolves to the box and port 80 is reachable from the internet. Caddy requests a certificate on first start for every site block with a hostname, redirects HTTP to HTTPS, and renews in the background. There is no separate certbot step and no cron job to add.
Can I run Caddy behind Cloudflare?
Yes, but decide who terminates TLS. With Cloudflare's proxy on and SSL mode set to Full (strict), Caddy still needs a valid certificate of its own, which means the HTTP challenge has to pass through the proxy or you switch to a DNS challenge. The simpler path for a single VPS is to leave the record DNS-only until the certificate issues.
How do I reverse proxy multiple apps with one Caddyfile?
Add one site block per hostname, each with its own reverse_proxy target. Caddy treats them independently — separate certificates, separate upstreams — so a broken app on one domain doesn't take down the others. Reload with systemctl reload caddy after editing.
Related reading: The Cheapest Way to Run a Node.js API in Production, How to Deploy a Python App Without Docker on a VPS
Bottom line
Install Caddy from the official repo, write one site block with a literal 127.0.0.1:port upstream, open 80 and 443 in both firewalls, and let it handle certificates. That covers the overwhelming majority of single-VPS deployments, and the failure modes you'll actually meet are DNS and loopback resolution, not the proxy itself.
Today: add the A record, confirm port 80 is open from outside with an online checker, then run the install commands and validate the Caddyfile before you reload. Do those three things in that order and the certificate will issue on the first try.