第5章 HTTPS与安全配置

发布于:2025-09-14 ⋅ 阅读:(14) ⋅ 点赞:(0)

5.1 HTTPS概述

5.1.1 为什么需要HTTPS

  1. 数据加密:保护传输中的敏感数据
  2. 身份验证:确认服务器身份的真实性
  3. 数据完整性:防止数据在传输过程中被篡改
  4. SEO优势:搜索引擎优先排名HTTPS网站
  5. 浏览器要求:现代浏览器对HTTP网站显示不安全警告
  6. 合规要求:许多行业标准要求使用HTTPS

5.1.2 Caddy的HTTPS特性

  1. 自动HTTPS:默认为所有站点启用HTTPS
  2. 自动证书管理:自动获取、续期SSL/TLS证书
  3. Let’s Encrypt集成:内置Let’s Encrypt ACME客户端
  4. 多CA支持:支持多个证书颁发机构
  5. OCSP装订:自动OCSP响应装订
  6. 现代TLS:默认使用安全的TLS配置
  7. 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 off
    respond "HTTP only"
}

# 禁用重定向但保留HTTPS
example.com {
    auto_https disable_redirects
    respond "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 {
        # Route53
        dns 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.pem
    file_server
}

# 使用证书和中间证书
example.com {
    tls /path/to/fullchain.pem /path/to/privkey.pem
    file_server
}

# 客户端证书认证
example.com {
    tls /path/to/cert.pem /path/to/key.pem {
        client_auth {
            mode require_and_verify
            trusted_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/directory
    email 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 10s
            cache_timeout 1h
        }
    }
    
    file_server
}

5.5 安全头部配置

5.5.1 基本安全头部

example.com {
    # 基本安全头部
    header {
        # 强制HTTPS
        Strict-Transport-Security "max-age=31536000; includeSubDomains; preload"
        
        # 防止点击劫持
        X-Frame-Options "DENY"
        
        # 防止MIME类型嗅探
        X-Content-Type-Options "nosniff"
        
        # XSS保护
        X-XSS-Protection "1; mode=block"
        
        # 引用策略