如何在Linux上优化GitLab加载速度
硬件是GitLab性能的基础,需根据团队规模调整:
调整Linux内核参数以提升网络和文件处理能力:
/etc/sysctl.conf,添加以下配置并执行sysctl -p生效:net.core.somaxconn = 65535 # 增加TCP连接队列长度
net.ipv4.tcp_max_syn_backlog = 65535 # 增加SYN队列长度
net.ipv4.tcp_tw_reuse = 1 # 允许复用TIME-WAIT连接
net.ipv4.tcp_fin_timeout = 30 # 缩短TIME-WAIT超时时间
net.ipv4.ip_local_port_range = 1024 65535 # 扩大临时端口范围
/etc/security/limits.conf,添加以下内容,允许更多并发连接:* soft nofile 65535
* hard nofile 65535
同时在/etc/pam.d/common-session*中添加session required pam_limits.so,使配置生效。通过调整GitLab自身配置提升处理效率:
/etc/gitlab/gitlab.rb,根据CPU核心数设置worker数量(如Unicorn:unicorn['worker_processes'] = 4;Puma:puma['threads_min'] = 4、puma['threads_max'] = 16),并启用keepalive:unicorn['keepalive'] = true
nginx['keepalive_timeout'] = 65
unicorn['worker_timeout'] = 60),避免长时间挂起占用资源;nginx['client_max_body_size'] = '200m')。sudo gitlab-ctl reconfigure使配置生效。GitLab依赖PostgreSQL,优化数据库配置可显著提升响应速度:
/etc/gitlab/gitlab.rb,设置PostgreSQL共享缓冲区为内存的25%-40%(如gitlab_rails['database_configuration'] = { 'postgresql' => { 'shared_buffers' => '4GB' } }),并调整连接池大小(如postgresql['max_connections'] = 200,建议为并发用户数的2倍);work_mem(每个查询的工作内存,如work_mem = '16MB')和maintenance_work_mem(大型任务内存,如maintenance_work_mem = '512MB'),提升复杂查询(如仓库统计)的处理速度。启用缓存可减少重复计算和数据库查询:
/etc/gitlab/gitlab.rb,配置Redis作为缓存实例(如gitlab_rails['redis_cache_instance'] = "redis://127.0.0.1:6379");gitlab_rails['page_cache_storage_path'] = "/var/cache/gitlab"),提升静态页面(如项目首页)的加载速度;gitlab_rails['cache_store'] = :memory_store, { size: 64.megabytes })。sudo gitlab-ctl reconfigure生效。通过负载均衡和网络优化提升并发处理能力:
upstream gitlab { server 192.168.1.101:8080; server 192.168.1.102:8080; }),将请求分发到多个GitLab实例,提升高并发下的处理能力;net.ipv4.tcp_fastopen = 3),减少TCP握手时间。持续监控和定期维护是保持性能的关键:
log_level = "info",可改为warn或error减少日志量),并设置日志轮转(log_rotate_frequency = 'daily'、log_max_size = '200MB'),避免日志文件过大占用磁盘空间;gitlab-rake gitlab:cleanup:orphan_repository_files清理孤儿文件,使用git gc压缩仓库数据。及时升级到GitLab最新稳定版本,获取官方发布的性能优化和安全补丁,避免因版本问题导致的性能下降。