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

Git 提交报错 RPC failed HTTP 413 Request Entity Too Large 排障

执行 git push 推送大文件或大批量 commit 时,终端抛出 error: RPC failed; HTTP 413。本文分析 Nginx client_max_body_size、GitLab 限制及 http.postBuffer 优化方案。

在向 GitLab、Gitea、Gerrit 或私有 Git 服务器执行 git push 推送代码时,若本次提交包含了大量历史记录、模型权重文件或大型静态资源,进程会在压缩写入阶段突然中止,终端报错:error: RPC failed; HTTP 413 curl 22 The requested URL returned error: 413 Request Entity Too Large。

故障现象与报错信息

执行推送命令:

git push origin main

终端进度显示传输到一定比例时退出:

Enumerating objects: 1500, done.
Counting objects: 100% (1500/1500), done.
Delta compression using up to 8 threads
Compressing objects: 100% (800/800), done.
Writing objects:  85% (1275/1500), 45.00 MiB | 5.20 MiB/s
error: RPC failed; HTTP 413 curl 22 The requested URL returned error: 413 Request Entity Too Large
fatal: the remote end hung up unexpectedly
fatal: the remote end hung up unexpectedly
Everything up-to-date

本地的分支修改完全无法同步到远端仓库。

根本原因剖析

  1. 反向代理层(Nginx)请求体上限拦截(最主要原因):自建的 Git 平台前方通常部署了 Nginx 反向代理。Nginx 默认的 client_max_body_size 仅为 1m。当 Git 客户端通过 HTTP POST 打包传输超过 1MB 的 pack 文件时,Nginx 直接在入口处拦截并返回 HTTP 413 状态码。
  2. GitLab / Gitea 应用层设置了推送大小限制:在 GitLab 管理后台(Admin Area)的 Account and limit 中,对最大附件与代码推送大小设定了上限(如 100MB)。
  3. 本地客户端 http.postBuffer 偏小:默认情况下,Git 客户端的 HTTP POST 缓冲区较小,导致大包在拆分分块传输(Chunked Transfer)时与服务端的代理协议发生不兼容。

诊断排查步骤

  1. 定位提交中最大的巨型文件: 检查本次未推送的 commit 中究竟包含了多大的文件:
git verify-pack -v .git/objects/pack/*.idx | sort -k 3 -n | tail -5

或者检查工作区大文件:

find . -type f -size +50M
  1. 检查远端 Nginx 错误日志: 登录 Git 服务器的 Nginx 反向代理节点查看 /var/log/nginx/error.log:
2026/01/01 10:00:00 [error] 1234#1234: *567 client intended to send too large body: 52428800 bytes, client: 192.168.1.50, server: gitlab.example.com, request: "POST /mygroup/myproject.git/git-receive-pack HTTP/1.1"

这 100% 确认是由 Nginx 拦截。

解决方案与恢复操作

步骤一:调整反向代理 Nginx 的 client_max_body_size

编辑 Nginx 配置文件(例如 /etc/nginx/conf.d/gitlab.conf):

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

    # 将允许的最大客户端上传体扩大到 500MB 或更高
    client_max_body_size 500m;

    location / {
        proxy_pass http://gitlab_backend;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto https;
        
        # 延长大文件读写超时
        proxy_read_timeout 600s;
        proxy_send_timeout 600s;
    }
}

测试语法并平滑重载:

nginx -t && nginx -s reload

步骤二:调整客户端本地的 http.postBuffer

在本地开发机上,将 Git 的 HTTP 提交缓冲区调大(例如设为 500MB):

git config --global http.postBuffer 524288000

步骤三:调整 GitLab 内部配置(若使用 GitLab 自带 Nginx)

如果是使用官方 Omnibus 安装的 GitLab,编辑 /etc/gitlab/gitlab.rb:

nginx['client_max_body_size'] = '500m'

重新编译配置:

gitlab-ctl reconfigure

步骤四:改用 SSH 协议推送(最简便替代方案)

如果临时无法修改服务端的 Nginx 配置,可以直接将 Git 远程地址切换为 SSH 协议(SSH 协议不经过 HTTP 代理,不受 Nginx 的 body_size 限制):

git remote set-url origin git@git.example.com:mygroup/myproject.git
git push origin main

最佳工程防范建议:使用 Git LFS 管理大文件

代码仓库本身不应存放超过数十兆的二进制资产。应在项目中使用 git-lfs 追踪大文件:

git lfs install
git lfs track "*.zip" "*.tar.gz" "*.bin"
git add .gitattributes

大文件将被存入专门的对象存储服务,代码仓库始终保持轻量极速。