Caddy自动HTTPS服务器教程


Caddy是一款现代化的Web服务器,最大的特点是自动HTTPS——只需配置域名,Caddy会自动申请和续期Let's Encrypt证书。配置语法简洁直观,非常适合快速部署Web应用。本文介绍在搬瓦工VPS上安装和配置Caddy的完整流程。

安装Caddy

Debian/Ubuntu 系统:

sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https curl
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 -y

CentOS/AlmaLinux 系统:

sudo yum install yum-plugin-copr -y
sudo yum copr enable @caddy/caddy -y
sudo yum install caddy -y

通用二进制安装:

curl -o caddy.tar.gz -L "https://github.com/caddyserver/caddy/releases/latest/download/caddy_2_linux_amd64.tar.gz"
tar xzf caddy.tar.gz
sudo mv caddy /usr/bin/
sudo chmod +x /usr/bin/caddy
caddy version
# 启动Caddy服务
sudo systemctl enable caddy
sudo systemctl start caddy

Caddyfile基本语法

Caddy的配置文件是 /etc/caddy/Caddyfile,语法简洁:

sudo nano /etc/caddy/Caddyfile

最简静态网站配置:

example.com {
    root * /var/www/example.com
    file_server
}

仅需3行配置,Caddy会自动完成:申请Let's Encrypt SSL证书、配置HTTPS、HTTP自动跳转HTTPS、证书自动续期。

反向代理配置

Caddy的反向代理配置同样简洁:

# 基本反向代理
example.com {
    reverse_proxy localhost:3000
}

# 带路径的反向代理
example.com {
    reverse_proxy /api/* localhost:8080
    reverse_proxy /ws/* localhost:8081

    # 其他请求返回静态文件
    root * /var/www/frontend
    file_server
}

# WebSocket代理(自动支持,无需额外配置)
ws.example.com {
    reverse_proxy localhost:8082
}

# 负载均衡
example.com {
    reverse_proxy localhost:3001 localhost:3002 localhost:3003 {
        lb_policy round_robin
        health_uri /health
        health_interval 10s
    }
}

多站点配置

在一个Caddyfile中配置多个站点:

# 站点A - 静态网站
site-a.com {
    root * /var/www/site-a
    file_server
    encode gzip
}

# 站点B - Node.js应用
site-b.com {
    reverse_proxy localhost:3000
}

# 站点C - PHP应用
site-c.com {
    root * /var/www/site-c
    php_fastcgi unix//run/php/php8.2-fpm.sock
    file_server
}

# 带www重定向
www.example.com {
    redir https://example.com{uri} permanent
}

常用功能配置

Gzip压缩:

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

自定义错误页面:

example.com {
    root * /var/www/example.com
    file_server

    handle_errors {
        rewrite * /{err.status_code}.html
        file_server
    }
}

访问日志:

example.com {
    log {
        output file /var/log/caddy/example.com.log {
            roll_size 10mb
            roll_keep 5
        }
        format json
    }
    reverse_proxy localhost:3000
}

Basic Auth认证:

# 生成密码哈希
caddy hash-password --plaintext yourpassword

example.com {
    basicauth /admin/* {
        admin $2a$14$xxxhashxxx
    }
    reverse_proxy localhost:3000
}

请求头设置:

example.com {
    header {
        X-Content-Type-Options nosniff
        X-Frame-Options SAMEORIGIN
        Referrer-Policy strict-origin-when-cross-origin
        -Server
    }
    reverse_proxy localhost:3000
}

HTTPS高级配置

Caddy默认使用Let's Encrypt,也支持自定义证书和其他CA:

# 使用自定义证书
example.com {
    tls /etc/ssl/certs/example.com.pem /etc/ssl/private/example.com.key
    reverse_proxy localhost:3000
}

# 仅内网使用,禁用自动HTTPS
:8080 {
    reverse_proxy localhost:3000
}

# 使用ZeroSSL替代Let's Encrypt
{
    acme_ca https://acme.zerossl.com/v2/DV90
}

example.com {
    reverse_proxy localhost:3000
}

Caddy管理命令

# 验证配置文件
caddy validate --config /etc/caddy/Caddyfile

# 重新加载配置(不中断服务)
sudo systemctl reload caddy

# 格式化Caddyfile
caddy fmt --overwrite /etc/caddy/Caddyfile

# 查看当前运行配置
caddy adapt --config /etc/caddy/Caddyfile

# 查看证书信息
caddy trust

# 查看Caddy日志
sudo journalctl -u caddy --no-pager -f

Caddy vs Nginx

Tip: 更多教程请查看新手教程
关于本站

搬瓦工VPS中文网(bwgvps.com)是非官方中文信息站,整理搬瓦工 BandwagonHost 的优惠信息、使用教程和方案对比,方便中文用户选购和使用。

新手必读
搬瓦工优惠码

当前最大折扣优惠码:

NODESEEK2026(优惠 6.77%)

在购买方案时填入优惠码即可自动抵扣。详见 优惠码使用教程