温馨提示×

centos gitlab怎样域名访问

小樊
33
2026-10-02 22:34:13
栏目: 云计算

在 CentOS 上部署 GitLab 后,通过域名访问通常包含以下几个步骤:


一、前提条件

  1. 已安装 GitLab(omnibus 方式最常见)
  2. 有一个域名(如 gitlab.example.com)
  3. 服务器有公网 IP(或内网 DNS 可解析)
  4. 防火墙 / 安全组放行 80、443 端口

二、配置 GitLab 使用域名

1. 修改 GitLab 配置文件

sudo vi /etc/gitlab/gitlab.rb

找到并修改以下内容(没有就添加):

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

如果使用 HTTPS(推荐):

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

2. 重新配置 GitLab

sudo gitlab-ctl reconfigure

三、域名解析(DNS)

在你的域名解析控制台添加记录:

  • 类型:A
  • 主机记录:gitlab
  • 记录值:服务器 IP

例如:

gitlab.example.com → 1.2.3.4

等待 DNS 生效(几分钟~几小时)


四、防火墙设置(CentOS)

CentOS 7 / 8

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

五、使用 Nginx 反向代理(可选)

如果你 不想用 GitLab 自带 Nginx,可以禁用并自己配:

1. 禁用 GitLab Nginx

nginx['enable'] = false
sudo gitlab-ctl reconfigure

2. 自建 Nginx 示例

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;
    }
}

六、启用 HTTPS(推荐)

自动申请证书(Let’s Encrypt)

letsencrypt['enable'] = true
letsencrypt['contact_emails'] = ['admin@example.com']
sudo gitlab-ctl reconfigure

七、访问测试

浏览器访问:

http://gitlab.example.com

或

https://gitlab.example.com

八、常见问题

1. 访问显示 502

  • GitLab 服务未完全启动
sudo gitlab-ctl status

2. 端口被占用

  • 检查 80 / 443 是否被 Nginx / Apache 占用

3. 本地测试

curl http://gitlab.example.com

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

  • GitLab 安装方式(omnibus / docker)
  • 是否公网 / 内网
  • 是否已有 Nginx

我可以给你更精确的配置方案。

0 踩