随州市网站建设_网站建设公司_需求分析_seo优化
2025/12/20 17:03:30 网站建设 项目流程

前言

Caddy是一款用Go编写的现代Web服务器,最大特点是自动HTTPS——只需配置域名,Caddy会自动申请和续期Let’s Encrypt证书。本文将带你快速上手Caddy。

一、Caddy vs Nginx

特性CaddyNginx
配置语法简洁易读相对复杂
自动HTTPS✅ 开箱即用❌ 需certbot
配置热更新✅ API支持❌ 需reload
性能优秀极致
内存占用较高较低
扩展方式插件/编译模块

二、快速安装

2.1 Linux安装

# Ubuntu/Debiansudoaptinstall-y debian-keyring debian-archive-keyring apt-transport-httpscurl-1sLf'https://dl.cloudsmith.io/public/caddy/stable/gpg.key'|sudogpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpgcurl-1sLf'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt'|sudotee/etc/apt/sources.list.d/caddy-stable.listsudoaptupdatesudoaptinstallcaddy# CentOS/RHELyuminstallyum-plugin-copr yum coprenable@caddy/caddy yuminstallcaddy

2.2 Docker部署

# docker-compose.ymlversion:'3.8'services:caddy:image:caddy:2-alpinecontainer_name:caddyports:-"80:80"-"443:443"volumes:-./Caddyfile:/etc/caddy/Caddyfile-./site:/srv-caddy_data:/data-caddy_config:/configrestart:unless-stoppedvolumes:caddy_data:caddy_config:

三、Caddyfile配置

3.1 基础静态站点

# Caddyfile example.com { root * /srv/www file_server } # 就这么简单!Caddy会自动申请HTTPS证书

3.2 反向代理

# 代理到后端服务 api.example.com { reverse_proxy localhost:8080 } # 带路径的代理 example.com { reverse_proxy /api/* localhost:8080 reverse_proxy /ws/* localhost:9000 root * /srv/www file_server }

3.3 负载均衡

example.com { reverse_proxy { to localhost:8001 to localhost:8002 to localhost:8003 lb_policy round_robin health_uri /health health_interval 10s } }

3.4 多站点配置

# 站点1 app1.example.com { reverse_proxy localhost:3001 } # 站点2 app2.example.com { reverse_proxy localhost:3002 } # 站点3 - 静态文件 static.example.com { root * /srv/static file_server browse # 启用目录浏览 }

四、常用功能

4.1 Gzip压缩

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

4.2 访问日志

example.com { log { output file /var/log/caddy/access.log { roll_size 100mb roll_keep 10 } format json } root * /srv/www file_server }

4.3 Basic认证

admin.example.com { basicauth { admin $2a$14$xxxx # caddy hash-password生成 } reverse_proxy localhost:8080 }

生成密码哈希:

caddy hash-password# 输入密码,获取哈希值

4.4 IP限制

internal.example.com { @allowed { remote_ip 192.168.0.0/16 10.0.0.0/8 } handle @allowed { reverse_proxy localhost:8080 } respond "Forbidden" 403 }

4.5 重定向

# HTTP重定向到HTTPS(默认自动) http://example.com { redir https://{host}{uri} permanent } # 路径重定向 example.com { redir /old-path /new-path permanent root * /srv/www file_server }

4.6 请求头设置

example.com { header { # 安全头 Strict-Transport-Security "max-age=31536000; includeSubDomains" X-Content-Type-Options "nosniff" X-Frame-Options "DENY" # 移除Server头 -Server } reverse_proxy localhost:8080 }

五、高级配置

5.1 自定义证书

# 使用自己的证书 example.com { tls /path/to/cert.pem /path/to/key.pem reverse_proxy localhost:8080 } # 内网环境自签名证书 internal.local { tls internal reverse_proxy localhost:8080 }

5.2 WebSocket代理

example.com { reverse_proxy /ws/* localhost:9000 { # WebSocket自动支持 } reverse_proxy localhost:8080 }

5.3 PHP支持

example.com { root * /srv/www php_fastcgi unix//run/php/php8.2-fpm.sock file_server }

5.4 SPA应用

# React/Vue单页应用 example.com { root * /srv/app try_files {path} /index.html file_server }

六、API管理

6.1 启用Admin API

{ admin localhost:2019 } example.com { reverse_proxy localhost:8080 }

6.2 动态配置

# 查看当前配置curlhttp://localhost:2019/config/# 更新配置curl-X POST http://localhost:2019/load\-H"Content-Type: application/json"\-d @config.json# 添加路由curl-X POST http://localhost:2019/config/apps/http/servers/srv0/routes\-H"Content-Type: application/json"\-d'{"handle":[{"handler":"static_response","body":"Hello"}]}'

七、代理内网服务

7.1 场景

当需要代理没有公网IP的内网服务时,可以使用组网软件(如星空组网)将服务器组成虚拟局域网:

# Caddyfile # 代理内网NAS nas.example.com { reverse_proxy 10.26.0.10:5000 # NAS的虚拟内网IP } # 代理内网GitLab git.example.com { reverse_proxy 10.26.0.20:80 # GitLab的虚拟内网IP }

7.2 通配符证书

*.example.com { tls { dns cloudflare {env.CF_API_TOKEN} } @nas host nas.example.com handle @nas { reverse_proxy 10.26.0.10:5000 } @git host git.example.com handle @git { reverse_proxy 10.26.0.20:80 } }

八、监控与日志

8.1 Prometheus指标

{ servers { metrics } } example.com { # ... } # 访问 :2019/metrics 获取指标

8.2 结构化日志

example.com { log { output file /var/log/caddy/access.log format json level INFO } }

九、常见问题

9.1 证书申请失败

# 检查80/443端口是否开放sudolsof-i :80sudolsof-i :443# 检查域名解析digexample.com# 查看Caddy日志journalctl -u caddy -f

9.2 配置验证

# 验证配置文件caddy validate --config /etc/caddy/Caddyfile# 格式化配置caddyfmt--overwrite /etc/caddy/Caddyfile

9.3 重载配置

# systemd方式sudosystemctl reload caddy# API方式caddy reload --config /etc/caddy/Caddyfile

十、生产配置示例

# /etc/caddy/Caddyfile # 全局配置 { email admin@example.com admin localhost:2019 servers { protocol { experimental_http3 } } } # 主站 www.example.com, example.com { encode gzip zstd header { Strict-Transport-Security "max-age=31536000" X-Content-Type-Options "nosniff" X-Frame-Options "SAMEORIGIN" } log { output file /var/log/caddy/www.log { roll_size 100mb roll_keep 5 } } root * /srv/www file_server } # API服务 api.example.com { reverse_proxy { to localhost:8001 to localhost:8002 lb_policy least_conn health_uri /health health_interval 10s } log { output file /var/log/caddy/api.log } }

十一、总结

Caddy是一款现代化的Web服务器,核心优势:

特点说明
自动HTTPS无需手动配置证书
配置简洁Caddyfile语法直观
热更新配置变更无需重启
功能齐全反向代理、负载均衡、压缩等

适用场景

  • 个人项目/小型网站
  • 需要快速部署HTTPS的场景
  • 不想折腾证书配置

不适用场景

  • 对性能极致要求(选Nginx)
  • 需要复杂模块(选OpenResty)

参考资料

  1. Caddy官方文档:https://caddyserver.com/docs/
  2. Caddyfile语法:https://caddyserver.com/docs/caddyfile
  3. Caddy GitHub:https://github.com/caddyserver/caddy

本文首发于CSDN,转载请注明出处。

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询