DEBUG.WIKI
阿里云;从建站到云上业务;云服务器 · 数据库 · 对象存储;查看产品 →
PanstarCloud:部署应用,接入 AI,云服务器与 AI 网关
IPPeak:从目标地区访问网页,住宅代理、SEO 监测、广告验证

解决 Git 报错 RPC failed curl 22 The requested URL returned error 413

分析 Git push 提交包含大文件时被反向代理拦截报 HTTP 413 Request Entity Too Large 根因,提供 client_max_body_size 与 postBuffer 配置步骤。

问题现象

在向 GitLab、Gitea 或自建 Git 服务器执行 git push 时,数据上传到中途被强行切断,终端输出:

Enumerating objects: 150, done.
Counting objects: 100% (150/150), done.
Delta compression using up to 8 threads
Compressing objects: 100% (120/120), done.
Writing objects:  85% (128/150), 45.00 MiB | 2.10 MiB/s
error: RPC failed; curl 22 The requested URL returned error: 413 Request Entity Too Large
fatal: the remote end hung up unexpectedly

触发根因

  1. 反向代理层(Nginx / Ingress)请求体尺寸限制:Git HTTP 请求通过 Nginx 转发,Nginx 默认的 client_max_body_size 仅为 1MB。当推送包含大文件或庞大历史提交树时,Nginx 在接收到超出体积的数据后直接返回 HTTP 413。
  2. 本地 Git 客户端缓冲区溢出:本地 http.postBuffer 较小,导致大包发送机制失败。

诊断排查步骤

  1. 查看 Git 服务器前端反向代理(如 Nginx)的访问日志:
sudo tail -n 20 /var/log/nginx/gitlab_error.log

确认出现 client intended to send too large body。

解决步骤与操作命令

步骤一:调大前端 Nginx 的 client_max_body_size

编辑反向代理配置文件(例如 /etc/nginx/conf.d/git.conf):

server {
    listen 443 ssl;
    server_name git.example.com;

    # 允许最大推送单个包体为 500MB
    client_max_body_size 500M;

    location / {
        proxy_pass http://gitlab_upstream;
        proxy_request_buffering off;
        proxy_read_timeout 600s;
        proxy_set_header Host $host;
    }
}

重载 Nginx:

sudo nginx -t && sudo nginx -s reload

步骤二:调整本地 Git 客户端的 postBuffer 缓冲区

在开发机器上执行配置命令:

git config --global http.postBuffer 524288000

步骤三:排查并使用 Git LFS 管理超大文件

如果仓库中包含数十 MB 的二进制文件(如 .zip、.onnx、.mp4),建议将其转由 Git LFS 管理以减轻仓库体积负担:

git lfs install
git lfs track "*.zip"
git add .gitattributes

步骤四:重新执行推送

git push origin main

数据包顺利完成传输并合并入库。