Skip to content

🔒 请求限制配置指南

📋 概述

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

🛡️ 推荐的配置

🔧 基础配置

yaml
gateway:
  # URL级别的HTTP配置 - 用于控制客户端请求到网关的超时和大小限制
  http:
    enabled: true                    # 启用URL级别的HTTP配置
    rules:
      # 默认接口配置 - 普通API请求
      - name: "default-api"
        urls:
          - "/**"                   # 匹配所有路径
        max-header-bytes: "1MB"     # 最大请求头大小 - 防止请求头攻击
        max-body-bytes: "5MB"       # 最大请求体大小 - 平衡安全性和可用性
        write-timeout: "30s"        # 写入响应超时时间 - 快速释放连接
        read-timeout: "30s"         # 读取整个请求超时时间 - 防止慢速读取攻击

🎯 安全配置详解

🚫 防护的主要攻击类型

1. 慢速攻击 (Slowloris Attack)

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

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

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

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

  • 攻击原理: 建立大量空闲连接,耗尽服务器连接池
  • 防护配置:
    • write-timeout: "30s" - 防止响应写入时间过长
    • read-timeout: "30s" - 防止请求读取时间过长

📏 配置值选择原则

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

yaml
gateway:
  http:
    enabled: true
    rules:
      - name: "secure-api"
        urls:
          - "/**"
        max-header-bytes: "512KB"   # 更严格的请求头限制
        max-body-bytes: "1MB"       # 严格限制请求体大小
        write-timeout: "15s"        # 更短的写入超时
        read-timeout: "15s"         # 更短的请求超时

适用场景:

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

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

yaml
gateway:
  http:
    enabled: true
    rules:
      - name: "balanced-api"
        urls:
          - "/**"
        max-header-bytes: "1MB"     # 平衡的请求头限制
        max-body-bytes: "5MB"       # 支持一般文件上传
        write-timeout: "30s"        # 合理的写入超时
        read-timeout: "30s"         # 合理的请求超时

适用场景:

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

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

yaml
gateway:
  http:
    enabled: true
    rules:
      - name: "performance-api"
        urls:
          - "/**"
        max-header-bytes: "2MB"     # 较宽松的请求头限制
        max-body-bytes: "50MB"      # 支持大文件上传
        write-timeout: "60s"        # 较长的写入超时
        read-timeout: "60s"         # 较长的请求超时

适用场景:

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

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

🌐 API网关场景

yaml
gateway:
  http:
    enabled: true
    rules:
      - name: "api-gateway"
        urls:
          - "/**"
        max-header-bytes: "1MB"     # API请求头通常较小
        max-body-bytes: "5MB"       # 支持JSON数据和小文件
        write-timeout: "30s"        # 快速响应
        read-timeout: "30s"         # 防止慢速攻击

📁 文件上传服务

yaml
gateway:
  http:
    enabled: true
    rules:
      - name: "file-upload"
        urls:
          - "/upload/**"
          - "/file/**"
        max-header-bytes: "2MB"     # 文件上传可能需要更大的请求头
        max-body-bytes: "100MB"     # 支持大文件上传
        write-timeout: "300s"       # 大文件响应需要时间
        read-timeout: "300s"        # 大文件上传需要时间

🔐 高安全API

yaml
gateway:
  http:
    enabled: true
    rules:
      - name: "secure-api"
        urls:
          - "/secure/**"
          - "/admin/**"
        max-header-bytes: "512KB"   # 严格限制
        max-body-bytes: "1MB"       # 严格限制
        write-timeout: "15s"        # 快速响应
        read-timeout: "15s"         # 快速处理

⚠️ 安全注意事项

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
gateway:
  http:
    enabled: true
    rules:
      - name: "dev-api"
        urls:
          - "/**"
        max-header-bytes: "2MB"     # 开发环境 - 宽松配置便于调试
        max-body-bytes: "50MB"      # 支持大文件调试
        read-timeout: "60s"         # 较长超时便于调试
        write-timeout: "60s"        # 较长超时便于调试

测试环境

yaml
gateway:
  http:
    enabled: true
    rules:
      - name: "test-api"
        urls:
          - "/**"
        max-header-bytes: "1MB"     # 测试环境 - 接近生产的配置
        max-body-bytes: "10MB"      # 中等大小限制
        read-timeout: "45s"         # 中等超时时间
        write-timeout: "45s"        # 中等超时时间

生产环境

yaml
gateway:
  http:
    enabled: true
    rules:
      - name: "prod-api"
        urls:
          - "/**"
        max-header-bytes: "1MB"     # 生产环境 - 安全优先配置
        max-body-bytes: "5MB"       # 严格大小限制
        read-timeout: "30s"         # 较短超时时间
        write-timeout: "30s"        # 较短超时时间

📝 配置检查清单

  • [ ] 请求头大小限制是否合理 (推荐: 512KB-2MB)
  • [ ] 请求体大小限制是否满足业务需求 (推荐: 1MB-50MB)
  • [ ] 读取超时是否能防止慢速攻击 (推荐: 15s-60s)
  • [ ] 写入超时是否能及时释放连接 (推荐: 15s-300s)
  • [ ] URL匹配规则是否覆盖所有需要的路径
  • [ ] 是否有监控和告警机制
  • [ ] 是否定期回顾和调整配置