Installing Caddy on Ubuntu: Reverse Proxy with Automatic HTTPS in a Heartbeat

Ubuntu tutorial - IT technology blog
Ubuntu tutorial - IT technology blog

Why I “Broke Up” with Nginx for Caddy

Nginx used to be my go-to choice every time I set up a server. But honestly, wrestling with hundred-line config files, installing Certbot, and configuring cronjobs just to maintain an SSL certificate wasted too much of my time. Sometimes, a single missing semicolon would bring the entire system down.

Caddy came as a lifesaver. Instead of spending 30 minutes struggling with Nginx, I only need 2 minutes with Caddy to get that green HTTPS lock. The best part is that Caddy is written in Go, making it extremely lightweight and 100% automated for issuing and renewing SSL from Let’s Encrypt or ZeroSSL. You don’t need any external tools; Caddy handles everything from A to Z.

Deploying Caddy on Ubuntu in 5 Minutes

Before starting, prepare a VPS running Ubuntu (preferably 22.04 or later). Point your domain’s A record to the server’s IP beforehand so Caddy can validate SSL immediately.

1. Installation from the Official Repository

To make future updates easier, I recommend installing via the repository instead of downloading the binary manually.

sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list
sudo apt update
sudo apt install caddy

2. Verify the Service

Immediately after installation, Caddy will activate itself. Run this command to ensure everything is stable:

systemctl status caddy

3. Ultra-Simplified Reverse Proxy Configuration

Imagine you’re running a Node.js API on port 3000. To serve it on the domain itfromzero.io with HTTPS, open the configuration file:

sudo nano /etc/caddy/Caddyfile

Replace all content with these 3 concise lines:

itfromzero.io {
    reverse_proxy localhost:3000
}

Save and reload the Caddy configuration:

sudo systemctl reload caddy

That’s it. Caddy will automatically contact Let’s Encrypt, obtain the certificate, and install it on the server. If you were to do this with Nginx, you might have to write 20-30 lines of code.

Decoding Caddyfile: Configuration Has Never Been This Easy

Caddyfile has a very modern and readable syntax. It bypasses complex technical barriers to focus on what developers need.

The package system on Ubuntu might take a few days to get used to, but with Caddyfile, you only need 15 minutes. Want to run multiple sites on one server? Just list them out:

app1.itfromzero.io {
    reverse_proxy localhost:3000
}

app2.itfromzero.io {
    reverse_proxy localhost:4000
}

Optimizing Caddy for Real-World Projects

In production environments, we need more than just a basic proxy. Here are some configurations I often use to speed up and secure my server.

Serving Static Files (SPAs like React/Vue)

If you’re deploying a built React app, use this directive to handle client-side routing:

mysite.com {
    root * /var/www/mysite/dist
    file_server
    try_files {path} /index.html
}

Enable Zstd & Gzip Compression

To make your website load faster, enable compression. Caddy supports Zstd—a modern compression algorithm that reduces file sizes 10-20% more effectively than traditional Gzip:

itfromzero.io {
    encode zstd gzip
    reverse_proxy localhost:3000
}

Professional Log Management

Don’t let logs get mixed up in the system. Separate them into JSON files for easy monitoring with data analysis tools:

itfromzero.io {
    log {
        output file /var/log/caddy/access.log
        format json
    }
    reverse_proxy localhost:3000
}

A Few Tips from “Hard-Won” Experience

When using Caddy on Ubuntu, there are two things you should know to avoid wasting time. First, always use the caddy validate command before reloading to prevent the server from crashing due to syntax errors. Second, if the server is behind a strict firewall that prevents HTTP challenges, use xcaddy to build a custom version supporting DNS challenges (like Cloudflare).

Conclusion: Should You Choose Caddy or Nginx?

Nginx remains the “performance king” at a massive scale with millions of requests per second. However, for 90% of small to medium projects, Caddy is the clear winner due to its convenience. It saves you the SSL headache, offers clean config files, and lets you focus entirely on coding your app.

Caddy feels modern and smart. If you’re tired of tracking SSL renewal dates, try installing Caddy on Ubuntu today. You surely won’t want to go back to Nginx for your personal projects!

Share: