在 CentOS 上部署 GitLab 后,通过域名访问一般分为几个步骤:域名解析、GitLab 配置、反向代理(可选)、HTTPS(可选)。下面按最常见场景讲清楚。
一、前提
- 已安装 GitLab(omnibus 方式,最常见)
- 有一个域名,例如:
git.example.com - 服务器有公网 IP(或内网域名也可)
二、域名解析(DNS)
在你的域名服务商处添加记录:
A 记录
主机名:git
类型:A
值:你的服务器IP
解析后:
git.example.com → 服务器IP
可用命令测试:
ping git.example.com
三、修改 GitLab 域名配置(关键)
编辑 GitLab 配置文件:
sudo vi /etc/gitlab/gitlab.rb
找到并修改:
external_url 'http://git.example.com'
如果你要用 HTTPS:
external_url 'https://git.example.com'
保存后执行:
sudo gitlab-ctl reconfigure
四、防火墙 / 端口
GitLab 默认使用 80(HTTP)或 443(HTTPS)
开放端口:
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
如果是云服务器,还要在安全组放行 80 / 443。
五、(可选)用 Nginx 反向代理
如果你服务器上已有 Nginx,不想让 GitLab 占用 80:
1. 修改 GitLab 不绑定 80
external_url 'http://git.example.com'
nginx['listen_port'] = 8080
nginx['listen_https'] = false
sudo gitlab-ctl reconfigure
2. Nginx 配置
server {
listen 80;
server_name git.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;
}
}
六、(推荐)配置 HTTPS
最简单方式(GitLab 自带 Let’s Encrypt):
external_url 'https://git.example.com'
letsencrypt['enable'] = true
letsencrypt['auto_renew'] = true
sudo gitlab-ctl reconfigure
七、访问测试
浏览器访问:
http://git.example.com
或
https://git.example.com
八、常见问题
1. 还是 IP 访问?
- 没执行
gitlab-ctl reconfigure - DNS 未生效(等几分钟)
2. 502 错误?
sudo gitlab-ctl status
sudo gitlab-ctl restart
3. 端口被占用?
检查:
sudo netstat -tunlp | grep 80
如果你愿意,可以告诉我:
- GitLab 安装方式(omnibus / docker)
- 是否已有 Nginx
- 是否公网服务器
我可以直接给你一套可用配置。