Comprehensive Guide to Installing and Using the Caddy Web Server
This article provides a detailed tutorial on installing Caddy on CentOS and Docker, configuring its powerful Caddyfile syntax, enabling HTTPS, reverse and forward proxy, gzip compression, address rewriting, and directory routing, demonstrating how Caddy can replace Nginx for modern web hosting.
Caddy is a popular, Go‑based web server with over 38K stars on GitHub, offering simple configuration via Caddyfile, dynamic Admin API, automatic HTTPS, and high scalability.
Caddy Introduction
Caddy supports static file serving and reverse proxy, and its key features include a concise Caddyfile, Admin API for live config changes, built‑in HTTPS, ability to handle thousands of sites, no extra dependencies, and memory‑safe Go implementation.
Installation
On CentOS 8, install Caddy using DNF:
dnf install 'dnf-command(copr)'
dnf copr enable @caddy/caddy
dnf install caddyCheck the service status with systemctl status caddy . Caddy registers as a system service but is not started automatically.
Basic Usage
Run Caddy on port 2015 to return Hello, world! :
caddy runUse caddy start to run it in the background. Caddy can convert a Caddyfile to JSON with caddy adapter and reload the configuration with caddy reload .
Caddyfile Basic Syntax
The Caddyfile uses directives such as file_server , reverse_proxy , encode , redir , and route . A sample Caddyfile:
:2015
respond "Hello, world!"Reverse Proxy
Static proxy example mapping docs.macrozheng.com and mall.macrozheng.com to local directories, and dynamic proxy forwarding api.macrozheng.com to an upstream API service.
http://api.macrozheng.com {
reverse_proxy http://admin-api.macrozheng.com
}Gzip Compression
Enable gzip with the encode directive to reduce a 1.7 MB JavaScript file to 544 KB, improving load speed.
http://mall.macrozheng.com {
root * /mydata/caddy/html/mall
encode { gzip }
file_server browse
}Address Rewrite
Redirect old domains to new ones using redir :
http://docs.macrozheng.com {
redir http://www.macrozheng.com
}Directory‑Based Routing
Serve multiple front‑end projects under one domain with route and uri strip_prefix :
http://www.macrozheng.com {
route /admin/* {
uri strip_prefix /admin
file_server { root /mydata/caddy/html/admin }
}
route /app/* {
uri strip_prefix /app
file_server { root /mydata/caddy/html/app }
}
file_server * { root /mydata/caddy/html/www }
}HTTPS
Caddy automatically obtains TLS certificates; simply configure the domain in DNS and add a basic site block:
docs.macrozheng.com {
root * /mydata/caddy/html/docs
file_server browse
}Start the server with caddy run .
Docker Support
Pull the official image and mount the Caddyfile, data, and HTML directories:
docker pull caddy
docker run -p 80:80 -p 443:443 --name caddy \
-v /mydata/caddy/Caddyfile:/etc/caddy/Caddyfile \
-v /mydata/caddy/data:/data \
-v /mydata/caddy/html:/usr/share/caddy \
-d caddyEnter the container with docker exec -it caddy /bin/sh to run Caddy commands as on a bare‑metal host.
Conclusion
Caddy’s concise configuration, automatic HTTPS, and rich feature set make it an elegant alternative to Nginx for static hosting, reverse proxying, compression, and more.
IT Services Circle
Delivering cutting-edge internet insights and practical learning resources. We're a passionate and principled IT media platform.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.