在 Ubuntu 上给 GitLab 配置 SSL(HTTPS),常见有两种方式:
下面以 GitLab Omnibus(官方包)最常见方式 为例说明。
你需要:
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
编辑配置文件:
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"
sudo gitlab-ctl reconfigure
sudo gitlab-ctl restart
确保:
sudo ufw allow 80
sudo ufw allow 443
编辑 crontab:
sudo crontab -e
添加:
0 3 * * * certbot renew --quiet && gitlab-ctl restart nginx
把你自己的证书放到安全目录,例如:
/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 反代,告诉我,我可以给你对应配置。