5.1 HTTPS概述
5.1.1 為什么需要HTTPS
數據加密 :保護傳輸中的敏感數據 身份驗證 :確認服務器身份的真實性 數據完整性 :防止數據在傳輸過程中被篡改 SEO優勢 :搜索引擎優先排名HTTPS網站 瀏覽器要求 :現代瀏覽器對HTTP網站顯示不安全警告 合規要求 :許多行業標準要求使用HTTPS
5.1.2 Caddy的HTTPS特性
自動HTTPS :默認為所有站點啟用HTTPS 自動證書管理 :自動獲取、續期SSL/TLS證書 Let’s Encrypt集成 :內置Let’s Encrypt ACME客戶端 多CA支持 :支持多個證書頒發機構 OCSP裝訂 :自動OCSP響應裝訂 現代TLS :默認使用安全的TLS配置 HTTP/2和HTTP/3 :自動啟用現代HTTP協議
5.2 自動HTTPS配置
5.2.1 基本自動HTTPS
# 最簡配置 - 自動啟用HTTPS
example.com {respond "Hello, HTTPS World!"
}# 多域名自動HTTPS
example.com, www.example.com {file_server
}# 子域名通配符(需要DNS驗證)
*.example.com {respond "Wildcard HTTPS"
}
5.2.2 禁用自動HTTPS
# 全局禁用自動HTTPS
{auto_https off
}example.com {respond "HTTP only"
}# 僅對特定站點禁用
example.com {auto_https offrespond "HTTP only"
}# 禁用重定向但保留HTTPS
example.com {auto_https disable_redirectsrespond "HTTPS without redirect"
}
5.2.3 強制HTTPS重定向
# 自動HTTP到HTTPS重定向(默認行為)
example.com {file_server
}# 自定義重定向
http://example.com {redir https://www.example.com{uri} 301
}https://www.example.com {file_server
}
5.3 證書管理
5.3.1 Let’s Encrypt配置
# 默認Let's Encrypt配置
example.com {file_server
}# 自定義Let's Encrypt配置
example.com {tls {# 指定郵箱email admin@example.com# 使用Let's Encrypt生產環境ca https://acme-v02.api.letsencrypt.org/directory# 或使用測試環境# ca https://acme-staging-v02.api.letsencrypt.org/directory}file_server
}
5.3.2 DNS驗證配置
# DNS驗證(適用于通配符證書)
*.example.com {tls {dns cloudflare {env.CLOUDFLARE_API_TOKEN}}respond "Wildcard certificate via DNS validation"
}# 其他DNS提供商
example.com {tls {# Route53dns route53 {access_key_id {env.AWS_ACCESS_KEY_ID}secret_access_key {env.AWS_SECRET_ACCESS_KEY}region us-east-1}# 或者使用Godaddy# dns godaddy {# api_key {env.GODADDY_API_KEY}# api_secret {env.GODADDY_API_SECRET}# }}file_server
}
5.3.3 自定義證書
# 使用自定義證書
example.com {tls /path/to/cert.pem /path/to/key.pemfile_server
}# 使用證書和中間證書
example.com {tls /path/to/fullchain.pem /path/to/privkey.pemfile_server
}# 客戶端證書認證
example.com {tls /path/to/cert.pem /path/to/key.pem {client_auth {mode require_and_verifytrusted_ca_cert_file /path/to/ca.pem}}file_server
}
5.3.4 證書存儲配置
{# 自定義證書存儲位置storage file_system {root /var/lib/caddy/certificates}# 或使用Redis存儲(集群環境)# storage redis {# host localhost:6379# password {env.REDIS_PASSWORD}# db 0# }# ACME配置acme_ca https://acme-v02.api.letsencrypt.org/directoryemail admin@example.com
}example.com {file_server
}
5.4 TLS配置優化
5.4.1 TLS版本和密碼套件
example.com {tls {# 指定TLS版本protocols tls1.2 tls1.3# 自定義密碼套件(TLS 1.2)ciphers TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305 TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305 TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256# 橢圓曲線配置curves x25519 secp256r1 secp384r1}file_server
}
5.4.2 ALPN和協議協商
{# 全局協議配置servers {protocols h1 h2 h3}
}example.com {tls {# ALPN協議alpn h2 h1}file_server
}
5.4.3 OCSP裝訂
example.com {tls {# 啟用OCSP裝訂(默認啟用)ocsp_stapling on# 自定義OCSP配置ocsp_stapling {responder_timeout 10scache_timeout 1h}}file_server
}
5.5 安全頭部配置
5.5.1 基本安全頭部
example.com {# 基本安全頭部header {# 強制HTTPSStrict-Transport-Security "max-age=31536000; includeSubDomains; preload"# 防止點擊劫持X-Frame-Options "DENY"# 防止MIME類型嗅探X-Content-Type-Options "nosniff"# XSS保護X-XSS-Protection "1; mode=block"# 引用策略