性能优化 #

概述 #

Caddy 本身已经具有很高的性能,但通过合理的配置和系统调优,可以进一步提升性能表现。

text
┌─────────────────────────────────────────────────────────────┐
│                    性能优化层次                              │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│    1. 系统层 - 内核参数、文件描述符                          │
│         ↓                                                   │
│    2. 网络层 - 连接池、超时设置                              │
│         ↓                                                   │
│    3. 应用层 - 压缩、缓存、并发                              │
│         ↓                                                   │
│    4. 存储层 - 磁盘 I/O、证书存储                            │
│                                                             │
└─────────────────────────────────────────────────────────────┘

系统级优化 #

文件描述符限制 #

bash
# 查看当前限制
ulimit -n

# 临时修改
ulimit -n 65535

# 永久修改 - /etc/security/limits.conf
* soft nofile 65535
* hard nofile 65535

内核参数优化 #

bash
# /etc/sysctl.conf 或 /etc/sysctl.d/99-caddy.conf

# 网络优化
net.core.somaxconn = 65535
net.core.netdev_max_backlog = 65535
net.ipv4.tcp_max_syn_backlog = 65535
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 1200
net.ipv4.tcp_keepalive_probes = 5
net.ipv4.tcp_keepalive_intvl = 30
net.ipv4.tcp_max_tw_buckets = 5000
net.ipv4.ip_local_port_range = 1024 65535

# 应用优化
fs.file-max = 2097152
fs.nr_open = 2097152

# 应用配置
sudo sysctl -p

systemd 服务优化 #

ini
# /etc/systemd/system/caddy.service.d/override.conf
[Service]
LimitNOFILE=1048576
LimitNPROC=512
LimitMEMLOCK=infinity
bash
# 应用配置
sudo systemctl daemon-reload
sudo systemctl restart caddy

Caddy 配置优化 #

全局配置 #

caddyfile
{
    # 服务器配置
    servers {
        # 最大头部大小
        max_header_size 16KB
        
        # 协议支持
        protocols h1 h2 h3
        
        # 日志
        logs {
            logger_name {
                output file /var/log/caddy/access.log
            }
        }
    }
    
    # 存储
    storage file_system {
        root /var/lib/caddy
    }
}

HTTP/2 和 HTTP/3 #

caddyfile
{
    servers {
        # 启用 HTTP/2 和 HTTP/3
        protocols h1 h2 h3
    }
}

example.com {
    # HTTP/3 (QUIC) 自动启用
    # 需要开放 UDP 443 端口
    respond "HTTP/3 enabled"
}

连接优化 #

caddyfile
example.com {
    reverse_proxy localhost:3000 {
        transport http {
            # 连接池
            keepalive 90s
            keepalive_idle_conns 100
            keepalive_idle_conns_per_host 20
            
            # 超时
            dial_timeout 5s
            response_header_timeout 10s
        }
    }
}

压缩优化 #

最优压缩配置 #

caddyfile
example.com {
    root * /var/www/html
    file_server
    
    encode {
        # Gzip 中等压缩率
        gzip 6
        
        # Zstd 更好的压缩
        zstd 5
        
        # 最小压缩大小
        minimum_length 256
        
        # 排除已压缩格式
        except image/* video/* audio/* application/pdf application/zip
    }
}

预压缩文件 #

caddyfile
example.com {
    root * /var/www/html
    
    file_server {
        # 启用预压缩
        precompressed
    }
    
    encode gzip zstd
}

缓存优化 #

浏览器缓存 #

caddyfile
example.com {
    root * /var/www/html
    file_server
    
    # 静态资源长期缓存
    @static path *.css *.js *.png *.jpg *.svg *.woff *.woff2
    header @static Cache-Control "public, max-age=31536000, immutable"
    
    # HTML 短期缓存
    @html path *.html
    header @html Cache-Control "public, max-age=3600"
}

代理缓存 #

caddyfile
example.com {
    reverse_proxy localhost:3000
    
    # 允许缓存
    header Cache-Control "public, max-age=3600, s-maxage=86400"
}

并发优化 #

工作进程 #

Caddy 是单进程多协程模型,无需配置工作进程数。

连接池配置 #

caddyfile
example.com {
    reverse_proxy localhost:3000 {
        transport http {
            # 连接池大小
            keepalive_idle_conns 200
            keepalive_idle_conns_per_host 50
            
            # 连接保持时间
            keepalive 120s
            
            # 最大连接数
            max_conns_per_host 0  # 0 表示无限制
        }
    }
}

TLS 优化 #

会话复用 #

caddyfile
example.com {
    tls {
        # TLS 会话复用默认启用
        protocols tls1.2 tls1.3
    }
    
    respond "TLS optimized"
}

OCSP Stapling #

caddyfile
# OCSP Stapling 默认启用
example.com {
    respond "OCSP Stapling enabled"
}

会话票据 #

caddyfile
{
    # 全局 TLS 配置
    servers {
        protocols h1 h2 h3
    }
}

example.com {
    tls {
        protocols tls1.2 tls1.3
    }
}

内存优化 #

减少内存占用 #

caddyfile
{
    # 禁用不必要的功能
    admin off  # 如果不需要 API
    
    # 日志优化
    log {
        output file /var/log/caddy/access.log {
            roll_size 50mb
            roll_keep 5
        }
    }
}

example.com {
    # 最小化配置
    root * /var/www/html
    file_server
}

日志优化 #

caddyfile
example.com {
    # 使用 JSON 格式(更紧凑)
    log {
        output file /var/log/caddy/access.log {
            roll_size 100mb
            roll_keep 10
        }
        format json
    }
    
    # 排除健康检查
    @health path /health
    log @health off
    
    respond "Hello"
}

磁盘 I/O 优化 #

证书存储 #

caddyfile
{
    # 使用 SSD 存储
    storage file_system {
        root /var/lib/caddy  # 确保在 SSD 上
    }
}

日志存储 #

caddyfile
example.com {
    log {
        output file /var/log/caddy/access.log {
            # 较大的轮转大小减少 I/O
            roll_size 100mb
            roll_keep 10
        }
    }
}

监控与调优 #

性能监控 #

bash
# 查看 Caddy 状态
curl localhost:2019/config/

# 查看连接状态
ss -tlnp | grep caddy

# 查看进程状态
ps aux | grep caddy

# 查看内存使用
top -p $(pgrep caddy)

压力测试 #

bash
# 使用 ab 测试
ab -n 10000 -c 100 https://example.com/

# 使用 wrk 测试
wrk -t12 -c400 -d30s https://example.com/

# 使用 hey 测试
hey -n 10000 -c 100 https://example.com/

性能分析 #

bash
# CPU 分析
curl localhost:2019/debug/pprof/profile?seconds=30 > cpu.prof
go tool pprof cpu.prof

# 内存分析
curl localhost:2019/debug/pprof/heap > mem.prof
go tool pprof mem.prof

# 协程分析
curl localhost:2019/debug/pprof/goroutine > goroutine.prof
go tool pprof goroutine.prof

完整优化配置 #

高性能静态站点 #

caddyfile
{
    servers {
        max_header_size 16KB
        protocols h1 h2 h3
    }
}

example.com {
    root * /var/www/html
    file_server {
        precompressed
    }
    
    # 压缩
    encode {
        gzip 6
        zstd 5
        minimum_length 256
    }
    
    # 缓存
    @static path *.css *.js *.png *.jpg *.svg *.woff *.woff2
    header @static Cache-Control "public, max-age=31536000, immutable"
    
    @html path *.html
    header @html Cache-Control "public, max-age=3600"
    
    # 安全头部
    header {
        Strict-Transport-Security "max-age=31536000"
        X-Content-Type-Options "nosniff"
        -Server
    }
    
    # 日志
    log {
        output file /var/log/caddy/access.log {
            roll_size 100mb
            roll_keep 10
        }
        format json
    }
}

高性能 API 网关 #

caddyfile
{
    servers {
        max_header_size 32KB
        protocols h1 h2 h3
    }
}

api.example.com {
    # 连接优化
    reverse_proxy localhost:3000 {
        transport http {
            keepalive 120s
            keepalive_idle_conns 200
            keepalive_idle_conns_per_host 50
            dial_timeout 5s
            response_header_timeout 10s
        }
    }
    
    # 压缩
    encode {
        gzip 4
        zstd 3
    }
    
    # 头部
    header {
        -Server
        X-Response-Time "{duration}"
    }
    
    # 日志
    log {
        output file /var/log/caddy/api.log {
            roll_size 100mb
            roll_keep 14
        }
        format json
    }
}

高并发负载均衡 #

caddyfile
{
    servers {
        max_header_size 16KB
        protocols h1 h2 h3
    }
}

example.com {
    reverse_proxy {
        to server1:3000
        to server2:3000
        to server3:3000
        to server4:3000
        
        lb_policy least_conn
        
        health_uri /health
        health_interval 10s
        
        transport http {
            keepalive 90s
            keepalive_idle_conns 400
            keepalive_idle_conns_per_host 100
            dial_timeout 3s
        }
    }
    
    encode gzip zstd
    
    log {
        output file /var/log/caddy/lb.log {
            roll_size 100mb
        }
        format json
    }
}

性能优化清单 #

系统级 #

  • [ ] 增加文件描述符限制
  • [ ] 优化内核网络参数
  • [ ] 使用 SSD 存储
  • [ ] 确保足够的内存

Caddy 配置 #

  • [ ] 启用 HTTP/2 和 HTTP/3
  • [ ] 配置合理的压缩级别
  • [ ] 设置浏览器缓存
  • [ ] 使用预压缩文件
  • [ ] 优化连接池大小

网络优化 #

  • [ ] 启用 TCP 快速打开
  • [ ] 配置合理的超时
  • [ ] 使用 TLS 会话复用

监控 #

  • [ ] 设置性能监控
  • [ ] 定期进行压力测试
  • [ ] 分析性能瓶颈

下一步 #

现在你已经掌握了性能优化配置,接下来学习 Docker 部署 了解如何容器化部署 Caddy!

最后更新:2026-03-28