背景
在服务器上跑一个带 HTTPS 的私有网盘,最省事的方案就是 Caddy——单二进制、自动 TLS、配置极简。但实际部署过程中踩了几个坑:进程随终端消亡、curl 无输出误判失败、反代自身端口报 502、目录浏览不生效。本文按实际排障顺序记录完整过程。
文中
example.com、203.0.113.10、myserver均为示例占位,请替换为你自己的域名 / IP / 主机名。
一、进程随终端消亡
前台执行 caddy run --config Caddyfile 启动后,日志显示一切正常:
{"level":"info","msg":"server running","name":"srv0","protocols":["h1","h2","h3"]}
{"level":"info","msg":"serving initial configuration"}
但关掉 SSH 后服务就没了。诊断命令:
curl -k https://127.0.0.1:8443 # 无输出
lsof -i:8443 # 无输出
原因:前台阻塞进程随终端会话一起被 SIGHUP 杀掉。
规范做法是注册 systemd 服务:
[Unit]
Description=Caddy File Server
After=network.target
[Service]
Type=simple
WorkingDirectory=/home/myuser/file-server
ExecStart=/usr/bin/caddy run --config /home/myuser/file-server/Caddyfile
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
WorkingDirectory 必须指定为证书所在目录,否则 tls 指令里的相对路径找不到文件。
sudo systemctl daemon-reload
sudo systemctl start file-server
sudo systemctl enable file-server
临时测试可用 nohup sudo caddy run --config Caddyfile > caddy.log 2>&1 &。
排查提示:用 sudo 启动的进程属于 root,普通用户执行 lsof -i:8443 看不到,必须加 sudo。
二、curl 无输出 ≠ 服务失败
服务确认存活后:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
caddy 2654492 root 7u IPv6 57829980 0t0 TCP *:8443 (LISTEN)
caddy 2654492 root 8u IPv6 57829982 0t0 UDP *:8443
TCP 与 UDP(HTTP/3)均在监听,但 curl -k https://127.0.0.1:8443 依然一片空白。
原因:curl 默认只打印响应 body。根目录既无 index.html 又未开启 browse 时,Caddy 返回 404 且 body 为空,因此无任何输出。
加 -i 打印响应头即可看到真相:
curl -k -i https://127.0.0.1:8443
# HTTP/1.1 404 Not Found
能看到状态码行,说明 TLS 握手与服务转发完全正常。三种验证方式:
外网访问若本机通而公网不通,检查云安全组是否放行 TCP 8443(启用 HTTP/3 还需放行 UDP 8443)。
三、端口共存与进程识别
最终端口状态:
# 443
hermes 1086852 myuser 19u IPv4 TCP myserver:54368->203.0.113.10:https (ESTABLISHED)
caddy 2411325 caddy 3u IPv6 TCP *:https (LISTEN)
# 80
caddy 2411325 caddy 6u IPv6 TCP *:http (LISTEN)
hermes 与 caddy 并非端口冲突:
(LISTEN):主动监听,等待外部连接(ESTABLISHED):作为客户端向外发起的连接
此时架构为双实例分工:系统级 Caddy 管理 80/443,独立实例管理 8443 文件服务。
注意:不要让两个进程争抢同一端口,后启动者会因 bind: address already in use 直接退出。
四、反向代理 HTTPS 后端
后端 3000 端口自身运行 HTTPS 时,reverse_proxy localhost:3000 不可用——Caddy 会用明文 HTTP 连接只说 TLS 的端口。
api.example.com {
reverse_proxy https://localhost:3000 {
transport http {
tls_insecure_skip_verify
}
header_up X-Forwarded-Proto https
header_up X-Forwarded-Host {host}
}
}
三个关键点:
若后端证书签给特定内网域名,用 tls_server_name 指定 SNI。
高频报错:transport 是 reverse_proxy 的子指令,必须写在其 { } 内部,否则报 unrecognized directive: transport。
排查捷径:
curl -k https://localhost:3000 # 有内容 → 后端确为 HTTPS
curl -v http://localhost:3000 # 也有内容 → 直接走 http,免去证书校验
五、反代本机 8443
目标是 auto_https off + 手动证书的 HTTPS 站点时:
file.example.com {
reverse_proxy https://localhost:8443 {
transport http {
tls
tls_insecure_skip_verify
}
header_up X-Forwarded-Proto https
header_up Host {host}
}
}
证书签给 example.com 而连接目标是 localhost,域名不匹配,必须跳过校验。
但更推荐直接合并配置,避免 Caddy 反代 Caddy 造成的多余 TLS 握手与端口占用:
file.example.com {
root * /home/myuser/file-server
file_server browse
encode zstd gzip
}
六、browse 不生效:index 优先级
配置了 file_server browse 却只显示 index.html 内容,这是设计行为而非 bug。
官方定义:
browse enables file listings for requests to directories that do not have an index file
Caddy 默认查找 index.html 和 index.txt,找到即直接返回,browse 被短路:
请求 / → 目录 → 找 index.html → 命中 → 返回(browse 不执行)
四种解法:
# A. 移除索引文件(最干净)
mv ~index.html ~index.html.bak
# B. 强制始终列目录
file_server {
browse
index off
}
# C. 改索引文件名,保留首页
file_server {
browse
index home.html
}
# D. 隐藏 index.html 使其回落到 browse
file_server {
browse
hide index.html
}
hide 匹配的是文件系统路径,只写文件名则匹配任意层级同名文件。
目录排序优化,让文件夹置顶:
file_server {
browse {
sort namedirfirst desc
}
index off
}
sort 支持 name / namedirfirst / size / time,配合 asc / desc。
七、最终配置
{
auto_https off
}
https://:8443 {
tls /home/myuser/ssl-certs/example.com_bundle.pem /home/myuser/ssl-certs/example.com.key
root * /home/myuser/file-server
file_server {
browse {
sort namedirfirst desc
}
index off
}
encode zstd gzip
# 密码保护:caddy hash-password --plaintext 你的密码
# basicauth {
# admin $2a$14$你的哈希值
# }
}
每次改动后:
sudo caddy fmt --overwrite Caddyfile
sudo caddy validate --config Caddyfile
sudo systemctl restart file-server
评论区