Chapter 8 · Lesson 3 of 8
Web servers and proxies

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.

By the end you will

Install Caddy, serve a static folder and proxy an app with a Caddyfile, then validate, reload and read logs.

Time
20 min
Level
Beginner
Check
4 questions
You need
  • 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.

bash
caddy version
systemctl status caddy --no-pager

What 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):

output
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:

bash
sudo mkdir -p /var/www/example.com/html
echo '<h1>Hello from Caddy</h1>' | sudo tee /var/www/example.com/html/index.html

The 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):

output
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#

bash
sudo caddy fmt --overwrite /etc/caddy/Caddyfile
caddy validate --config /etc/caddy/Caddyfile
sudo systemctl reload caddy

caddy 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#

bash
sudo journalctl -u caddy --no-pager -n 50
sudo journalctl -u caddy -f

Certificate 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#

bash
curl -I http://example.com
curl -I https://example.com

The 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 fmt shows layout mistakes quickly.
  • An IP address or localhost gets 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.
  • reload needs the admin endpoint. The service talks to Caddy on its local admin port. If you disabled it in the config, reload fails and you must restart.
  • A global options block must come first. Settings such as email go 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#

SymptomLikely causeFix
Error: adapting config using caddyfile: ... Unexpected '}'Brace on the wrong lineRun caddy fmt, fix the named line
listen tcp :80: bind: address already in usenginx or Apache holds port 80Stop that service, then sudo systemctl restart caddy
Logs show challenge failed or no valid A recordsDNS wrong, or ports blockedFix the A record, open 80 and 443
Browser says connection refused on 443Caddy not runningsystemctl status caddy, read the journal
502 Bad GatewayNothing on the proxied addressStart the app. See fix 502
403 or blank page on a static sitecaddy user cannot read the folderFix permissions on every parent folder
Reload says connection refused to the admin endpointCaddy is not runningStart the service first
Site shows the default Caddy welcome pageYour block does not match the hostCheck the site address spelling

Quick reference#

output
# /etc/caddy/Caddyfile
example.com {
    root * /var/www/example.com/html
    file_server
}

api.example.com {
    reverse_proxy 127.0.0.1:3000
}
bash
sudo caddy fmt --overwrite /etc/caddy/Caddyfile
caddy validate --config /etc/caddy/Caddyfile
sudo systemctl reload caddy
sudo journalctl -u caddy -n 50 --no-pager

Next#

Prefer nginx for HTTPS? Continue with Nginx HTTPS with certbot.

Check what you learned4 questions
1Which command checks a Caddyfile for errors before you apply it?
2Which directive serves static files from a folder?
3What must be true for Caddy to get a public certificate for example.com?
4Where do you look for certificate errors from the packaged Caddy service?

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

View MD Edit
Share
Helpful?
Progress is saved in this browser only.

Keep going: guides and tutorials

View MD Edit
Share
Helpful?
Run this on your own server

Levelrail deploys apps from git with health-checked releases and one-click rollback.

docker pull ghcr.io/glincker/levelrail:latest Install Levelrail From GLINR Studios