温馨提示×

linux gitlab如何开启https

小樊
35
2026-08-13 08:22:47
栏目: 智能运维

在 Linux 上给 GitLab 开启 HTTPS,常见有三种方式,按使用频率从高到低说明。以下以 GitLab CE(Omnibus 安装) 为例(最常见)。


一、使用 Let’s Encrypt 自动开启 HTTPS(推荐)

前提条件

  • 域名已解析到服务器公网 IP
  • 80 / 443 端口未被占用
  • GitLab 是 Omnibus 安装方式

1️⃣ 修改 GitLab 配置

sudo vim /etc/gitlab/gitlab.rb

关键配置:

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

letsencrypt['enable'] = true
letsencrypt['contact_emails'] = ['your@email.com']
letsencrypt['auto_renew'] = true

⚠️ 注意:external_url 必须是 https


2️⃣ 重新配置 GitLab

sudo gitlab-ctl reconfigure

GitLab 会自动:

  • 申请 Let’s Encrypt 证书
  • 配置 Nginx
  • 开启 443 端口

3️⃣ 验证

浏览器访问:

https://gitlab.example.com

二、使用自定义 SSL 证书(企业 / 内网常用)

1️⃣ 准备证书

假设你有:

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

⚠️ 文件名必须和域名一致


2️⃣ 修改配置

sudo vim /etc/gitlab/gitlab.rb
external_url "https://gitlab.example.com"

nginx['enable'] = true
nginx['redirect_http_to_https'] = true

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

3️⃣ 重新配置

sudo gitlab-ctl reconfigure

三、已有 HTTP GitLab,强制跳转 HTTPS

external_url "https://gitlab.example.com"
nginx['redirect_http_to_https'] = true

然后:

sudo gitlab-ctl reconfigure

四、防火墙 & 端口检查

开放端口

sudo ufw allow 443
sudo ufw allow 80

或(CentOS / RHEL):

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

五、常见问题排查

❌ 证书申请失败

  • 域名未解析
  • 80 端口被占用
  • 服务器在内网 / NAT

✅ 解决:

sudo netstat -tulnp | grep :80

❌ 浏览器提示证书不安全

  • 自签证书
  • 证书域名不匹配

✅ 解决:使用 Let’s Encrypt 或更换正规 CA


六、查看证书是否生效

openssl s_client -connect gitlab.example.com:443 | grep issuer

七、Docker 版 GitLab(简要)

docker run -d \
  -p 443:443 -p 80:80 \
  -e GITLAB_HTTPS=true \
  -e GITLAB_HOST=gitlab.example.com \
  gitlab/gitlab-ce

总结(推荐方案)

场景 推荐方式
公网服务器 ✅ Let’s Encrypt
内网 / 企业 ✅ 自定义证书
快速测试 HTTP(不推荐)

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

  • ✅ GitLab 安装方式(Omnibus / Docker / 源码)
  • ✅ 是否是公网 IP
  • ✅ 使用的 Linux 发行版

我可以给你 精确到命令级别 的配置方案。

0