Master Caddy: Simplify Web Server Setup and Advanced Proxy Configurations
This tutorial introduces Caddy, a modern Go‑based web server, shows how to install it with Docker, and demonstrates static and dynamic proxy setups, directory routing, gzip compression, and address rewriting for a SpringBoot‑Vue e‑commerce project.
Caddy Overview
Caddy is a powerful, highly extensible web server written in Go, currently boasting over 58K stars on GitHub. It supports static file hosting and reverse proxying.
Key Features
Caddyfile simple configuration format.
Dynamic JSON API for live configuration without restart.
HTTPS enabled by default for every site.
High scalability, suitable for hundreds of thousands of sites.
Native support for HTTP/1.1, HTTP/2, and HTTP/3.
Cross‑platform operation with no external dependencies.
Built with Go, offering memory safety and reliability.
Installation via Docker
<code>docker pull caddy</code> <code>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/site:/srv \
-d caddy</code>mall Project Overview
The mall project is a SpringBoot3 + Vue e‑commerce system (≈60K GitHub stars) with a modular backend and a 2024 micro‑service architecture, deployed using Docker and Kubernetes. It includes modules for products, orders, carts, permissions, coupons, members, and payments.
Boot project: https://github.com/macrozheng/mall
Cloud project: https://github.com/macrozheng/mall-swarm
Documentation site: https://www.macrozheng.com
Static Proxy
Map
mall.macrozheng.comto the frontend files of the mall project.
<code>192.168.3.101 mall.macrozheng.com</code>Copy the frontend build to
/mydata/caddy/site, then update the Caddyfile and restart the container:
<code>http://mall.macrozheng.com {
root * /srv/mall
file_server browse
}</code>Dynamic Proxy
Forward requests for
api.macrozheng.comto the backend API service.
<code>192.168.3.101 api.macrozheng.com</code> <code>http://api.macrozheng.com {
reverse_proxy http://admin-api.macrozheng.com
}</code>Directory‑Based Routing
Serve different sub‑directories under the same domain:
<code>mall.macrozheng.com/admin # admin UI
mall.macrozheng.com/app # storefront</code>Configure routes in the Caddyfile:
<code>http://mall.macrozheng.com {
route /admin/* {
uri strip_prefix /admin
file_server {
root /srv/admin
}
}
route /app/* {
uri strip_prefix /app
file_server {
root /srv/app
}
}
}</code>Gzip Compression
Enable gzip to reduce bandwidth for large assets (e.g., a 1.7 MB JS file compressed to ~500 KB):
<code>http://mall.macrozheng.com {
root * /srv/mall
encode {
gzip
}
file_server browse
}</code>Address Rewriting
Redirect old domain to a new one using the
redirdirective:
<code>http://mall.macrozheng.com {
redir https://www.macrozheng.com
}</code>Caddyfile Syntax Overview
Key elements include global options, snippets, site blocks, matcher definitions, comments, site addresses, and directives such as
file_serverand
reverse_proxy. These building blocks enable flexible configuration of static and dynamic proxies.
Conclusion
Caddy’s concise configuration language and powerful directives allow developers to set up static sites, reverse proxies, compression, and redirects with minimal effort, making it an elegant alternative to traditional servers like Nginx.
macrozheng
Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.
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.