DEV Community

Cover image for Caddy Web Server Tutorial: Serve a Shop Site in Minutes
The Practical Developer
The Practical Developer

Posted on Originally published at practicaldevblog.blogspot.com

Caddy Web Server Tutorial: Serve a Shop Site in Minutes

Quick answer

Caddy is a web server written in Go that serves static files and reverse proxies a backend with a short config file called a Caddyfile, and it handles HTTPS certificates on its own when a real domain is in play. This post serves a Karachi fabric shop site and proxies its orders API through one Caddyfile, all on localhost.

A fabric shop in Gulshan takes orders by phone and keeps the list in a notebook, while its website is a page hosted wherever it was cheapest. The owner wants one server for two jobs: the shop page and the orders API. NGINX can do it, but its config reads like a manual, and a wrong line fails quietly until a customer reports it. Caddy promises the same two jobs with a config file of a few lines, and checks that file before it starts. The question is whether a config that short can be trusted, so the only honest test is to run it.

What I would check before using it

My first check is the config file. A Caddyfile for this job is about ten lines: a site address, a root folder, one reverse proxy rule, and file_server. The NGINX Explained: The Server in Front of Your Server post shows the same job taking more lines and more syntax. Short config only wins if it is checked, and Caddy validates the file before serving, as the broken file below proves.

My second check is HTTPS. Caddy's best-known feature is automatic HTTPS: give it a real domain and it fetches and renews the certificate on its own. This demo runs on localhost only, so there is no domain and no certificate here, and the certificate flow needs a staging domain test before going live.

My third check is maturity. Caddy is written in Go, ships as one binary, and has been around since 2015. NGINX still has the bigger community, but Caddy's plugins cover the common cases.

My fourth check is the team that will run it. A static page plus one small API fits in an afternoon and one person can own it. If the shop already runs its services in containers, the Docker Explained: Containers Without the Confusion post shows how that packaging works.

My verdict, as judgement: use Caddy for a small site plus API where a short, checked config matters more than the biggest community. Keep NGINX when the deployment already uses it. The recommendation flips if the site needs a real domain with certificates and nobody owns that flow, because then automatic HTTPS decides it.

A shop site, a small API, and one Caddyfile

The shop is Gulshan Fabrics in Karachi. Its site is one HTML page, and its orders live in a small JSON file served by a Python server on port 8001. Caddy listens on port 8080, serves the page, and passes every request under /api/ to that server. Everything runs on localhost, with no real domain and no TLS.

First, download Caddy, one static binary:

$ curl -L -o caddy.tar.gz https://github.com/caddyserver/caddy/releases/download/v2.10.0/caddy_2.10.0_linux_amd64.tar.gz
$ tar -xzf caddy.tar.gz
$ ./caddy version
v2.10.0 h1:fonubSaQKF1YANl8TXqGcn4IbIRUDdfAkpcsfI/vX5U=
Enter fullscreen mode Exit fullscreen mode

Then write the Caddyfile: listen on port 8080, serve files from the shop folder, and send anything under /api/ to the Python server on port 8001. handle_path also strips the /api prefix, so the backend sees a plain /orders.json:

:8080 {
    root * /home/hatch/workspace/.toolchains/verify/batch58-caddy/shop

    handle_path /api/* {
        reverse_proxy localhost:8001
    }

    file_server
}
Enter fullscreen mode Exit fullscreen mode

Before starting the server, check the file. The first check failed, because the draft was missing its closing brace:

$ ./caddy validate --config Caddyfile.broken --adapter caddyfile
Error: adapting config using caddyfile: syntax error: unexpected token 'file_server', expecting '}', at Caddyfile.broken:8
Enter fullscreen mode Exit fullscreen mode

Add the missing brace and validate again:

$ ./caddy validate --config Caddyfile --adapter caddyfile
Valid configuration
Enter fullscreen mode Exit fullscreen mode

Start the backend first, then Caddy. The orders service is a plain Python file server; the Koa Tutorial: Build a Node.js Orders API From Scratch post shows the same job as a small Node API:

$ python3 -m http.server 8001 --directory api
$ ./caddy run --config Caddyfile --adapter caddyfile
Enter fullscreen mode Exit fullscreen mode
$ curl -s -i http://localhost:8080/
HTTP/1.1 200 OK
Content-Length: 474
Content-Type: text/html; charset=utf-8
Server: Caddy

<h1>Gulshan Fabrics, Karachi</h1>
<p>Fresh lawn and cotton, delivered across Karachi.</p>
Enter fullscreen mode Exit fullscreen mode
$ curl -s -i http://localhost:8080/api/orders.json
HTTP/1.1 200 OK
Content-Type: application/json
Server: SimpleHTTP/0.6 Python/3.12.3
Via: 1.0 Caddy

{"orders":[{"id":201,"customer":"Ayesha","city":"Karachi","items":["lawn kurta"],"total":2450},{"id":202,"customer":"Bilal","city":"Karachi","items":["shalwar kameez set","dupatta"],"total":5750},{"id":203,"customer":"Hina","city":"Lahore","items":["men's kurta"],"total":3150}]}
Enter fullscreen mode Exit fullscreen mode

Terminal output showing a Caddy demo: Caddy v2.10.0, the Caddyfile passing validation, a Karachi fabric shop page served with HTTP 200, an orders API proxied with HTTP 200, and a 502 Bad Gateway after the backend is stopped.

The numbers check out: Karachi orders of Rs 2,450 plus Rs 5,750 come to Rs 8,200, and all three add to Rs 11,350.

The thing that went wrong came next. With the backend stopped, the same API request returned a 502 with an empty body, while the shop page kept working:

$ curl -s -i http://localhost:8080/api/orders.json
HTTP/1.1 502 Bad Gateway
Server: Caddy
Content-Length: 0
Enter fullscreen mode Exit fullscreen mode

The fix is to start the backend again, and the same request returns 200 at once. That split is the point: the static site keeps serving while the API is down, and the 502 says which side failed.

Common mistakes and how to fix them

First, a broken Caddyfile. The symptom is the validate error above, naming the exact line, such as "syntax error: unexpected token 'file_server', expecting '}'". The fix is to run caddy validate before starting and repair the line it names.

Second, a stopped backend behind the proxy. The symptom is HTTP 502 Bad Gateway with an empty body on every /api/ request, while static pages still load. The fix is to start the backend on the port the Caddyfile names, and check the reverse_proxy address matches it.

Third, a page that does not exist. The symptom is HTTP 404, which means the file is not in the root folder. The fix is to check the file name and the root path, remembering the root is the folder Caddy serves from, not the folder the Caddyfile sits in.

Fourth, expecting a certificate on localhost. The symptom is surprise when the demo serves plain HTTP even though Caddy is famous for automatic HTTPS. The fix is to remember that certificates need a real domain, so localhost stays on HTTP and the certificate flow needs a staging domain.

Frequently Asked Questions

Does Caddy need a domain name to work? No. A domain is only needed for automatic HTTPS. On localhost Caddy serves HTTP on any port, as this demo does on port 8080.

Is Caddy free to use? Yes. Caddy is open source under the Apache 2.0 licence, and the binary from its GitHub releases costs nothing. Paid support exists, but the server itself is free.

Can Caddy replace NGINX? For a small site plus API, yes, and the config is shorter. NGINX still wins when the deployment already uses it, or needs a module only NGINX has.

What happens when the backend behind Caddy stops? Caddy answers the proxied requests with 502 Bad Gateway and an empty body, while static files keep serving. Restart the backend and they return 200 again.

What to remember

  • A Caddyfile of about ten lines serves a static site and proxies an API.
  • Run caddy validate before starting: Caddy names the exact broken line.
  • handle_path strips the /api prefix, so the backend sees a plain /orders.json.
  • A stopped backend shows up as 502 Bad Gateway, while static pages keep working.
  • Automatic HTTPS needs a real domain; on localhost Caddy serves plain HTTP.

Where this leaves you

Caddy earns its keep the moment a config file stops being a document to study and becomes ten lines you can read at a glance. The demo proved it: the shop page served with a 200, the orders API answered through the proxy with its totals adding to Rs 11,350, a broken Caddyfile was caught by validate before anything started, and a stopped backend showed up as an honest 502 instead of a silent failure. Take a static site you already run, put Caddy in front of it with a Caddyfile like this one, and see how much server config you can throw away.

Further reading

Waqas Shahid is a software engineer from Karachi with more than 12 years of experience, including 6+ years in engineering leadership. He writes The Practical Developer.

Top comments (0)