温馨提示×

centos gitlab如何开https

小樊
44
2026-10-02 22:38:15
栏目: 智能运维

在 CentOS 上让 GitLab 使用 HTTPS,通常有三种常见方式。下面按最推荐 → 最灵活的顺序说明。


一、使用 GitLab 自带 Nginx + 自带证书(最简单,推荐)

1. 修改 GitLab 配置

编辑配置文件:

sudo vim /etc/gitlab/gitlab.rb

修改或添加以下内容:

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

# 自动申请 Let's Encrypt 证书(需公网域名)
letsencrypt['enable'] = true
letsencrypt['contact_emails'] = ['admin@example.com']

如果只用自签名证书,跳过 letsencrypt 配置

2. 重新配置 GitLab

sudo gitlab-ctl reconfigure

3. 防火墙开放 443

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

访问:

https://gitlab.example.com

二、使用自签名证书(内网 / 测试环境)

1. 生成证书

sudo mkdir -p /etc/gitlab/ssl
sudo openssl req -x509 -nodes -days 365 \
  -newkey rsa:2048 \
  -keyout /etc/gitlab/ssl/gitlab.example.com.key \
  -out /etc/gitlab/ssl/gitlab.example.com.crt

2. 修改 gitlab.rb

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"

3. 重新配置

sudo gitlab-ctl reconfigure

三、使用外部 Nginx / Apache 反代 HTTPS(企业常用)

场景

  • 已有 Nginx
  • GitLab 用 http://127.0.0.1:8080

1. GitLab 配置

external_url 'http://gitlab.example.com'
gitlab_rails['gitlab_port'] = 8080
nginx['enable'] = false
sudo gitlab-ctl reconfigure

2. Nginx 配置(HTTPS)

server {
    listen 443 ssl;
    server_name gitlab.example.com;

    ssl_certificate     /etc/nginx/ssl/gitlab.crt;
    ssl_certificate_key /etc/nginx/ssl/gitlab.key;

    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;
        proxy_set_header X-Forwarded-Proto https;
    }
}

四、常见问题

1. 浏览器提示不安全

  • 自签名证书正常现象
  • 企业建议用 Let’s Encrypt 或内部 CA

2. 端口被占用

sudo gitlab-ctl status
sudo netstat -tunlp | grep 443

3. 强制 HTTP 跳转 HTTPS

nginx['redirect_http_to_https'] = true

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

  • CentOS 版本(7 / 8 / Stream)
  • GitLab 安装方式(omnibus / docker)
  • 是否公网 IP / 域名

我可以直接给你一份可用配置。

0 踩