πŸ”§ DevOps

Caddy

Caddy

A web server with automatic HTTPS. Written in Go, simple to configure.

Detailed Explanation

Caddy is a modern web server written in Go, and its most distinctive feature is automatic HTTPS. Matt Holt began developing it in 2015, and it integrates automatically with Let's Encrypt, handling certificate issuance, renewal, and even OCSP stapling automatically.

Its configuration file (Caddyfile) is very concise, making configuration easier than with Nginx or Apache. With just a few lines of configuration, you can implement reverse proxying, load balancing, file serving, compression, header manipulation, and more.

It is distributed as a static binary with no dependencies, and is particularly useful in container environments. Runtime configuration changes are possible through a JSON API, and functionality can be extended through a module system. HTTP/3 (QUIC) is also supported by default.

In practice, it is used for local HTTPS setup in development environments, as a production web server for small to medium-sized projects, and as an alternative to Kubernetes Ingress. Nginx is still widely used for large-scale traffic, but Caddy's share is growing thanks to its convenience.

Code Example

# Caddyfile - ν”„λ‘œλ•μ…˜ μ„€μ • 예제

# κΈ€λ‘œλ²Œ μ„€μ •
{
    email admin@example.com
    acme_ca https://acme-v02.api.letsencrypt.org/directory
}

# 메인 μ‚¬μ΄νŠΈ - μžλ™ HTTPS
example.com {
    # 정적 파일 μ„œλΉ™
    root * /var/www/html
    file_server

    # SPA λΌμš°νŒ…
    try_files {path} /index.html

    # μ••μΆ•
    encode gzip zstd

    # λ³΄μ•ˆ 헀더
    header {
        X-Content-Type-Options nosniff
        X-Frame-Options DENY
        Referrer-Policy strict-origin-when-cross-origin
        -Server
    }

    # 둜그
    log {
        output file /var/log/caddy/access.log
        format json
    }
}

# API λ¦¬λ²„μŠ€ ν”„λ‘μ‹œ
api.example.com {
    reverse_proxy localhost:8080 {
        health_uri /health
        health_interval 10s

        # λ‘œλ“œ λ°ΈλŸ°μ‹± (μ—¬λŸ¬ λ°±μ—”λ“œ μ‹œ)
        # lb_policy round_robin
    }

    # CORS 헀더
    header Access-Control-Allow-Origin *
}

How It's Said in Practice

Senior: "If you need HTTPS in a local development environment, use Caddy. Set up mkcert and three lines of Caddyfile are all it takes."

Junior: "Can I use it in production instead of Nginx?"

Senior: "Absolutely. Automatic certificate renewal is the default, so you don't have to worry about things like a certbot cron job. That said, for very high traffic, Nginx tuning may be more familiar to you."

Interviewer: "How does Caddy's automatic HTTPS work?"

Candidate: "Caddy communicates with Let's Encrypt via the ACME protocol. It handles domain validation (HTTP-01 or DNS-01), certificate issuance, and renewal all automatically, and renews in the background before expiration. OCSP stapling is also automatic, so TLS handshake performance is good as well."

Take a mock interview on this topic β†’ WIA SOOM AI Interview Β· Start for free

Reviewer: "I'd like the hardcoded backend address in the Caddyfile to be changed to an environment variable. It may differ by environment in Docker Compose."

Developer: "Yes, it supports environment variable placeholders like {$BACKEND_HOST}. I'll make the change."

Cautions

Learn More