温馨提示×

linux gitlab怎么限速

小樊
48
2026-09-04 14:45:36
栏目: 智能运维

在 Linux 上给 GitLab(包括 GitLab CE/EE 自托管)做“限速”,常见有几种层面,取决于你想限什么:


一、限制 Git / HTTP 下载上传速度(最常用)

方案 1:在 Nginx 层限速(推荐)

GitLab 默认使用 Nginx 作为反向代理。

1. 修改 GitLab Nginx 配置

编辑:

vim /etc/gitlab/gitlab.rb

添加或修改:

nginx['enable'] = true

# 限制单连接速度为 1MB/s
nginx['custom_gitlab_server_config'] = "
  limit_rate 1m;
"

或者更细一点(针对 clone / push):

nginx['custom_gitlab_server_config'] = "
  location ~ ^/.*\.git/ {
    limit_rate 1m;
  }
"

2. 重新加载配置

gitlab-ctl reconfigure
gitlab-ctl restart nginx

方案 2:限制 SSH(git over ssh)

SSH 本身不支持“限速”,但可以用 tc(Linux 流量控制)。

示例(限制 eth0 出口 5Mbps):

tc qdisc add dev eth0 root tbf rate 5mbit burst 32kbit latency 400ms

⚠️ 这是全局限速,影响所有流量。


二、限制 API / 请求频率(防止刷接口)

GitLab 自带的 Rate Limit

编辑 gitlab.rb

gitlab_rails['rate_limiter_enabled'] = true

# 每个 IP 每分钟最多 600 次请求
gitlab_rails['rack_attack_git_basic_auth'] = {
  'enabled' => true,
  'ip_whitelist' => [],
  'maxretry' => 10,
  'findtime' => 60,
  'bantime' => 3600
}

然后:

gitlab-ctl reconfigure

三、限制 CI Runner 拉取代码速度(间接限速)

.gitlab-ci.yml 中:

variables:
  GIT_DEPTH: "1"

减少 clone 体积。


四、限制整体服务器带宽(最粗暴)

# 限制整台机器出口 10Mbps
wondershaper eth0 10240 10240

推荐组合 ✅

  • 普通用户限速 → Nginx limit_rate
  • 防刷接口 → Rack::Attack
  • 服务器保护 → 系统级 tc / wondershaper

如果你能说清楚:

  • HTTP 还是 SSH
  • 想限 clone / push / API
  • 单用户还是全局

我可以给你更精确的配置。

0