温馨提示×

centos gitlab如何开启https

小樊
42
2026-08-03 16:25:38
栏目: 智能运维

CentOS 上给 GitLab 开启 HTTPS,常见有三种方式,最推荐的是 让 GitLab 自带 Nginx 直接支持 HTTPS。下面按“最常用 → 进阶”给你完整步骤。


一、前提条件

  • 已有 域名(如 gitlab.example.com
  • 域名已解析到服务器 IP
  • 服务器 80 / 443 端口未被占用
  • CentOS 7 / 8(示例以 CentOS 7 为主)

二、方式一(推荐):GitLab 自带 Nginx + HTTPS

1️⃣ 安装 GitLab(如已安装可跳过)

sudo yum install -y curl policycoreutils openssh-server
curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
sudo EXTERNAL_URL="http://gitlab.example.com" yum install -y gitlab-ce

⚠️ 先不用 HTTPS,后面统一改


2️⃣ 准备 SSL 证书

✅ 方式 A:使用 Let’s Encrypt(推荐)

GitLab 自带支持

✅ 方式 B:使用已有证书

假设证书路径:

/etc/gitlab/ssl/gitlab.example.com.crt
/etc/gitlab/ssl/gitlab.example.com.key

3️⃣ 修改 GitLab 配置

sudo vi /etc/gitlab/gitlab.rb

示例(Let’s Encrypt)

external_url "https://gitlab.example.com"

letsencrypt['enable'] = true
letsencrypt['contact_emails'] = ['admin@example.com']
letsencrypt['auto_renew'] = true

示例(已有证书)

external_url "https://gitlab.example.com"

nginx['ssl_certificate'] = "/etc/gitlab/ssl/gitlab.example.com.crt"
nginx['ssl_certificate_key'] = "/etc/gitlab/ssl/gitlab.example.com.key"

4️⃣ 重新配置 GitLab

sudo gitlab-ctl reconfigure

5️⃣ 防火墙放行 HTTPS

sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload

6️⃣ 验证

浏览器访问:

https://gitlab.example.com

三、方式二:GitLab + 外部 Nginx(进阶)

适合你 已有 Nginx / 需要统一代理

GitLab 配置

external_url "https://gitlab.example.com"
nginx['enable'] = false

外部 Nginx 示例

server {
    listen 443 ssl;
    server_name gitlab.example.com;

    ssl_certificate     /etc/nginx/ssl/gitlab.crt;
    ssl_certificate_key /etc/nginx/ssl/gitlab.key;

    location / {
        proxy_pass http://127.0.0.1:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Proto https;
    }
}

然后:

gitlab-ctl reconfigure
systemctl restart nginx

四、常见问题排查

❌ 80 端口被占用

sudo netstat -tulnp | grep :80

❌ Let’s Encrypt 失败

  • 确认域名解析正确
  • 确认 80 / 443 端口对外开放
  • 查看日志:
sudo gitlab-ctl tail

五、证书自动续期(Let’s Encrypt)

GitLab 会自动续期,也可手动:

sudo gitlab-ctl renew-le-certs

六、总结(推荐方案)

最省事

GitLab 自带 Nginx + Let’s Encrypt

企业内网

使用内网 CA 证书 + 自带 Nginx


如果你愿意,可以告诉我:

  • CentOS 版本
  • GitLab 是否已安装
  • 是否用公网域名 / 内网 IP

我可以给你 完全定制化的配置方案

0