caddy
caddy 是什么
golang 编写的反向代理服务器, 并提供了很多的插件, 可以自动注册 https 证书, 对懒人极度友好
版本选择
目前分为 v1, v2 两个版本, 本人迁移发现 v1,v2 版本并不兼容(Caddyfile), 建议使用 V2 版本, 但是目前中文教程里面使用 v1 版本的还是有很多 v1 版本密码认证是明文, v2 是一个 hash 后的密文
下载 && 安装
下载
在很早的时候 caddy 跟 cloudflare 不太兼容, 会导致无法注册 https 证书, 虽然概率不高, 但是遇见了就比较蛋疼, 建议选择上 caddy-dns/cloudflare 插件
安装
下载完二进制文件后, 建议配置 systemd, 参考 官方文档 以下是我自己使用的配置文件, 大家可以参考下
# caddy.service
#
# For using Caddy with a config file.
#
# Make sure the ExecStart and ExecReload commands are correct
# for your installation.
#
# See https://caddyserver.com/docs/install for instructions.
#
# WARNING: This service does not use the --resume flag, so if you
# use the API to make changes, they will be overwritten by the
# Caddyfile next time the service is restarted. If you intend to
# use Caddy's API to configure it, add the --resume flag to the
# `caddy run` command or use the caddy-api.service file instead.
[Unit]
Description=Caddy
Documentation=https://caddyserver.com/docs/
After=network.target network-online.target
Requires=network-online.target
[Service]
Type=notify
User=caddy
Group=caddy
ExecStart=/usr/bin/caddy run --environ --config /etc/caddy/Caddyfile
ExecReload=/usr/bin/caddy reload --config /etc/caddy/Caddyfile --force
TimeoutStopSec=5s
LimitNOFILE=1048576
LimitNPROC=512
PrivateTmp=true
ProtectSystem=full
AmbientCapabilities=CAP_NET_BIND_SERVICE
[Install]
WantedBy=multi-user.target
[Service]
User=root
Group=root
常用命令
service caddy reload // 重载配置文件
service caddy restart // 重启服务器
service caddy status // 查看服务器状态
caddy hash-password // 生成认证密码
配置文件
官方提供了两种配置文件格式 Caddyfile
以及 json
Caddyfile
xxx.com {
// 反向代理
reverse_proxy https://www.baidu.com {
header_up Host {upstream_hostport}
header_up X-Forwarded-Host {host}
}
// 前缀匹配, 会去掉 /path1
handle_path /path1 {
reverse_proxy 127.0.0.1:10086
}
// 前缀匹配, 不会去掉 /path2
handle /path2 {
reverse_proxy 127.0.0.1:10010
}
// index 页面
handle /path3 {
root * /root/work/xxx
file_server
}
// 页面增加 auth 认证
// 密码用 caddy hash-password --plaintext 生成
// eg. caddy hash-password --plaintext 123
basicauth * {
admin $2a$14$vDORNSddGldNW09oxJnyCuHhZ1dlh6D074wkyJOz./ZCnhL12GkwO
}
// import, 在 Caddyfile 文件夹同目录有一个 sites 文件夹, 文件夹内容为配置文件, 类似当前的文件
import sites/*
}
json
- 没怎么用过 json, 写个 TODO 吧