温馨提示×

linux gitlab怎么域名访问

小樊
46
2026-09-13 14:26:35
栏目: 云计算

在 Linux 上部署的 GitLab 想要通过域名访问,一般需要做下面几步(以官方 Omnibus GitLab 为例,最常见):


一、前提条件

  1. 一台 Linux 服务器(CentOS / Ubuntu 等)
  2. 已安装 GitLab
  3. 一个域名(如 git.example.com
  4. 域名已解析到服务器公网 IP

二、域名解析(DNS)

在你的域名服务商处添加记录:

类型:A
主机记录:git
记录值:你的服务器公网IP

例如:

git.example.com → 1.2.3.4

三、修改 GitLab 配置文件

编辑 GitLab 配置:

sudo vim /etc/gitlab/gitlab.rb

修改或添加以下内容:

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

如果你要用 HTTPS(推荐):

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

保存后执行:

sudo gitlab-ctl reconfigure

四、防火墙 & 端口

确保端口开放:

HTTP

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

HTTPS(如果用)

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

云服务器还要在安全组放通 80 / 443。


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

如果你服务器上已有 Nginx:

  1. 禁用 GitLab 自带 Nginx:
nginx['enable'] = false
  1. 自己配置 Nginx:
server {
    listen 80;
    server_name git.example.com;

    location / {
        proxy_pass http://127.0.0.1:8080;
    }
}

然后:

sudo gitlab-ctl reconfigure
sudo systemctl restart nginx

六、HTTPS(Let’s Encrypt 自动证书)

gitlab.rb 中:

letsencrypt['enable'] = true
letsencrypt['auto_renew'] = true

重新配置:

sudo gitlab-ctl reconfigure

七、访问测试

浏览器访问:

http://git.example.com

https://git.example.com

常见问题

  • 访问不了:检查 DNS、防火墙、安全组
  • 502:GitLab 服务未启动或端口冲突
  • 域名跳转 IP:external_url 没改对

如果你能告诉我:

  • GitLab 安装方式(Omnibus / Docker / 源码)
  • 是否用 Docker
  • 是否已有 Nginx

我可以给你更精确的配置示例

0