Caddy from scratch: Caddyfile, file_server and reverse_proxy
Caddy is a web server configured by a short Caddyfile, where a site address plus file_server or reverse_proxy is enough to serve a site with automatic HTTPS.
- Updated Nov 27, 2026
- Markdown
Install Caddy, serve a static folder and proxy an app with a Caddyfile, then validate, reload and read logs.
- An Ubuntu server you can reach over SSH with sudo
- A domain whose A record points at the server
- Ports 80 and 443 open and free
Caddy reads one small file, the Caddyfile, and does the rest: certificates, redirects from HTTP to HTTPS and sensible defaults. This lesson covers the file format and the daily commands. For the certificate side in depth, see automatic HTTPS with Caddy.
How it works#
A Caddyfile is a list of site blocks. Each block starts with a site address and holds directives. If the address is a public domain name, Caddy turns on HTTPS for it and gets a certificate from an ACME authority. The proof of ownership is an HTTP or TLS challenge from the internet, so the domain must point at this server and ports 80 and 443 must be reachable and free.
Directives run in a fixed internal order, not the order you write them, so you rarely care about sequencing. Two directives cover most small sites: file_server serves files and reverse_proxy forwards to an app. The Debian and Ubuntu package runs Caddy as a systemd service named caddy, with the config at /etc/caddy/Caddyfile.
Step 1: Install Caddy#
Use the apt repository steps on the official page, https://caddyserver.com/docs/install, under the Debian, Ubuntu and Raspbian section. In short, the page tells you to install a few helper packages, add Caddy’s signing key and apt source list as shown there, run sudo apt update, then sudo apt install caddy. Copy the exact commands from that page, because the key and repository details can change.
caddy version
systemctl status caddy --no-pagerWhat you should see: a version string and active (running). The package starts Caddy with a default Caddyfile that serves a welcome page on port 80. Stop any nginx or Apache first with sudo systemctl disable --now nginx, since only one program can hold ports 80 and 443.
Step 2: Serve a static site#
Replace /etc/caddy/Caddyfile (use sudo):
example.com {
root * /var/www/example.com/html
file_server
}root * path sets the folder for all requests (the * matches every path). file_server serves from it and answers index.html for a folder. Create the folder and a file first:
sudo mkdir -p /var/www/example.com/html
echo '<h1>Hello from Caddy</h1>' | sudo tee /var/www/example.com/html/index.htmlThe caddy user must be able to read the folder. World readable files are fine.
Step 3: Proxy an app instead#
For an app on 127.0.0.1:3000 (see nginx reverse proxy for a Node app for the app side):
example.com {
encode gzip
reverse_proxy 127.0.0.1:3000
}reverse_proxy sets X-Forwarded-For, X-Forwarded-Proto and X-Forwarded-Host for you and passes the original Host through, so you write none of the header lines nginx needs. encode gzip compresses responses. You can serve two sites by adding a second block, and you can mix them, for example www.example.com { redir https://example.com{uri} }.
Step 4: Format, validate and reload#
sudo caddy fmt --overwrite /etc/caddy/Caddyfile
caddy validate --config /etc/caddy/Caddyfile
sudo systemctl reload caddycaddy fmt fixes indentation (without --overwrite it prints the result and changes nothing). caddy validate parses and loads the config without serving. reload applies it with no downtime and leaves the old config running if the new one is invalid.
Step 5: Read the logs#
sudo journalctl -u caddy --no-pager -n 50
sudo journalctl -u caddy -fCertificate messages, startup errors and ACME failures show here. For general journal use see read logs with journalctl. Access logs are off by default in a plain site block. Add a log directive inside the block when you want them.
Check it worked#
curl -I http://example.com
curl -I https://example.comThe first should return a redirect to HTTPS (a 308 with a Location header). The second should return 200 with a valid certificate and no -k flag. The padlock in a browser confirms it.
Edge cases and gotchas#
- Tabs and braces matter. The opening brace must be on the site address line, and a closing brace sits alone on its line.
caddy fmtshows layout mistakes quickly. - An IP address or
localhostgets no public certificate. Caddy uses its own local authority there, which browsers do not trust until you install its root. - Staging first avoids rate limits. While experimenting, repeated failed requests can hit the authority’s limits. Fix DNS and ports before retrying many times.
reloadneeds the admin endpoint. The service talks to Caddy on its local admin port. If you disabled it in the config,reloadfails and you must restart.- A global options block must come first. Settings such as
emailgo in a block with no address at the top of the file, before any site. - Docker needs a volume for
/data. Without it, certificates vanish when the container is recreated. - One config, one writer. If you also use the JSON API to change config, the Caddyfile reload overwrites those changes.
Troubleshooting#
| Symptom | Likely cause | Fix |
|---|---|---|
Error: adapting config using caddyfile: ... Unexpected '}' | Brace on the wrong line | Run caddy fmt, fix the named line |
listen tcp :80: bind: address already in use | nginx or Apache holds port 80 | Stop that service, then sudo systemctl restart caddy |
Logs show challenge failed or no valid A records | DNS wrong, or ports blocked | Fix the A record, open 80 and 443 |
| Browser says connection refused on 443 | Caddy not running | systemctl status caddy, read the journal |
502 Bad Gateway | Nothing on the proxied address | Start the app. See fix 502 |
403 or blank page on a static site | caddy user cannot read the folder | Fix permissions on every parent folder |
Reload says connection refused to the admin endpoint | Caddy is not running | Start the service first |
| Site shows the default Caddy welcome page | Your block does not match the host | Check the site address spelling |
Quick reference#
# /etc/caddy/Caddyfile
example.com {
root * /var/www/example.com/html
file_server
}
api.example.com {
reverse_proxy 127.0.0.1:3000
}sudo caddy fmt --overwrite /etc/caddy/Caddyfile
caddy validate --config /etc/caddy/Caddyfile
sudo systemctl reload caddy
sudo journalctl -u caddy -n 50 --no-pagerRelated lessons#
- Automatic HTTPS with Caddy: what Caddy needs for certificates, and Docker volumes.
- Nginx from scratch: the same job with a different tool, to compare.
- What is a reverse proxy: the concept behind reverse_proxy.
- Point a domain at your server: the DNS step Caddy depends on.
- Deploy a Docker Compose app, self-hosted: Caddy in front of a full project.
Next#
Prefer nginx for HTTPS? Continue with Nginx HTTPS with certbot.
Check what you learned
Frequently asked questions
Where is the Caddyfile on Ubuntu?
The packaged service reads /etc/caddy/Caddyfile. Edit it with sudo, then validate and reload.
How do I reload Caddy after editing the Caddyfile?
Run sudo systemctl reload caddy. It applies the new config without dropping connections, and keeps the old one if the new one is invalid.
What is the difference between file_server and reverse_proxy in Caddy?
file_server serves files from a folder you set with root. reverse_proxy forwards requests to another address, such as an app on 127.0.0.1:3000.
Why does Caddy not get a certificate for my domain?
The usual causes are DNS not pointing at this server, ports 80 or 443 blocked, or another program holding those ports. Read the logs with journalctl -u caddy.
Sources
Keep going: guides and tutorials
- Self-Hosted Static Site Hosting: Deploy a Folder With Levelrail Self-hosted static site hosting from a plain folder: one deploy command, _redirects, _headers and a custom 404 honored, plus a one command rollback.
- Deploy Next.js on a VPS With Docker, Git Push and HTTPS Deploy a Next.js app to your own VPS with Docker, a health check, HTTPS and rollback. Includes the NEXT_PUBLIC build trap that leaves variables empty.