在部署 GitLab 时,可能涉及以下几部分内容:
本文将完整涵盖这些配置,帮助您成功部署和维护 GitLab 服务。
使用以下示例配置文件,通过 FRP 将 GitLab 的 HTTP 和 HTTPS 服务暴露到外部。
[[proxies]]
name = "GitLab-HTTP"
type = "tcp"
local_ip = "127.0.0.1"
local_port = 6680 # 群晖上 GitLab 的 HTTP 端口
remote_port = 8080 # 阿里云服务器上的 HTTP 映射端口
[[proxies]]
name = "GitLab-HTTPS"
type = "tcp"
local_ip = "127.0.0.1"
local_port = 6443 # 群晖上 GitLab 的 HTTPS 端口
remote_port = 8443 # 阿里云服务器上的 HTTPS 映射端口
阿里云服务器需要配置 Nginx,代理到 FRP 服务端暴露的端口。
server {
listen 80;
server_name gitlab.example.com;
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-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_connect_timeout 60s;
proxy_read_timeout 60s;
proxy_send_timeout 60s;
}
}
server {
listen 443 ssl;
server_name gitlab.example.com;
ssl_certificate /etc/letsencrypt/live/gitlab.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/gitlab.example.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
location / {
proxy_pass https://127.0.0.1:8443;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_connect_timeout 60s;
proxy_read_timeout 60s;
proxy_send_timeout 60s;
}
}
gitlab.rb 是 GitLab 配置的核心文件,位于容器内的 /etc/gitlab/gitlab.rb
。
# 设置外部 URL 为 HTTPS
external_url 'https://gitlab.example.com'
# 启用内置 Nginx
nginx['enable'] = true
# 客户端上传文件大小限制
nginx['client_max_body_size'] = '250m'
# 强制 HTTP 重定向到 HTTPS
nginx['redirect_http_to_https'] = true
nginx['redirect_http_to_https_port'] = 443
# SSL 证书和私钥路径
nginx['ssl_certificate'] = "/etc/letsencrypt/live/gitlab.example.com/fullchain.pem"
nginx['ssl_certificate_key'] = "/etc/letsencrypt/live/gitlab.example.com/privkey.pem"
# 启用的 TLS 协议和加密算法
nginx['ssl_protocols'] = "TLSv1.2 TLSv1.3"
nginx['ssl_ciphers'] = "ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384"
nginx['ssl_prefer_server_ciphers'] = "on"
在容器中运行以下命令:
gitlab-ctl reconfigure
gitlab-ctl restart
如果 GitLab 的内置 Nginx 服务占用了 80 端口,Certbot 无法运行。需要停止 Nginx:
gitlab-ctl stop nginx
运行以下命令生成证书:
certbot certonly --standalone -d gitlab.example.com
提示: 如果生成失败,请查看日志
/var/log/letsencrypt/letsencrypt.log
。
ls /etc/letsencrypt/live/gitlab.example.com/
应包含以下文件:
gitlab-ctl start nginx
certbot certificates
输出示例:
Certificate Name: gitlab.example.com
Domains: gitlab.example.com
Expiry Date: 2024-01-15 23:59:59+00:00 (VALID: 59 days)
certbot renew --dry-run
成功输出示例:
Congratulations, all renewals succeeded!
在容器内设置定时任务:
apt install cron -y
crontab -e
添加以下内容:
0 2 * * * certbot renew --deploy-hook "gitlab-ctl restart nginx"
启动 Cron 服务:
cron
验证任务是否正常运行:
cat /var/log/cron.log
现象: Certbot 报错。
解决方案:
netstat -tuln | grep :80
certbot renew
现象: 配置 HTTPS 后仍使用 HTTP 拉取代码。
解决方案:
修改 gitlab.rb
:
external_url 'https://gitlab.example.com'
然后运行:
gitlab-ctl reconfigure
阿里云 Nginx 配置的区别:
proxy_pass http://127.0.0.1:8080;
proxy_pass https://127.0.0.1:8443;
FRP 的 HTTP 和 HTTPS 配置需区分:
申请证书前的前提条件:
为何配置 HTTPS 的 external_url:
external_url
未配置为 https://
,即使 HTTPS 配置成功,GitLab 界面生成的 URL 仍然是 HTTP。通过 HTTP 判断 FRP 服务状态: