温馨提示×

centos gitlab为何报502

小樊
82
2026-10-02 22:37:15
栏目: 智能运维

在 CentOS 上 GitLab 报 502(Bad Gateway),通常不是 GitLab 本身坏了,而是前端(Nginx)连不上后端服务(通常是 Puma / Unicorn)。下面按最常见 → 较少见的顺序给你排查思路。


一、最常见原因(90% 情况)

1️⃣ GitLab 核心服务没起来

sudo gitlab-ctl status

重点看这几个:

  • puma(或老版本 unicorn)
  • sidekiq
  • nginx

如果 puma 是 down,Nginx 就会 502。

✅ 启动:

sudo gitlab-ctl start

2️⃣ 内存不足(非常常见)

GitLab 官方建议 至少 4GB 内存。

查看内存:

free -h

如果内存爆了,puma 会被 OOM 杀掉。

✅ 解决:

  • 加内存
  • 或限制 GitLab 内存占用(下面有)

3️⃣ Puma / Unicorn 启动慢(刚重启)

刚执行过:

gitlab-ctl reconfigure
gitlab-ctl restart

可能需要 1–3 分钟才能恢复。

✅ 等一会儿再访问


二、Nginx 相关

4️⃣ Nginx 连不上 Puma socket

查看日志:

sudo gitlab-ctl tail nginx

常见错误:

connect() to unix:/var/opt/gitlab/gitlab-rails/sockets/gitlab.socket failed

✅ 通常是 puma 没启动或 socket 路径不对


三、端口 / 配置问题

5️⃣ 端口被占用(如 8080)

GitLab 默认:

  • Nginx:80
  • Puma:8080(内部)

检查:

netstat -tunlp | grep 8080

✅ 修改配置:

sudo vi /etc/gitlab/gitlab.rb
puma['port'] = 8081

然后:

sudo gitlab-ctl reconfigure

四、SELinux(CentOS 特有)

6️⃣ SELinux 阻止 Nginx

getenforce

如果是 Enforcing

临时关闭测试:

setenforce 0

✅ 如果 502 消失,说明是 SELinux 问题

永久解决(不推荐直接关):

setsebool -P httpd_can_network_connect 1

五、磁盘 / 权限问题

7️⃣ 磁盘满了

df -h

GitLab 对磁盘非常敏感。


8️⃣ 权限错误

sudo gitlab-ctl reconfigure

六、快速自查清单(建议照做)

# 1. 看状态
sudo gitlab-ctl status

# 2. 看日志
sudo gitlab-ctl tail

# 3. 看内存
free -h

# 4. 看磁盘
df -h

七、如果你愿意,我可以更精准帮你

你可以贴出以下信息之一:

  • gitlab-ctl status 输出
  • /var/log/gitlab/nginx/error.log
  • /var/log/gitlab/gitlab-rails/production.log

我可以直接告诉你是哪一行配置或资源导致的 502。

0 踩