如何使用 Caddy 部署 HTTPS 服务

前言

Caddy 是现代化的 Web 服务器,最大特点是自动 HTTPS。相比 Nginx 配置更简单,本文记录完整部署过程。

一、安装 Caddy

sudo apt update
sudo apt install caddy
sudo systemctl enable --now caddy

二、基础配置

example.com {
    root * /var/www/html
    file_server
    encode gzip
}

三、高级配置

添加安全响应头和 HTTP/3 支持:

example.com {
    root * /var/www/html
    file_server
    encode gzip zstd
    
    header {
        Strict-Transport-Security "max-age=31536000;"
        X-Content-Type-Options "nosniff"
        X-Frame-Options "SAMEORIGIN"
    }
    
    # HTTP/3 默认启用
    protocols h1 h2 h3
}

四、重启服务

sudo systemctl restart caddy
sudo systemctl status caddy

💡 提示:Caddy 会自动申请和续期 Let's Encrypt 证书,无需手动配置。

← 返回首页

💬 评论 (10)

李明
2026-02-20
很实用的教程!我按照步骤成功部署了,Caddy 确实比 Nginx 简单很多。
J
John Smith
2026-02-21
Great tutorial! Caddy's automatic HTTPS is a game changer. Much easier than dealing with certbot and nginx configs.
王小华
2026-02-21
请问 Caddy 的性能和 Nginx 比怎么样?生产环境可以用吗?
张伟(博主)
2026-02-21
@王小华 Caddy 2.x 性能已经很好了,我们生产环境就在用。对于中小规模完全够用,而且配置维护比 Nginx 省心太多。
M
Maria Garcia
2026-02-22
Does Caddy support reverse proxy? I need to proxy requests to my backend API running on port 3000.
张伟(博主)
2026-02-22
@Maria Garcia Yes! Just add: reverse_proxy localhost:3000 It's that simple!
刘强
2026-02-23
支持 HTTP/3 吗?怎么配置?我看到配置里有 protocols h1 h2 h3 这个。
张伟(博主)
2026-02-23
@刘强 Caddy 默认就支持 HTTP/3!在全局配置加上 protocols h1 h2 h3 就行,已经更新到文章里了。
A
Alex Kumar
2026-02-24
Bookmarked! I'm migrating our company's infrastructure to Caddy next month. This will be helpful.
陈晨
2026-02-25
感谢分享!我们公司准备用 Caddy 替换现有的 Nginx。能否写一篇关于 Caddy 负载均衡的文章?

发表评论

⚠️ 评论功能即将上线,敬请期待