You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Cloudflare或Nginx是否会剥离入站请求Body?API请求Body丢失排查

问题分析与解决方案

1. 排查Nginx请求Body缓冲配置

Nginx日志中$request_body显示-,通常意味着请求Body被写入磁盘而非内存,导致变量无法捕获内容,同时可能影响后端服务接收。需在Nginx配置中添加以下参数:

# 在http块或对应location块中添加
client_body_buffer_size 16k;  # 调整缓冲区大小,适配JSON请求
client_max_body_size 100m;    # 设置允许的最大请求Body尺寸,按需调整
proxy_pass_request_body on;   # 强制Nginx转发完整请求Body
proxy_set_header Content-Type $http_content_type;  # 确保Content-Type头正确传递

2. 检查Cloudflare SSL与传输设置

由于你的Nginx仅监听80端口(HTTP),但用户请求走HTTPS,需确认Cloudflare的配置:

  • SSL/TLS模式:若设置为「灵活」模式,Cloudflare将HTTPS转为HTTP转发给Nginx,可能导致JSON Body丢失。建议改为「完全」或「严格」模式(需先给Nginx配置SSL证书,参考下文)。
  • 在线压缩:在Cloudflare「Speed」→「Optimization」中关闭「自动在线压缩」,避免压缩后的Body未被Nginx正确解析;或在Nginx中添加proxy_set_header Accept-Encoding "";禁用压缩传递。
  • WAF规则:临时关闭Cloudflare WAF测试,或查看防火墙日志,确认是否有规则误拦截POST请求Body。

3. 调整Nginx为HTTPS监听(推荐)

既然所有请求走HTTPS,直接让Nginx监听443端口并配置SSL证书(可使用Let's Encrypt免费证书),消除HTTP转发的潜在问题:

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    server_name dev.ru-pirie.com;

    ssl_certificate /path/to/fullchain.pem;
    ssl_certificate_key /path/to/privkey.pem;

    location / {
        access_log  /var/log/nginx/postdata.log  postdata;
        proxy_pass http://192.168.1.74:3000;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        
        # 确保Body传递的关键配置
        client_body_buffer_size 16k;
        client_max_body_size 100m;
        proxy_pass_request_body on;
        proxy_set_header Content-Type $http_content_type;
    }
}

# 80端口重定向到HTTPS
server {
    listen 80;
    listen [::]:80;
    server_name dev.ru-pirie.com;
    return 301 https://$host$request_uri;
}

4. 验证测试

  • 本地测试(绕过Cloudflare):在Nginx服务器上执行命令,确认后端是否能接收Body:
curl -X POST -H "Content-Type: application/json" -d '{"test":"demo_data"}' http://localhost/your-api-endpoint

若此测试正常,说明问题出在Cloudflare配置;若仍异常,需检查后端服务的Body解析逻辑。

  • Cloudflare网络设置:在「Network」→「Origin Server」中关闭「HTTP/2 to Origin」后重新测试。

内容的提问来源于stack exchange,提问作者Rubens Pirie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 23:01:14