Caddy 部署文件服务器 📂

📖 目录导航
🌟 简介
本指南介绍如何使用 Caddy 服务器部署高性能的文件下载服务,并配置负载均衡。Caddy 是一个现代化的 web 服务器,以其简单的配置、自动 HTTPS 和高性能而闻名。
服务信息:
🔧 一、Debian/Ubuntu 安装 Caddy
1. 安装 Caddy
1 2 3 4 5 6 7 8
| sudo apt update && sudo apt install caddy -y
caddy version
sudo systemctl status caddy
|
2. 卸载 Caddy
1 2 3 4 5 6
| sudo systemctl stop caddy sudo apt remove caddy -y
sudo rm -rf /etc/caddy /var/lib/caddy
|
3. 创建共享文件夹
1 2 3 4 5 6
| sudo mkdir -p /usr/share/caddy/windows
sudo chown -R www-data:www-data /usr/share/caddy sudo chmod -R 755 /usr/share/caddy
|
4. 文件同步
1 2 3 4
| rsync -avhzp --progress --delete \ "/mnt/ARS2-NFS/阿里云盘/教程文件/Win系统/全自动获取hostIP/windows自动写入hosts(需要管理员权限)_24.8.30_v1.1.zip" \ /usr/share/caddy/windows/
|
🚀 二、Debian 部署文件下载服务
1. 创建文件目录
1 2 3 4 5 6 7
| sudo mkdir -p /mnt/ARS2-NFS/file sudo mkdir -p /mnt/file
sudo chown -R www-data:www-data /mnt/ARS2-NFS/file /mnt/file sudo chmod -R 755 /mnt/ARS2-NFS/file /mnt/file
|
2. 配置 Caddyfile
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
| sudo tee /etc/caddy/Caddyfile > /dev/null <<'EOF'
{ admin off auto_https disable_redirects log { output file /var/log/caddy/access.log { roll_size 100MB roll_keep 10 roll_keep_for 720h } format json } }
:11300 { encode gzip zstd root * /mnt/file/ file_server browse header { X-Content-Type-Options nosniff X-Frame-Options DENY X-XSS-Protection "1; mode=block" } header Content-Type application/octet-stream header Access-Control-Allow-Origin "*" header Cache-Control "no-cache, no-store, must-revalidate" log { output file /var/log/caddy/download.log format json } }
file.meimolihan.eu.org:888 { encode gzip zstd root * /mnt/ARS2-NFS/file/ file_server browse tls { ciphers TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 curves x25519 secp521r1 secp384r1 alpn http/1.1 h2 } header { X-Content-Type-Options nosniff X-Frame-Options DENY X-XSS-Protection "1; mode=block" Strict-Transport-Security "max-age=31536000; includeSubDomains" } header Content-Type application/octet-stream header Access-Control-Allow-Origin "*" header Cache-Control "no-cache, no-store, must-revalidate" log { output file /var/log/caddy/https_download.log format json } } EOF
|
3. 管理 Caddy 服务
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| cat /etc/caddy/Caddyfile
sudo caddy validate --config /etc/caddy/Caddyfile
sudo systemctl restart caddy
sudo systemctl status caddy
sudo systemctl enable caddy
sudo systemctl reload caddy
sudo systemctl stop caddy
sudo tail -f /var/log/caddy/access.log
|
4. 创建测试文件
1 2 3 4 5 6
| echo "这是一个测试文件" | sudo tee /mnt/file/test.txt echo "这是另一个测试文件" | sudo tee /mnt/ARS2-NFS/file/sample.txt
sudo dd if=/dev/zero of=/mnt/file/largefile.bin bs=1M count=100
|
📥 三、文件下载命令
1. 本地路径操作
1 2 3 4 5 6 7 8
| ls -la /mnt/ARS2-NFS/file/
cd /mnt/ARS2-NFS/file/
find /mnt/ARS2-NFS/file/ -type f | head -10
|
2. 文件下载命令
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| curl -# -O https://file.meimolihan.eu.org:888/test.txt
mkdir -p ~/downloads && curl -# -L -o ~/downloads/test.txt https://file.meimolihan.eu.org:888/test.txt
curl -# -L -o ~/downloads/largefile.bin https://file.meimolihan.eu.org:888/largefile.bin
wget -O ~/downloads/test.txt https://file.meimolihan.eu.org:888/test.txt
files=("file1.txt" "file2.txt" "file3.zip") for file in "${files[@]}"; do echo "下载: $file" curl -# -L -o "/tmp/$file" "https://file.meimolihan.eu.org:888/$file" done
|
3. 设置文件权限
1 2 3 4 5 6
| chmod +x ~/downloads/DnsParse.py
find ~/downloads -name "*.sh" -exec chmod +x {} \; find ~/downloads -name "*.py" -exec chmod +x {} \;
|
⚖️ 四、负载均衡配置
1. 负载均衡器配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| sudo tee /etc/caddy/load-balancer.conf > /dev/null <<'EOF'
:888 { reverse_proxy { to 10.10.10.245:11300 10.10.10.247:11300 lb_policy round_robin health_uri /health health_interval 30s health_timeout 5s health_status 200 } tls { ciphers TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 curves x25519 secp521r1 secp384r1 } header { X-Content-Type-Options nosniff X-Frame-Options DENY X-XSS-Protection "1; mode=block" Strict-Transport-Security "max-age=31536000; includeSubDomains" } } EOF
|
2. 后端服务器配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| sudo tee /etc/caddy/backend.conf > /dev/null <<'EOF' :11300 { encode gzip root * /mnt/ARS2-NFS/file/ file_server browse handle /health { respond 200 "Healthy" } header { X-Content-Type-Options nosniff X-Frame-Options DENY } } EOF
|
3. 同步文件内容
1 2 3 4 5
| rsync -avh --delete /mnt/ARS2-NFS/file/ user@10.10.10.247:/mnt/ARS2-NFS/file/
(crontab -l 2>/dev/null; echo "0 2 * * * rsync -avh --delete /mnt/ARS2-NFS/file/ user@10.10.10.247:/mnt/ARS2-NFS/file/") | crontab -
|
🔒 五、安全加固
1. 防火墙配置
1 2 3 4 5 6 7 8 9 10 11 12
| sudo ufw enable
sudo ufw allow 22/tcp sudo ufw allow 80/tcp sudo ufw allow 443/tcp sudo ufw allow 888/tcp sudo ufw allow 11300/tcp
sudo ufw status verbose
|
2. 访问控制
1 2 3 4 5 6 7 8 9 10 11 12 13
| sudo tee /etc/caddy/whitelist.conf > /dev/null <<'EOF'
@whitelist { remote_ip 192.168.1.0/24 10.10.10.0/24 }
handle /admin/* { @blocked not remote_ip 192.168.1.100 10.10.10.50 respond @blocked 403 } EOF
|
3. 速率限制
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| sudo tee /etc/caddy/rate-limit.conf > /dev/null <<'EOF'
@rate_limit { remote_ip 192.168.1.0/24 }
limit_rate @rate_limit 100k
rate_limit { zone rate_limit_zone 10m 10r/s key $remote_ip } EOF
|
🐛 六、故障排除
1. 常见问题解决
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| sudo systemctl status caddy
sudo tail -f /var/log/caddy/access.log sudo journalctl -u caddy -f
curl -I http://localhost:11300 curl -I https://file.meimolihan.eu.org:888
sudo netstat -tulnp | grep :11300 sudo netstat -tulnp | grep :888
namei -l /mnt/ARS2-NFS/file/test.txt
curl -O http://localhost:11300/test.txt
|
2. 性能优化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| sudo tee /etc/caddy/cache.conf > /dev/null <<'EOF'
cache { memory { capacity 256MB ttl 1h } match_path /static/* /downloads/* } EOF
echo "fs.file-max = 100000" | sudo tee -a /etc/sysctl.conf echo "* soft nofile 100000" | sudo tee -a /etc/security/limits.conf echo "* hard nofile 100000" | sudo tee -a /etc/security/limits.conf
|
3. 监控脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| sudo tee /usr/local/bin/monitor-caddy.sh > /dev/null <<'EOF'
LOG_FILE="/var/log/caddy/access.log" ERROR_LOG="/var/log/caddy/error.log"
echo "=== Caddy 服务监控 ===" echo "当前时间: $(date)" echo ""
if systemctl is-active --quiet caddy; then echo "✅ Caddy 服务运行正常" else echo "❌ Caddy 服务未运行" echo "尝试启动服务..." sudo systemctl start caddy sleep 2 if systemctl is-active --quiet caddy; then echo "✅ Caddy 服务已启动" else echo "❌ 启动失败,请检查日志" fi fi
echo "" echo "📊 连接统计:" netstat -an | grep :11300 | awk '{print $6}' | sort | uniq -c netstat -an | grep :888 | awk '{print $6}' | sort | uniq -c
echo "" echo "📈 最近访问:" tail -n 10 $LOG_FILE | awk '{print $1, $4, $7, $9}'
echo "" echo "⚠️ 最近错误:" tail -n 5 $ERROR_LOG 2>/dev/null || echo "无错误日志" EOF
sudo chmod +x /usr/local/bin/monitor-caddy.sh
(crontab -l 2>/dev/null; echo "*/5 * * * * /usr/local/bin/monitor-caddy.sh >> /var/log/caddy/monitor.log") | crontab -
|
通过本指南,您已经成功部署了一个高性能的 Caddy 文件下载服务,并配置了负载均衡。这个解决方案提供了以下优势:
- ✅ 高性能:Caddy 提供优异的静态文件服务性能
- ✅ 自动 HTTPS:无需手动配置 SSL 证书
- ✅ 负载均衡:支持多台服务器分担负载
- ✅ 易于管理:简洁的配置文件格式
- ✅ 安全加固:内置安全功能和访问控制
您现在可以通过 https://file.meimolihan.eu.org:888 访问文件下载服务了!🎉
💡 提示:
- 定期检查日志文件以监控服务状态
- 使用负载均衡时确保后端服务器文件同步
- 考虑设置定期备份重要文件
- 监控磁盘空间使用情况,避免存储不足