温馨提示×

centos gitlab怎样域名

小樊
45
2026-09-21 04:05:38
栏目: 云计算

在 CentOS 上部署的 GitLab,如果你想通过域名访问(而不是 IP + 端口),通常有以下几种常见做法,取决于你是 GitLab Omnibus 安装 还是 Docker 安装

下面以 最常见的情况:GitLab Omnibus(官方 RPM 安装) 为例说明。


一、前提条件

  1. 你有一个域名(如 gitlab.example.com
  2. 域名已解析到服务器公网 IP(A 记录)
  3. CentOS 已安装 GitLab(rpm 方式)
  4. 服务器防火墙 / 云安全组已放行 80 / 443

二、修改 GitLab 域名配置(核心)

1. 编辑配置文件

vim /etc/gitlab/gitlab.rb

2. 修改以下配置

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

如果是 HTTPS(推荐):

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

⚠️ 注意:

  • external_url 后,GitLab 会自动占用 80 / 443 端口
  • 不要再手动改 nginx 端口

三、重新加载配置

gitlab-ctl reconfigure
gitlab-ctl restart

四、Nginx 端口说明(常见坑)

如果你之前改过端口

比如:

nginx['listen_port'] = 8080

必须删掉或注释掉,否则域名访问会失败。

正确示例:

# nginx['listen_port'] = 8080

五、使用 HTTPS(可选但推荐)

1. 准备证书

  • 证书文件:
    • gitlab.example.com.crt
    • gitlab.example.com.key

2. 配置

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

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

创建目录并上传证书:

mkdir -p /etc/gitlab/ssl

重新配置:

gitlab-ctl reconfigure

六、防火墙 & SELinux

防火墙

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

SELinux(如报错)

setsebool -P httpd_can_network_connect 1

七、Docker 安装的 GitLab(补充)

如果你是用 Docker:

docker run -d \
  --name gitlab \
  -p 80:80 -p 443:443 \
  -e GITLAB_OMNIBUS_CONFIG="external_url 'https://gitlab.example.com'" \
  gitlab/gitlab-ce

八、常见问题

访问显示 502

  • 内存不足(GitLab 至少 2G)
  • 端口冲突
  • reconfigure 没执行

域名访问还是 IP

  • 清除浏览器缓存
  • 确认 DNS 已生效

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

  • ✅ 你是 rpm 安装 还是 docker
  • ✅ 是否要用 HTTPS
  • ✅ 是否已有域名和证书

我可以直接帮你写一份完整可用配置

0