温馨提示×

centos gitlab怎么配置域名

小樊
47
2026-09-27 01:31:50
栏目: 云计算

在 CentOS 上给 GitLab 配置域名,通常有两种常见场景:

  1. 直接通过 GitLab 自带的 Nginx 监听 80/443(推荐)
  2. 用外部 Nginx / Apache 反向代理

下面以 GitLab 自带 Nginx(最常用) 为例说明。


一、前提条件

  • 已安装 GitLab(omnibus 方式)
  • 有一个域名(如 git.example.com)
  • 域名已解析到服务器公网 IP
  • 服务器防火墙放行 80 / 443

二、修改 GitLab 配置文件

编辑 GitLab 配置:

sudo vim /etc/gitlab/gitlab.rb

1️⃣ 配置域名

找到或添加:

external_url 'http://git.example.com'

如果是 HTTPS:

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

三、HTTPS(可选但推荐)

方式一:使用自有证书

把证书放到服务器,例如:

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

在 gitlab.rb 中:

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

方式二:使用 Let’s Encrypt(自动签发)

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

GitLab 会自动申请和续期证书。


四、重新配置 GitLab

每次修改配置后都必须执行:

sudo gitlab-ctl reconfigure

重启服务(可选):

sudo gitlab-ctl restart

五、防火墙与端口

确保防火墙允许:

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

六、验证

浏览器访问:

http://git.example.com

或

https://git.example.com

七、常见问题

1️⃣ 访问显示 502

  • 端口冲突
  • 内存不足(GitLab 至少 2G)
  • Nginx 未启动

查看状态:

sudo gitlab-ctl status

2️⃣ 端口不是 80

例如改成 8080:

external_url 'http://git.example.com'
nginx['listen_port'] = 8080

3️⃣ 使用外部 Nginx

如果你用外部 Nginx 反向代理,我可以单独给你一份配置。


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

  • GitLab 安装方式(omnibus / docker)
  • 是否要用 HTTPS
  • 是否已有域名解析

我可以直接给你一份可复制的配置。

0 踩