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.

macrozheng
macrozheng
macrozheng
Master Caddy: Simplify Web Server Setup and Advanced Proxy Configurations

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

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/site:/srv \
    -d caddy

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.com to the frontend files of the mall project. 192.168.3.101 mall.macrozheng.com Copy the frontend build to /mydata/caddy/site, then update the Caddyfile and restart the container:

http://mall.macrozheng.com {
    root * /srv/mall
    file_server browse
}

Dynamic Proxy

Forward requests for api.macrozheng.com to the backend API service.

192.168.3.101 api.macrozheng.com
http://api.macrozheng.com {
    reverse_proxy http://admin-api.macrozheng.com
}

Directory‑Based Routing

Serve different sub‑directories under the same domain:

mall.macrozheng.com/admin   # admin UI
mall.macrozheng.com/app    # storefront

Configure routes in the Caddyfile:

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

Gzip Compression

Enable gzip to reduce bandwidth for large assets (e.g., a 1.7 MB JS file compressed to ~500 KB):

http://mall.macrozheng.com {
    root * /srv/mall
    encode {
        gzip
    }
    file_server browse
}

Address Rewriting

Redirect old domain to a new one using the redir directive:

http://mall.macrozheng.com {
    redir https://www.macrozheng.com
}

Caddyfile Syntax Overview

Key elements include global options, snippets, site blocks, matcher definitions, comments, site addresses, and directives such as file_server and 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.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Dockerreverse proxyWeb serverStatic Proxygzip compressionCaddyCaddyfile
macrozheng
Written by

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.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.