Introduction

Managing multiple applications on a single server can get messy pretty quickly. You have a frontend, a backend, documentation and perhaps a few self-hosted services, all running on different ports. You want each of them accessible through its own domain, preferably with HTTPS, without having to expose every application port to the internet.

This is where reverse proxies come in. NGINX has traditionally been one of the most popular choices, but recently I have found myself reaching for something else: Caddy.

Caddy is a modern web server and reverse proxy written in Go. What makes it interesting is how much functionality it provides with surprisingly little configuration. Automatic HTTPS, certificate renewal, reverse proxying, load balancing and HTTP/3 are all supported without having to assemble a collection of external tools.

I have been using Caddy quite extensively on my personal servers. My experience with it grew significantly while working on VPNLens, where I used it to expose multiple services through different subdomains.

Let's look at what makes Caddy interesting, how it works internally, and why I have started preferring it over NGINX for many of my deployments.

What is Caddy?

At its core, Caddy is a web server that can serve static files, handle HTTPS connections and act as a reverse proxy. However, its modular architecture makes it much more than a traditional web server.

One of its biggest selling points is automatic HTTPS. Normally, setting up HTTPS involves obtaining certificates, configuring certificate paths, arranging renewals and making sure your web server uses the correct certificates. Caddy automates this entire process through ACME-compatible certificate authorities.

You specify a domain, point its DNS records to your server, ensure the required ports are accessible, and Caddy handles certificate issuance, renewal and HTTPS configuration automatically. It also redirects HTTP traffic to HTTPS by default.

You don't need a separate Certbot installation, certificate renewal cron jobs or manually maintained SSL configurations. Caddy takes care of the certificate lifecycle itself.

How does Caddy work under the hood?

There is something interesting about Caddy that you might not notice when using it: the Caddyfile is not its native configuration format.

Caddy's native configuration is JSON. The Caddyfile is simply a more human-friendly way of defining that configuration. When Caddy starts, a config adapter parses the Caddyfile and converts it into the JSON structure the server understands.

Internally, Caddy uses a modular architecture. Different modules are responsible for HTTP servers, TLS, reverse proxying, logging and other functionality. These modules are loaded, provisioned and validated before being used by the running application.

When an HTTP request arrives, Caddy's HTTP server evaluates the configured routes and matchers to determine which handlers should process it. Those handlers can serve static files, modify requests, apply compression or forward traffic to another application.

For a reverse proxy request, Caddy forwards the incoming request to the configured upstream, receives its response and returns it to the client. TLS termination happens at Caddy, so your backend applications can continue running on internal HTTP ports when appropriate.

This modular approach is also what makes Caddy extensible. You can add functionality through additional modules rather than modifying the entire server.

What makes Caddy useful?

Automatic HTTPS is probably the feature that initially attracts most people to Caddy, but there is considerably more to it. Caddy supports HTTP/1.1, HTTP/2 and HTTP/3, with HTTP/3 using QUIC over UDP. It also supports response compression through gzip and Zstandard, reducing bandwidth consumption when serving websites and APIs.

Its reverse proxy is quite capable too. You can configure multiple upstream servers, use different load-balancing policies, perform health checks, retry requests and customize upstream transport behavior. This makes it suitable for deployments that go beyond forwarding requests to a single local port.

Caddy also has a powerful routing system. Its matchers and handlers allow you to route traffic based on domains, paths, headers and other request properties. You can rewrite URLs, manipulate headers, serve static files, return custom responses and combine multiple services behind a single domain.

Another feature I appreciate is its administration API and graceful configuration reloads. Caddy can replace its active configuration without requiring a complete server restart. If a new configuration fails to load, it can retain the previous working configuration.

Finally, Caddy is highly extensible. Its module system allows developers to add functionality, and custom builds can be created using xcaddy.

Caddy vs NGINX

NGINX is an excellent web server. It has been around for years, has a massive ecosystem and is used in production infrastructure across the internet. There is no reason to dismiss it.

However, Caddy takes a different approach to configuration.

Consider a basic reverse proxy in NGINX:

server { listen 80; server_name example.com; location / { proxy_pass http://localhost:3000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } } 

The equivalent Caddy configuration is:

example.com { reverse_proxy localhost:3000 } 

That's the entire configuration. Caddy handles the usual proxy headers and automatically manages HTTPS for qualifying domain names.

The difference becomes even more noticeable when managing multiple domains. With NGINX, you generally maintain separate server blocks and configure certificate management through an external ACME client or another solution. With Caddy, you can define all your domains in one file and let its automatic HTTPS functionality take care of the certificates.

There is also an architectural difference. NGINX uses an event-driven architecture with a master process and worker processes. Caddy is written in Go and uses goroutines and Go's networking facilities to handle concurrency.

Both are capable of handling substantial workloads. Performance depends on the application, configuration and environment, so claiming that one is universally faster would be misleading.

For my personal infrastructure, however, configuration simplicity and integrated HTTPS make Caddy a very attractive choice.

Running Caddy with Docker

Caddy doesn't even need to be installed directly on your host. You can run it as a Docker container, which is particularly useful when you have multiple applications deployed on the same machine.

Here is a basic Docker Compose configuration:

services: caddy: image: caddy:2 container_name: caddy restart: unless-stopped ports: - "80:80" - "443:443" - "443:443/udp" volumes: - ./Caddyfile:/etc/caddy/Caddyfile:ro - caddy_data:/data - caddy_config:/config volumes: caddy_data: caddy_config: 

The ports expose HTTP, HTTPS and HTTP/3 traffic. The Caddyfile is mounted read-only, while /data and /config store persistent state, including certificates and autosaved configuration.

One important thing to understand about Docker networking is that localhost inside a container refers to that container itself. If your applications are also running inside Docker, you should generally put them on a shared Docker network and use their Compose service names as upstream addresses.

This means Caddy can communicate with your backend containers directly without exposing their application ports publicly.

Adding custom domains is surprisingly easy

This is probably one of my favourite things about Caddy.

Suppose you have just deployed another application on port 4000 and want to make it available at app.example.com. You don't need to create a complicated virtual host configuration, obtain a certificate manually or configure a separate renewal process.

You simply add another block to your Caddyfile:

app.example.com { reverse_proxy localhost:4000 } 

That's it.

As long as the DNS record for app.example.com points to your server and the required network ports are accessible, Caddy can automatically obtain a TLS certificate and start serving the domain over HTTPS.

You can repeat this for as many services and domains as your deployment requires. Caddy handles the certificate lifecycle for each qualifying hostname.

This becomes particularly useful when you frequently deploy new applications. Adding another service to your infrastructure can be as simple as adding a few lines to a configuration file.

My actual Caddy configuration from VPNLens

Now let's look at something I have actually deployed.

While working on VPNLens, I needed to expose several different services through individual subdomains. The project has a frontend, backend API, documentation, WireGuard management interface and a Headscale server.

Instead of maintaining separate reverse proxy configurations, I used a single Caddyfile:

{ email samay15jan@gmail.com } vpn.samay15jan.com { reverse_proxy localhost:3000 } backend.vpn.samay15jan.com { reverse_proxy localhost:3001 } docs.vpn.samay15jan.com { reverse_proxy localhost:3002 } wg.vpn.samay15jan.com { reverse_proxy localhost:51821 } hs.vpn.samay15jan.com { handle_path /admin* { reverse_proxy localhost:3003 } handle { reverse_proxy localhost:51822 } } 

The global options block specifies the email address used for ACME certificate management. Each domain block then defines where its incoming traffic should go. The frontend is available on port 3000, the backend on 3001, documentation on 3002 and the WireGuard management interface on 51821.

The Headscale configuration is slightly more interesting. I use handle_path to forward requests beginning with /admin to port 3003, while all other requests go to port 51822. An important detail here is that handle_path automatically strips the matching path prefix before forwarding the request. So a request to /admin/dashboard reaches the upstream as /dashboard.

This lets me expose multiple services through the same domain without having to create additional subdomains.

Docker Compose configuration

This is the actual Compose configuration I used alongside that Caddyfile:

services: caddy: image: caddy:2.10.0-alpine container_name: caddy network_mode: host restart: unless-stopped volumes: - ./Caddyfile:/etc/caddy/Caddyfile:ro - config:/config - data:/data volumes: config: data: 

Notice the network_mode: host option. This makes the container share the host's network namespace. That is why I can use localhost:3000, localhost:3001 and the other host ports directly in my Caddyfile.

I don't need to publish individual ports using Docker's ports directive because Caddy is using the host network directly.

This is particularly convenient when your applications are already running on the host instead of inside Docker. However, host networking also removes the usual network isolation provided by Docker bridge networking, so you should understand which services are listening on which interfaces.

A small note about cloud firewall rules

One thing worth mentioning, especially if you're hosting on a cloud provider like Oracle Cloud Infrastructure (OCI), is how this setup simplifies firewall management.

You still need to allow inbound TCP traffic on ports 80 and 443 in your OCI security lists or network security groups. UDP 443 is also needed if you want HTTP/3.

However, you don't need to expose every individual application port.

For example, ports 3000, 3001, 3002 and 51822 can remain closed to external traffic, provided the applications are reachable internally by Caddy. The reverse proxy receives incoming traffic on the standard web ports and forwards it to the appropriate local service.

This is one of the reasons I like this architecture. Instead of opening a collection of application ports on my cloud firewall, I only need to expose the reverse proxy.

Why I keep using Caddy

I use Caddy quite frequently in my own infrastructure, especially when deploying services on my personal server. Whenever I spin up a new application, I usually just add another domain block to my Caddyfile, reload the configuration and I'm done.

My experience with VPNLens made me appreciate this even more. Managing several services, each with its own domain and routing requirements, became much easier with a single configuration file.

I'm not saying NGINX is bad. It remains an excellent choice, particularly for deployments that already depend on its ecosystem. But for personal infrastructure, self-hosted applications and smaller deployments, Caddy is an extremely convenient alternative.

Caddy is one of those tools that makes infrastructure feel less complicated without sacrificing the flexibility you need when things get bigger. A small configuration file, automatic HTTPS, Docker support and a powerful reverse proxy are a pretty good combination.

And honestly, that's why it has earned a permanent place in my server setup.

Resources: Caddy Documentation · Caddy Features · VPNLens