Skip to content

🔒 请求配置指南

📋 概述

本文档提供了网关HTTP服务器的安全配置建议,帮助你在保证服务可用性的同时,有效防护各种网络攻击。

🛡️ 推荐的配置

🔧 基础配置

yaml
server:
  port: 8080
  name: gateway
  http:
    max-header-bytes: "1MB"        # 最大请求头大小 - 防止请求头攻击
    max-body-bytes: "5MB"          # 最大请求体大小 - 平衡安全性和可用性
    read-header-timeout: "15s"     # 读取请求头超时时间 - 防止慢速攻击
    write-timeout: "30s"           # 写入响应超时时间 - 快速释放连接
    read-timeout: "30s"            # 读取整个请求超时时间 - 防止慢速读取攻击
    idle-timeout: "60s"            # 空闲连接超时时间 - 及时释放资源

🎯 安全配置详解

🚫 防护的主要攻击类型

1. 慢速攻击 (Slowloris Attack)

  • 攻击原理: 攻击者缓慢发送HTTP请求头,占用服务器连接资源
  • 防护配置:
    • read-header-timeout: "15s" - 限制读取请求头时间
    • read-timeout: "30s" - 限制整个请求读取时间

2. 大请求攻击 (Large Request Attack)

  • 攻击原理: 发送超大请求头或请求体,消耗服务器内存
  • 防护配置:
    • max-header-bytes: "1MB" - 限制请求头大小
    • max-body-bytes: "5MB" - 限制请求体大小

3. 连接耗尽攻击 (Connection Exhaustion)

  • 攻击原理: 建立大量空闲连接,耗尽服务器连接池
  • 防护配置:
    • idle-timeout: "60s" - 快速释放空闲连接
    • write-timeout: "30s" - 防止响应写入时间过长

📏 配置值选择原则

🔒 安全优先配置 (高安全环境)

yaml
server:
  http:
    max-header-bytes: "512KB"      # 更严格的请求头限制
    max-body-bytes: "1MB"          # 严格限制请求体大小
    read-header-timeout: "10s"     # 更短的读取超时
    write-timeout: "15s"           # 更短的写入超时
    read-timeout: "15s"            # 更短的请求超时
    idle-timeout: "30s"            # 更短的空闲超时

适用场景:

  • 公网暴露的API网关
  • 高风险环境
  • 主要处理小数据量的API请求

⚖️ 平衡配置 (推荐默认)

yaml
server:
  http:
    max-header-bytes: "1MB"        # 平衡的请求头限制
    max-body-bytes: "5MB"          # 支持一般文件上传
    read-header-timeout: "15s"     # 合理的读取超时
    write-timeout: "30s"           # 合理的写入超时
    read-timeout: "30s"            # 合理的请求超时
    idle-timeout: "60s"            # 合理的空闲超时

适用场景:

  • 大多数生产环境
  • 需要处理文件上传的应用
  • 内网和公网混合环境

🚀 性能优先配置 (内网环境)

yaml
server:
  http:
    max-header-bytes: "2MB"        # 较宽松的请求头限制
    max-body-bytes: "50MB"         # 支持大文件上传
    read-header-timeout: "30s"     # 较长的读取超时
    write-timeout: "60s"           # 较长的写入超时
    read-timeout: "60s"            # 较长的请求超时
    idle-timeout: "120s"           # 较长的空闲超时

适用场景:

  • 内网环境
  • 需要处理大文件的应用
  • 网络条件较差的环境

📊 不同业务场景的配置建议

🌐 API网关场景

yaml
server:
  http:
    max-header-bytes: "1MB"        # API请求头通常较小
    max-body-bytes: "5MB"          # 支持JSON数据和小文件
    read-header-timeout: "15s"     # 快速处理API请求
    write-timeout: "30s"           # 快速响应
    read-timeout: "30s"            # 防止慢速攻击
    idle-timeout: "60s"            # 及时释放连接

📁 文件上传服务

yaml
server:
  http:
    max-header-bytes: "1MB"        # 请求头大小正常
    max-body-bytes: "100MB"        # 支持大文件上传
    read-header-timeout: "15s"     # 请求头处理要快
    write-timeout: "300s"          # 大文件响应需要时间
    read-timeout: "300s"           # 大文件上传需要时间
    idle-timeout: "60s"            # 上传完成后及时释放

🔐 高安全API

yaml
server:
  http:
    max-header-bytes: "512KB"      # 严格限制
    max-body-bytes: "1MB"          # 严格限制
    read-header-timeout: "10s"     # 快速超时
    write-timeout: "15s"           # 快速响应
    read-timeout: "15s"            # 快速处理
    idle-timeout: "30s"            # 快速释放

⚠️ 安全注意事项

1. 配置过于严格的风险

  • 问题: 可能导致正常请求被拒绝
  • 建议: 根据实际业务需求调整,逐步收紧配置

2. 配置过于宽松的风险

  • 问题: 容易受到DoS攻击
  • 建议: 从严格配置开始,根据需要适当放宽

3. 监控和调优

  • 监控指标: 超时错误率、连接数、内存使用率
  • 调优策略: 根据监控数据动态调整配置

🔍 攻击检测和日志

关键日志模式

慢速攻击检测

net/http: request canceled (Client.Timeout exceeded while reading body)

大请求攻击检测

http: request header too large
http: request body too large

连接耗尽检测

net/http: request canceled (Client.Timeout exceeded while awaiting headers)

📈 性能影响分析

配置对性能的影响

配置项严格配置影响宽松配置影响
max-header-bytes减少内存占用增加内存风险
max-body-bytes限制上传能力增加内存风险
read-timeout可能中断慢请求占用连接时间长
write-timeout可能中断慢响应占用连接时间长
idle-timeout频繁建立连接占用连接资源

🛠️ 配置测试

测试请求头大小限制

bash
# 测试1MB请求头限制
curl -H "X-Large-Header: $(python3 -c 'print("a"*1048576)')" http://localhost:8080/test

测试请求体大小限制

bash
# 测试5MB请求体限制
dd if=/dev/zero bs=1M count=6 | curl -X POST --data-binary @- http://localhost:8080/test

测试超时配置

bash
# 测试读取超时
curl --limit-rate 1 -d "$(python3 -c 'print("a"*1000000)')" http://localhost:8080/test

🔧 动态配置调整

根据环境调整

开发环境

yaml
# 开发环境 - 宽松配置便于调试
max-body-bytes: "50MB"
read-timeout: "60s"
write-timeout: "60s"

测试环境

yaml
# 测试环境 - 接近生产的配置
max-body-bytes: "10MB"
read-timeout: "45s"
write-timeout: "45s"

生产环境

yaml
# 生产环境 - 安全优先配置
max-body-bytes: "5MB"
read-timeout: "30s"
write-timeout: "30s"

📝 配置检查清单

  • [ ] 请求头大小限制是否合理 (推荐: 512KB-2MB)
  • [ ] 请求体大小限制是否满足业务需求 (推荐: 1MB-50MB)
  • [ ] 读取超时是否能防止慢速攻击 (推荐: 10s-30s)
  • [ ] 写入超时是否能及时释放连接 (推荐: 15s-60s)
  • [ ] 空闲超时是否能有效回收连接 (推荐: 30s-120s)
  • [ ] 是否有监控和告警机制
  • [ ] 是否定期回顾和调整配置