在 Linux 上优化 GitLab,可以从系统资源、GitLab 配置、数据库、存储、网络和安全几个方面入手。下面按“实用优先”的方式给你一套优化建议。
GitLab 比较吃内存(尤其 Sidekiq、PostgreSQL、Puma)。
free -h
swapon --show
如果内存 ≤ 8GB,建议开 2–4GB swap:fallocate -l 4G /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
GitLab 会大量使用文件句柄:
ulimit -n
建议 ≥ 65535
在 /etc/security/limits.conf:
* soft nofile 65535
* hard nofile 65535
cat >> /etc/sysctl.conf <<EOF
net.core.somaxconn = 1024
net.ipv4.tcp_max_syn_backlog = 8192
vm.swappiness = 10
EOF
sysctl -p
配置文件:
/etc/gitlab/gitlab.rb
puma['worker_processes'] = 4
puma['min_threads'] = 4
puma['max_threads'] = 16
规则:
sidekiq['concurrency'] = 20
根据 CPU 和任务量调整(10–30 常见)
prometheus_monitoring['enable'] = false
grafana['enable'] = false
(非监控场景可关)
postgresql['shared_buffers'] = "2GB"
postgresql['work_mem'] = "16MB"
postgresql['maintenance_work_mem'] = "512MB"
postgresql['max_connections'] = 200
经验:
gitlab-psql -c "VACUUM ANALYZE;"
可加入定时任务(每周)
GitLab 对磁盘 IO 敏感,尤其是:
git_data_dirs({
"default" => { "path" => "/data/gitlab/git-data" }
})
gitlab-rake gitlab:cleanup:project_uploads
gitlab-rake gitlab:cleanup:orphan_job_artifacts
gitlab_rails['ci_pipeline_creation_step_timeout'] = 20
不要在 GitLab 本机跑 Runner(资源争抢)
设置保留时间:
gitlab_rails['artifacts_object_store_enabled'] = true
GitLab 自带 Nginx,可启用:
nginx['worker_processes'] = 4
nginx['worker_connections'] = 10240
nginx['ssl_protocols'] = "TLSv1.2 TLSv1.3"
top
htop
gitlab-ctl status
gitlab-ctl tail nginx
gitlab-ctl tail puma
| 问题 | 原因 | 解决 |
|---|---|---|
| 页面卡 | 内存不足 | 加内存 / 关服务 |
| Git 慢 | 磁盘 IO | 换 SSD |
| 后台任务堆积 | Sidekiq 少 | 提高并发 |
| DB 慢 | PG 未优化 | 调参数 |
如果你愿意,可以告诉我:
我可以直接给你一份定制版 gitlab.rb 优化配置。