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

URL: https://levelrail.com/learn/web-servers/caddy-from-scratch/
Updated: 2026-11-27

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](https://levelrail.com/learn/domains-https/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](https://levelrail.com/learn/glossary/#caddy)) turns on HTTPS for it and gets a certificate from an [ACME](https://levelrail.com/learn/glossary/#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](https://levelrail.com/learn/glossary/#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`):

```text
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](https://levelrail.com/learn/web-servers/nginx-reverse-proxy-for-a-node-app/)) for the app side):

```text
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](https://levelrail.com/learn/server-basics/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

| 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](https://levelrail.com/learn/deploy-operate/fix-502-bad-gateway/)) |
| `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

```text
# /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
```

## Related lessons

- [Automatic HTTPS with Caddy](https://levelrail.com/learn/domains-https/automatic-https-with-caddy/)): what Caddy needs for certificates, and Docker volumes.
- [Nginx from scratch](https://levelrail.com/learn/web-servers/nginx-from-scratch/)): the same job with a different tool, to compare.
- [What is a reverse proxy](https://levelrail.com/learn/deploy-operate/what-is-a-reverse-proxy/)): the concept behind reverse_proxy.
- [Point a domain at your server](https://levelrail.com/learn/domains-https/point-a-domain-at-your-server/)): the DNS step Caddy depends on.
- [Deploy a Docker Compose app, self-hosted](https://levelrail.com/blog/deploy-docker-compose-app-self-hosted/)): Caddy in front of a full project.

## Next

Prefer nginx for HTTPS? Continue with [Nginx HTTPS with certbot](https://levelrail.com/learn/web-servers/nginx-https-with-certbot/)).

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

- [Caddy docs, install](https://caddyserver.com/docs/install)
- [Caddy docs, Caddyfile concepts](https://caddyserver.com/docs/caddyfile)
- [Caddy docs, reverse_proxy](https://caddyserver.com/docs/caddyfile/directives/reverse_proxy)
