温馨提示×

ubuntu gitlab怎么配ssl

小樊
39
2026-10-07 18:07:14
栏目: 云计算

在 Ubuntu 上给 GitLab 配置 SSL(HTTPS),常见有两种方式:

  1. GitLab 自带 Nginx(推荐)
  2. 使用外部 Nginx / Apache 反代

下面以 GitLab Omnibus(官方包)最常见方式 为例说明。


一、准备 SSL 证书

你需要:

  • fullchain.pem(证书)
  • privkey.pem(私钥)

例如使用 Let’s Encrypt:

sudo apt install certbot
sudo certbot certonly --webroot -w /var/www/html -d gitlab.example.com

证书默认在:

/etc/letsencrypt/live/gitlab.example.com/fullchain.pem
/etc/letsencrypt/live/gitlab.example.com/privkey.pem

二、修改 GitLab 配置(自带 Nginx)

编辑配置文件:

sudo vim /etc/gitlab/gitlab.rb

关键配置:

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

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

# SSL 证书路径
nginx['ssl_certificate'] = "/etc/letsencrypt/live/gitlab.example.com/fullchain.pem"
nginx['ssl_certificate_key'] = "/etc/letsencrypt/live/gitlab.example.com/privkey.pem"

# 可选:强化安全
nginx['ssl_protocols'] = "TLSv1.2 TLSv1.3"

三、重新配置 GitLab

sudo gitlab-ctl reconfigure
sudo gitlab-ctl restart

四、防火墙 & 端口

确保:

sudo ufw allow 80
sudo ufw allow 443

五、自动续签 Let’s Encrypt(重要)

编辑 crontab:

sudo crontab -e

添加:

0 3 * * * certbot renew --quiet && gitlab-ctl restart nginx

六、使用自有证书(非 Let’s Encrypt)

把你自己的证书放到安全目录,例如:

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

配置:

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

七、验证

浏览器访问:

https://gitlab.example.com

无证书警告即成功。


如果你用的是 Docker GitLab 或 外部 Nginx 反代,告诉我,我可以给你对应配置。

0 踩