极狐 GitLab

Linux 软件包安装的配置选项

Tier: 基础版,专业版,旗舰版

Offering: 私有化部署

要配置极狐GitLab,请在 /etc/gitlab/gitlab.rb 文件中设置相关选项。

gitlab.rb.template 包含可用选项的完整列表。默认情况下,新安装的实例会在 /etc/gitlab/gitlab.rb 中列出模板的所有选项。

编辑 /etc/gitlab/gitlab.rb 时提供的示例可能并不总是反映实例的默认设置。

有关默认设置的列表,请参见 软件包默认值。

配置极狐GitLab 的外部 URL#

要向用户显示正确的代码仓库克隆链接, 您必须为极狐GitLab 提供用户访问代码仓库所使用的 URL。 您可以使用服务器的 IP,但更推荐使用完全限定域名(FQDN)。 有关在极狐GitLab 私有化部署实例中使用 DNS 的更多详细信息,请参见 DNS 文档。

要更改外部 URL:

  1. 可选。在更改外部 URL 之前,请确定您之前是否 定义过自定义 主页 URL 或 退出后路径。 这两个设置都可能在配置新的外部 URL 后导致意外重定向。如果您已定义任何 URL,请将其完全删除。

  2. 编辑 /etc/gitlab/gitlab.rb,并将 external_url 更改为您偏好的 URL:

    ruby
    external_url "http://gitlab.example.com"

    或者,您也可以使用服务器的 IP 地址:

    ruby
    external_url "http://10.0.0.1"

    在前面的示例中,我们使用的是普通 HTTP。如果您想使用 HTTPS,请参见 如何配置 SSL。

  3. 重新配置极狐GitLab:

    shell
    sudo gitlab-ctl reconfigure
  4. 可选。如果您已经使用极狐GitLab 一段时间,在更改 外部 URL 后,您还应该 使 Markdown 缓存失效。

在安装时指定外部 URL#

如果您使用 Linux 软件包,可以通过使用 EXTERNAL_URL 环境变量, 以最少的命令设置您的极狐GitLab 实例。 如果设置了此变量,系统会自动检测到它,并将其值作为 external_url 写入 gitlab.rb 文件。

EXTERNAL_URL 环境变量仅影响软件包的安装和升级。 对于常规的重新配置运行,将使用 /etc/gitlab/gitlab.rb 中的值。

作为软件包更新的一部分,如果您无意中设置了 EXTERNAL_URL 变量, 它会在没有任何警告的情况下替换 /etc/gitlab/gitlab.rb 中的现有值。 因此,我们建议不要全局设置该变量,而是将其专门传递给安装命令:

shell
sudo EXTERNAL_URL="https://gitlab.example.com" apt-get install gitlab-ee

为极狐GitLab 配置相对 URL#

Status: 测试版

为极狐GitLab 配置相对 URL 存在 Geo 的已知问题和 测试限制。

虽然我们建议将极狐GitLab 安装在其自己的(子)域中,但有时 这是不可能的。在这种情况下,极狐GitLab 也可以 安装在相对 URL 下,例如 https://example.com/gitlab。

更改 URL 后,所有远程 URL 也会随之更改,因此您必须 在任何指向极狐GitLab 实例的本地代码仓库中手动编辑它们。

这些说明适用于 Linux 软件包安装。有关自编译(源码)安装的说明,请参见 在相对 URL 下安装极狐GitLab。

要在极狐GitLab 中启用相对 URL:

  1. 在 /etc/gitlab/gitlab.rb 中设置 external_url:

    ruby
    external_url "https://example.com/gitlab"

    在此示例中,极狐GitLab 提供服务的相对 URL 是 /gitlab。请根据您的喜好进行更改。

  2. 重新配置极狐GitLab:

    shell
    sudo gitlab-ctl reconfigure

如果您遇到任何问题,请参见故障排查部分。

从非 root 用户加载外部配置文件#

Linux 软件包安装会从 /etc/gitlab/gitlab.rb 文件加载所有配置。 此文件具有严格的文件权限,并由 root 用户拥有。严格权限 和所有权的原因是,在 gitlab-ctl reconfigure 期间,/etc/gitlab/gitlab.rb 会由 root 用户作为 Ruby 代码执行。这意味着 对 /etc/gitlab/gitlab.rb 具有写访问权限的用户可以添加由 root 作为代码执行的配置。

在某些组织中,允许访问配置文件,但不能以 root 用户身份访问。 您可以通过指定文件路径,在 /etc/gitlab/gitlab.rb 中包含一个外部配置文件:

  1. 编辑 /etc/gitlab/gitlab.rb:

    ruby
    from_file "/home/admin/external_gitlab.rb"
  2. 重新配置极狐GitLab:

    shell
    sudo gitlab-ctl reconfigure

当您使用 from_file 时:

  • 您使用 from_file 包含到 /etc/gitlab/gitlab.rb 中的代码,在重新配置极狐GitLab 时会以 root 权限运行。
  • 在 from_file 被包含之后,/etc/gitlab/gitlab.rb 中设置的任何配置 都优先于所包含文件中的配置。

从文件读取证书#

证书可以存储为单独的文件,并在运行 sudo gitlab-ctl reconfigure 时加载到内存中。包含 证书的文件必须是纯文本格式。

在此示例中,PostgreSQL 服务器证书直接从文件读取,而不是直接复制粘贴到 /etc/gitlab/gitlab.rb 中。

ruby
postgresql['internal_certificate'] = File.read('/path/to/server.crt')

从 git_data_dirs 迁移#

从 18.0 开始,git_data_dirs 将不再作为配置 Gitaly 存储位置的支持方式。如果您显式定义了 git_data_dirs,则需要 迁移该配置。

例如,对于 Gitaly 服务,如果您的 /etc/gitlab/gitlab.rb 配置如下:

ruby
git_data_dirs({ "default" => { "path" => "/mnt/nas/git-data" } })

您需要改为在 gitaly['configuration'] 下重新定义配置。 请注意,必须将 /repositories 后缀附加到路径,因为此前该后缀是 在内部附加的。

ruby
1gitaly['configuration'] = { 2 storage: [ 3 { 4 name: 'default', 5 path: '/mnt/nas/git-data/repositories', 6 }, 7 ], 8}

需要注意的是,path 的父目录也必须由 Omnibus 管理。按照上面的示例,Omnibus 必须在重新配置时修改 /mnt/nas/git-data 的权限,并且可能在运行时将数据存储在该目录中。 您应该选择一个允许此行为的合适 path。

对于 Rails 和 Sidekiq 客户端,如果您的 /etc/gitlab/gitlab.rb 配置如下:

ruby
git_data_dirs({ "default" => { "gitaly_address" => "tcp://gitaly1.internal:8075" } })

您需要改为在 gitlab_rails['repositories_storages'] 下重新定义配置:

ruby
gitlab_rails['repositories_storages'] = { "default" => { "gitaly_address" => "tcp://gitaly1.internal:8075" } }

将 Git 数据存储在备用目录中#

默认情况下,Linux 软件包安装会将 Git 代码仓库数据存储在 /var/opt/gitlab/git-data/repositories 下,并且 Gitaly 服务监听 unix:/var/opt/gitlab/gitaly/gitaly.socket。

要更改目录的位置,

  1. 编辑 /etc/gitlab/gitlab.rb:

    ruby
    1gitaly['configuration'] = { 2 storage: [ 3 { 4 name: 'default', 5 path: '/mnt/nas/git-data/repositories', 6 }, 7 ], 8}

    您还可以添加多个 Git 数据目录:

    ruby
    1gitaly['configuration'] = { 2 storage: [ 3 { 4 name: 'default', 5 path: '/var/opt/gitlab/git-data/repositories', 6 }, 7 { 8 name: 'alternative', 9 path: '/mnt/nas/git-data/repositories', 10 }, 11 ], 12}
  2. 重新配置极狐GitLab:

    shell
    sudo gitlab-ctl reconfigure
  3. 可选。如果您在 /var/opt/gitlab/git-data 中已有 Git 代码仓库,您 可以将它们移动到新位置:

    1. 在移动代码仓库时,阻止用户向其写入:

      shell
      sudo gitlab-ctl stop
    2. 将代码仓库同步到新位置。请注意,repositories 后面_没有_斜杠, 但 git-data 后面_有_斜杠:

      shell
      sudo rsync -av --delete /var/opt/gitlab/git-data/repositories /mnt/nas/git-data/
    3. 重新配置以启动必要的进程并修复任何错误的权限:

      shell
      sudo gitlab-ctl reconfigure
    4. 仔细检查 /mnt/nas/git-data/ 中的目录布局。预期输出 应为 repositories:

      shell
      sudo ls /mnt/nas/git-data/
    5. 启动极狐GitLab,并验证您可以在 Web 界面中浏览代码仓库:

      shell
      sudo gitlab-ctl start

如果您在单独的服务器上运行 Gitaly,请参见 配置 Gitaly 的文档。

如果您不想移动所有代码仓库,而是想在现有代码仓库存储之间移动特定 项目,请使用 更新项目 API 端点并指定 repository_storage 属性。

更改 Git 用户或群组的名称#

我们不建议更改现有安装的用户或群组,因为这可能会导致不可预测的副作用。

默认情况下,Linux 软件包安装使用用户名 git 进行 Git GitLab Shell 登录、 Git 数据本身的所有权以及 Web 界面上的 SSH URL 生成。 类似地,git 群组用于 Git 数据的群组所有权。

要在新的 Linux 软件包安装上更改用户和群组:

  1. 编辑 /etc/gitlab/gitlab.rb:

    ruby
    user['username'] = "gitlab" user['group'] = "gitlab"
  2. 重新配置极狐GitLab:

    shell
    sudo gitlab-ctl reconfigure

如果您要更改现有安装的用户名,重新配置运行 不会更改嵌套目录的所有权,因此您必须手动执行此操作。

至少,您必须更改代码仓库和上传 目录的所有权:

shell
sudo chown -R gitlab:gitlab /var/opt/gitlab/git-data/repositories sudo chown -R gitlab:gitlab /var/opt/gitlab/gitlab-rails/uploads

指定数字用户和群组标识符#

Linux 软件包安装会为极狐GitLab、PostgreSQL、Redis、NGINX 等创建用户。要 为这些用户指定数字标识符:

  1. 记下旧的用户和群组标识符,因为您以后可能需要它们:

    shell
    sudo cat /etc/passwd
  2. 编辑 /etc/gitlab/gitlab.rb,并更改您想要的任何标识符:

    ruby
    1user['uid'] = 1234 2user['gid'] = 1234 3postgresql['uid'] = 1235 4postgresql['gid'] = 1235 5redis['uid'] = 1236 6redis['gid'] = 1236 7web_server['uid'] = 1237 8web_server['gid'] = 1237 9registry['uid'] = 1238 10registry['gid'] = 1238 11prometheus['uid'] = 1240 12prometheus['gid'] = 1240
  3. 停止、重新配置,然后启动极狐GitLab:

    shell
    sudo gitlab-ctl stop sudo gitlab-ctl reconfigure sudo gitlab-ctl start
  4. 可选。如果您要更改 user['uid'] 和 user['gid'],请确保更新任何不由 Linux 软件包直接管理的文件的 uid/gid, 例如日志:

    shell
    find /var/log/gitlab -uid <old_uid> | xargs -I:: chown git :: find /var/log/gitlab -gid <old_uid> | xargs -I:: chgrp git :: find /var/opt/gitlab -uid <old_uid> | xargs -I:: chown git :: find /var/opt/gitlab -gid <old_uid> | xargs -I:: chgrp git ::

禁用用户和群组账号管理#

默认情况下,Linux 软件包安装会创建系统用户和群组账号, 并保持信息更新。 这些系统账号运行软件包的各个组件。 大多数用户不需要更改此行为。 但是,如果您的系统账号由其他软件(例如 LDAP)管理,您 可能需要禁用极狐GitLab 软件包执行的账号管理。

默认情况下,Linux 软件包安装期望存在以下用户和群组:

Linux 用户和群组是否必需描述默认主目录默认 shell
git是极狐GitLab 用户/群组/var/opt/gitlab/bin/sh
gitlab-www是Web 服务器用户/群组/var/opt/gitlab/nginx/bin/false
gitlab-prometheus是用于 Prometheus 监控和各种导出器的 Prometheus 用户/群组/var/opt/gitlab/prometheus/bin/sh
gitlab-redis仅当使用打包的 Redis 时极狐GitLab 的 Redis 用户/群组/var/opt/gitlab/redis/bin/false
gitlab-psql仅当使用打包的 PostgreSQL 时PostgreSQL 用户/群组/var/opt/gitlab/postgresql/bin/sh
gitlab-consul仅当使用极狐GitLab Consul 时极狐GitLab Consul 用户/群组/var/opt/gitlab/consul/bin/sh
registry仅当使用极狐GitLab Registry 时极狐GitLab Registry 用户/群组/var/opt/gitlab/registry/bin/sh
gitlab-backup仅当使用 gitlab-backup-cli 时极狐GitLab 备份 CLI 用户/var/opt/gitlab/backups/bin/sh

要禁用用户和群组账号管理:

  1. 编辑 /etc/gitlab/gitlab.rb:

    ruby
    manage_accounts['enable'] = false
  2. 可选。您也可以使用不同的用户/群组名称,但随后必须指定用户/群组详细信息:

    ruby
    1# GitLab 2user['username'] = "git" 3user['group'] = "git" 4user['shell'] = "/bin/sh" 5user['home'] = "/var/opt/custom-gitlab" 6 7# Web server 8web_server['username'] = 'webserver-gitlab' 9web_server['group'] = 'webserver-gitlab' 10web_server['shell'] = '/bin/false' 11web_server['home'] = '/var/opt/gitlab/webserver' 12 13# Prometheus 14prometheus['username'] = 'gitlab-prometheus' 15prometheus['group'] = 'gitlab-prometheus' 16prometheus['shell'] = '/bin/sh' 17prometheus['home'] = '/var/opt/gitlab/prometheus' 18 19# Redis (not needed when using external Redis) 20redis['username'] = "redis-gitlab" 21redis['group'] = "redis-gitlab" 22redis['shell'] = "/bin/false" 23redis['home'] = "/var/opt/redis-gitlab" 24 25# Postgresql (not needed when using external Postgresql) 26postgresql['username'] = "postgres-gitlab" 27postgresql['group'] = "postgres-gitlab" 28postgresql['shell'] = "/bin/sh" 29postgresql['home'] = "/var/opt/postgres-gitlab" 30 31# Consul 32consul['username'] = 'gitlab-consul' 33consul['group'] = 'gitlab-consul' 34consul['dir'] = "/var/opt/gitlab/registry" 35 36# Registry 37registry['username'] = "registry" 38registry['group'] = "registry" 39registry['dir'] = "/var/opt/gitlab/registry" 40registry['shell'] = "/usr/sbin/nologin"
  3. 重新配置极狐GitLab:

    shell
    sudo gitlab-ctl reconfigure

移动用户的主目录#

对于极狐GitLab 用户,我们建议将主目录 设置在本地磁盘上,而不是 NFS 等共享存储上,以获得更好的性能。将其设置在 NFS 上时,Git 请求必须再发出一次网络请求来读取 Git 配置,这会增加 Git 操作的延迟。

要移动现有主目录,需要停止极狐GitLab 服务,并且需要一些停机时间:

  1. 停止极狐GitLab:

    shell
    sudo gitlab-ctl stop
  2. 停止 runit 服务器:

    shell
    sudo systemctl stop gitlab-runsvdir
  3. 更改主目录:

    shell
    sudo usermod -d /path/to/home <username>

    如果您有现有数据,则需要手动将其复制/rsync 到新位置:

  4. 编辑 /etc/gitlab/gitlab.rb:

    ruby
    user['home'] = "/var/opt/custom-gitlab"
  5. 启动 runit 服务器:

    shell
    sudo systemctl start gitlab-runsvdir
  6. 重新配置极狐GitLab:

    shell
    sudo gitlab-ctl reconfigure

禁用存储目录管理#

Linux 软件包负责创建所有必要的目录, 并设置正确的所有权和权限,同时保持更新。

某些目录包含大量数据,因此在某些设置中, 这些目录很可能挂载在 NFS(或其他)共享上。

某些类型的挂载不允许 root 用户自动创建目录 (初始设置的默认用户),例如在共享上启用了 root_squash 的 NFS。 为了解决这个问题,Linux 软件包会尝试 使用目录的所有者用户来创建这些目录。

禁用 /etc/gitlab 目录管理#

如果您挂载了 /etc/gitlab 目录,可以关闭对 该目录的管理:

  1. 编辑 /etc/gitlab/gitlab.rb:

    ruby
    manage_storage_directories['manage_etc'] = false
  2. 重新配置极狐GitLab:

    shell
    sudo gitlab-ctl reconfigure

禁用 /var/opt/gitlab 目录管理#

如果您要挂载所有极狐GitLab 存储目录,且每个目录都挂载在单独的挂载点上, 则应完全禁用存储目录的管理。

Linux 软件包安装期望这些目录 存在于文件系统上。如果设置了此设置,则由您负责创建并设置正确的 权限。

启用此设置会阻止创建以下目录:

默认位置权限所有权用途
/var/opt/gitlab/git-data2770git:git存放代码仓库目录
/var/opt/gitlab/git-data/repositories2770git:git存放 Git 代码仓库
/var/opt/gitlab/gitlab-rails/shared0751git:gitlab-www存放大对象目录
/var/opt/gitlab/gitlab-rails/shared/artifacts0700git:git存放 CI 产物
/var/opt/gitlab/gitlab-rails/shared/external-diffs0700git:git存放外部合并请求 diff
/var/opt/gitlab/gitlab-rails/shared/lfs-objects0700git:git存放 LFS 对象
/var/opt/gitlab/gitlab-rails/shared/packages0700git:git存放软件包仓库
/var/opt/gitlab/gitlab-rails/shared/dependency_proxy0700git:git存放依赖代理
/var/opt/gitlab/gitlab-rails/shared/terraform_state0700git:git存放 Terraform 状态
/var/opt/gitlab/gitlab-rails/shared/ci_secure_files0700git:git存放上传的安全文件
/var/opt/gitlab/gitlab-rails/shared/agent_plan_content0700git:git存放 Agent 计划内容
/var/opt/gitlab/gitlab-rails/shared/ci_catalog_bundles0700git:git存放 CI Catalog 组件包
/var/opt/gitlab/gitlab-rails/shared/pages0750git:gitlab-www存放用户 Pages
/var/opt/gitlab/gitlab-rails/uploads0700git:git存放用户附件
/var/opt/gitlab/gitlab-ci/builds0700git:git存放 CI 构建日志
/var/opt/gitlab/.ssh0700git:git存放已授权密钥

要禁用存储目录的管理:

  1. 编辑 /etc/gitlab/gitlab.rb:

    ruby
    manage_storage_directories['enable'] = false
  2. 重新配置极狐GitLab:

    shell
    sudo gitlab-ctl reconfigure

仅在给定文件系统挂载后启动 Linux 软件包安装服务#

如果您想阻止 Linux 软件包安装服务(NGINX、Redis、Puma 等) 在给定文件系统挂载之前启动,可以设置 high_availability['mountpoint'] 设置:

  1. 编辑 /etc/gitlab/gitlab.rb:

    ruby
    # wait for /var/opt/gitlab to be mounted high_availability['mountpoint'] = '/var/opt/gitlab'
  2. 重新配置极狐GitLab:

    shell
    sudo gitlab-ctl reconfigure

    如果挂载点不存在,极狐GitLab 将无法重新配置。

配置运行时目录#

启用 Prometheus 监控后,GitLab Exporter 会对 每个 Puma 工作进程进行测量(Rails 指标)。每个 Puma 工作进程都需要为每个控制器请求 将指标文件写入临时位置。 然后 Prometheus 会收集所有这些文件并处理它们的值。

为了避免产生磁盘 I/O,Linux 软件包使用 运行时目录。

在 reconfigure 期间,软件包会检查 /run 是否为 tmpfs 挂载。 如果不是,则会显示以下警告,并禁用 Rails 指标:

plaintext
Runtime directory '/run' is not a tmpfs mount.

要重新启用 Rails 指标:

  1. 编辑 /etc/gitlab/gitlab.rb 以创建 tmpfs 挂载 (请注意,配置中没有 =):

    ruby
    runtime_dir '/path/to/tmpfs'
  2. 重新配置极狐GitLab:

    shell
    sudo gitlab-ctl reconfigure

配置失败认证封禁#

您可以为 Git 和容器镜像仓库配置 失败认证封禁。 当客户端被封禁时,会返回 403 错误代码。

可以配置以下设置:

设置描述
enabled默认为 false。将其设置为 true 以启用 Git 和镜像仓库认证封禁。
ip_whitelist不阻止的 IP。它们必须格式化为 Ruby 数组中的字符串。您可以使用单个 IP 或 CIDR 表示法,例如 ["127.0.0.1", "127.0.0.2", "127.0.0.3", "192.168.0.1/24"]。
maxretry在指定时间内可以发出请求的最大次数。
findtime失败请求在 IP 被添加到拒绝列表之前可以计入该 IP 的最长时间(以秒为单位)。
bantimeIP 被阻止的总时间(以秒为单位)。

要配置 Git 和容器镜像仓库认证封禁:

  1. 编辑 /etc/gitlab/gitlab.rb:

    ruby
    1gitlab_rails['rack_attack_git_basic_auth'] = { 2 'enabled' => true, 3 'ip_whitelist' => ["127.0.0.1"], 4 'maxretry' => 10, # Limit the number of Git HTTP authentication attempts per IP 5 'findtime' => 60, # Reset the auth attempt counter per IP after 60 seconds 6 'bantime' => 3600 # Ban an IP for one hour (3600s) after too many auth attempts 7}
  2. 重新配置极狐GitLab:

    shell
    sudo gitlab-ctl reconfigure

在安装期间禁用自动缓存清理#

如果您有一个大型极狐GitLab 安装,您可能不想运行 rake cache:clear 任务, 因为它可能需要很长时间才能完成。默认情况下,缓存清理任务会在重新配置期间自动运行。

要在安装期间禁用自动缓存清理:

  1. 编辑 /etc/gitlab/gitlab.rb:

    ruby
    # This is an advanced feature used by large gitlab deployments where loading # whole RAILS env takes a lot of time. gitlab_rails['rake_cache_clear'] = false
  2. 重新配置极狐GitLab:

    shell
    sudo gitlab-ctl reconfigure

使用 Sentry 进行错误报告和日志记录#

在极狐GitLab 17.0 及更高版本中,仅支持 Sentry 21.5.0 或更高版本。如果 您使用的是自己托管的较早版本的 Sentry 实例,则必须 升级 Sentry 才能继续 从您的极狐GitLab 环境收集错误。

Sentry 是一个开源错误报告和日志记录工具,可以作为 SaaS(https://sentry.io/welcome/)使用,也可以自行托管。

要配置 Sentry:

  1. 在 Sentry 中创建一个项目。

  2. 找到您创建的项目的 数据源名称(DSN)。

  3. 编辑 /etc/gitlab/gitlab.rb:

    ruby
    gitlab_rails['sentry_enabled'] = true gitlab_rails['sentry_dsn'] = 'https://<public_key>@<host>/<project_id>' # value used by the Rails SDK gitlab_rails['sentry_clientside_dsn'] = 'https://<public_key>@<host>/<project_id>' # value used by the Browser JavaScript SDK gitlab_rails['sentry_environment'] = 'production'

    Sentry 环境 可用于跨多个已部署的极狐GitLab 环境跟踪错误和问题, 例如实验室、开发、预发布和生产环境。

  4. 可选。要为从特定服务器发送的每个事件设置自定义 Sentry 标签, 可以设置 GITLAB_SENTRY_EXTRA_TAGS 环境变量。此变量是一个 JSON 编码的哈希,表示 应传递给 Sentry 的该服务器所有异常的任何标签。

    例如,设置:

    ruby
    gitlab_rails['env'] = { 'GITLAB_SENTRY_EXTRA_TAGS' => '{"stage": "main"}' }

    将添加值为 main 的 stage 标签。

  5. 重新配置极狐GitLab:

    shell
    sudo gitlab-ctl reconfigure

设置内容分发网络 URL#

使用 gitlab_rails['cdn_host'] 通过内容分发网络(CDN)或资产主机提供静态资产服务。 这将配置一个 Rails 资产主机。

要设置 CDN/资产主机:

  1. 编辑 /etc/gitlab/gitlab.rb:

    ruby
    gitlab_rails['cdn_host'] = 'https://mycdnsubdomain.fictional-cdn.com'
  2. 重新配置极狐GitLab:

    shell
    sudo gitlab-ctl reconfigure

有关配置常见服务作为资产主机的其他文档 在此议题中进行跟踪。

设置内容安全策略#

设置内容安全策略(CSP)有助于阻止 JavaScript 跨站脚本(XSS)攻击。有关更多详细信息,请参见 有关 CSP 的 Mozilla 文档。

带有内联 JavaScript 的 CSP 和 nonce-source 在 JihuLab.com 上可用。 在极狐GitLab 私有化部署上,它默认未配置。

错误配置 CSP 规则可能会阻止极狐GitLab 正常工作。 在推出策略之前,您可能还想将 report_only 更改为 true 以测试配置。

要添加 CSP:

  1. 编辑 /etc/gitlab/gitlab.rb:

    ruby
    gitlab_rails['content_security_policy'] = { enabled: true, report_only: false }

    极狐GitLab 会自动为 CSP 提供安全的默认值。 为指令显式设置 <default_value> 值等同于 不设置值,并将使用默认值。

    要添加自定义 CSP:

    ruby
    1gitlab_rails['content_security_policy'] = { 2 enabled: true, 3 report_only: false, 4 directives: { 5 default_src: "'none'", 6 script_src: "https://example.com" 7 } 8}

    对于未显式配置的指令,将使用安全的默认值。

    要取消设置 CSP 指令,请将值设置为 false。

  2. 重新配置极狐GitLab:

    shell
    sudo gitlab-ctl reconfigure

设置允许的主机以防止主机头攻击#

要防止极狐GitLab 接受非预期的主机头:

  1. 编辑 /etc/gitlab/gitlab.rb:

    ruby
    gitlab_rails['allowed_hosts'] = ['gitlab.example.com']
  2. 重新配置极狐GitLab:

    shell
    sudo gitlab-ctl reconfigure

极狐GitLab 中不存在因未配置 allowed_hosts 而导致的已知安全问题, 但建议将其作为纵深防御措施,以防范潜在的 HTTP 主机头攻击。

如果使用 Apache 等自定义外部代理,则可能需要添加 localhost 地址或名称(localhost 或 127.0.0.1)。您应该向外部代理添加过滤器,以缓解通过代理传递给 Workhorse 的潜在 HTTP 主机头攻击。

ruby
gitlab_rails['allowed_hosts'] = ['gitlab.example.com', '127.0.0.1', 'localhost']

要更改生成的 Web 会话 Cookie 值的前缀:

  1. 编辑 /etc/gitlab/gitlab.rb:

    ruby
    gitlab_rails['session_store_session_cookie_token_prefix'] = 'custom_prefix_'
  2. 重新配置极狐GitLab:

    shell
    sudo gitlab-ctl reconfigure

默认值为空字符串 ""。

受管设置#

受管设置会覆盖数据库中存储的值。管理员无法从 管理区域或 REST API 更改该设置。

在多节点和云原生混合部署中,请将相同的受管设置配置应用到 每个运行 Puma 或 Sidekiq 的节点上的 gitlab.rb。这可以保持各节点之间的设置一致。

您可以管理以下设置:

设置描述
sidekiq_timezone_override用于评估 Sidekiq 作业的 cron 计划所使用的时区。

要管理 Sidekiq 时区:

  1. 编辑 /etc/gitlab/gitlab.rb:

    ruby
    gitlab_rails['managed_settings']['settings'] = { 'sidekiq_timezone_override' => 'Europe/London' }
  2. 重新配置极狐GitLab:

    shell
    sudo gitlab-ctl reconfigure

重新配置会将设置写入 /var/opt/gitlab/gitlab-rails/etc/managed_settings.yml,并 重启 Puma 和 Sidekiq。如果您删除所有受管设置,重新配置会删除该文件。

更改受管方名称#

在管理区域中,受管设置会显示管理该设置的一方的名称。默认名称是 GitLab Omnibus。

要使用不同的名称:

  1. 编辑 /etc/gitlab/gitlab.rb:

    ruby
    gitlab_rails['managed_settings']['installation']['managed_by'] = 'ACME Corp'
  2. 重新配置极狐GitLab:

    shell
    sudo gitlab-ctl reconfigure

在不使用纯文本存储的情况下向组件提供敏感配置#

某些组件在 gitlab.rb 中公开了 extra_config_command 选项。这允许外部脚本动态提供密钥, 而不是从纯文本存储中读取它们。

可用选项如下:

gitlab.rb 设置职责
redis['extra_config_command']向 Redis 服务器配置文件提供额外配置。
gitlab_rails['redis_extra_config_command']向 GitLab Rails 应用程序使用的 Redis 配置文件提供额外配置。(resque.yml、redis.yml、redis.<redis_instance>.yml 文件)
gitlab_rails['db_extra_config_command']向 GitLab Rails 应用程序使用的数据库配置文件提供额外配置。(database.yml)
gitlab_kas['extra_config_command']向用于 Kubernetes 的 GitLab agent server(KAS)提供额外配置。
gitlab_workhorse['extra_config_command']向 GitLab Workhorse 提供额外配置。
gitlab_exporter['extra_config_command']向 GitLab Exporter 提供额外配置。

分配给这些选项中任何一个的值都应该是可执行脚本的绝对路径, 该脚本将所需格式的敏感配置写入 STDOUT。这些 组件会:

  1. 执行所提供的脚本。
  2. 用脚本输出的值替换用户和默认配置文件设置的值。

向 Redis 服务器和客户端组件提供 Redis 密码#

例如,您可以使用下面的脚本和 gitlab.rb 片段来指定 Redis 服务器以及需要连接到 Redis 的组件的密码。

当向 Redis 服务器指定密码时,此方法仅使用户无需 在 gitlab.rb 文件中保存纯文本密码。该密码最终会以 纯文本形式出现在位于 /var/opt/gitlab/redis/redis.conf 的 Redis 服务器配置文件中。

  1. 将下面的脚本保存为 /opt/generate-redis-conf

    ruby
    1#!/opt/gitlab/embedded/bin/ruby 2 3require 'json' 4require 'yaml' 5 6class RedisConfig 7 REDIS_PASSWORD = `echo "toomanysecrets"`.strip # Change the command inside backticks to fetch Redis password 8 9 class << self 10 def server 11 puts "requirepass '#{REDIS_PASSWORD}'" 12 puts "masterauth '#{REDIS_PASSWORD}'" 13 end 14 15 def rails 16 puts YAML.dump({ 17 'password' => REDIS_PASSWORD 18 }) 19 end 20 21 def kas 22 puts YAML.dump({ 23 'redis' => { 24 'password' => REDIS_PASSWORD 25 } 26 }) 27 end 28 29 def workhorse 30 puts JSON.dump({ 31 redis: { 32 password: REDIS_PASSWORD 33 } 34 }) 35 end 36 37 def gitlab_exporter 38 puts YAML.dump({ 39 'probes' => { 40 'sidekiq' => { 41 'opts' => { 42 'redis_password' => REDIS_PASSWORD 43 } 44 } 45 } 46 }) 47 end 48 end 49end 50 51def print_error_and_exit 52 $stdout.puts "Usage: generate-redis-conf <COMPONENT>" 53 $stderr.puts "Supported components are: server, rails, kas, workhorse, gitlab_exporter" 54 55 exit 1 56end 57 58print_error_and_exit if ARGV.length != 1 59 60component = ARGV.shift 61begin 62 RedisConfig.send(component.to_sym) 63rescue NoMethodError 64 print_error_and_exit 65end
  2. 确保上面创建的脚本是可执行的:

    shell
    chmod +x /opt/generate-redis-conf
  3. 将下面的片段添加到 /etc/gitlab/gitlab.rb:

    ruby
    1redis['extra_config_command'] = '/opt/generate-redis-conf server' 2 3gitlab_rails['redis_extra_config_command'] = '/opt/generate-redis-conf rails' 4gitlab_workhorse['extra_config_command'] = '/opt/generate-redis-conf workhorse' 5gitlab_kas['extra_config_command'] = '/opt/generate-redis-conf kas' 6gitlab_exporter['extra_config_command'] = '/opt/generate-redis-conf gitlab_exporter'
  4. 运行 sudo gitlab-ctl reconfigure。

向 GitLab Rails 提供 PostgreSQL 用户密码#

例如,您可以使用下面的脚本和配置来提供 GitLab Rails 用于连接 PostgreSQL 服务器的密码。

  1. 将下面的脚本保存为 /opt/generate-db-config:

    ruby
    1#!/opt/gitlab/embedded/bin/ruby 2 3require 'yaml' 4 5db_password = `echo "toomanysecrets"`.strip # Change the command inside backticks to fetch DB password 6 7puts YAML.dump({ 8 'main' => { 9 'password' => db_password 10 }, 11 'ci' => { 12 'password' => db_password 13 } 14})
  2. 确保上面创建的脚本是可执行的:

    shell
    chmod +x /opt/generate-db-config
  3. 将下面的片段添加到 /etc/gitlab/gitlab.rb:

    ruby
    gitlab_rails['db_extra_config_command'] = '/opt/generate-db-config'
  4. 运行 sudo gitlab-ctl reconfigure。

故障排查#

相对 URL 故障排查#

如果您在迁移到相对 URL 配置后发现极狐GitLab 资产出现任何问题 (例如图片缺失或组件无响应), 请在 极狐GitLab 中使用 Frontend 标记提交议题。

错误:Mixlib::ShellOut::ShellCommandFailed: linux_user[GitLab user and group]#

在移动用户的主目录时, 如果未停止 runit 服务,并且未手动移动该用户的 主目录,极狐GitLab 在重新配置时会遇到错误:

plaintext
1account[GitLab user and group] (package::users line 28) had an error: Mixlib::ShellOut::ShellCommandFailed: linux_user[GitLab user and group] (/opt/gitlab/embedded/cookbooks/cache/cookbooks/package/resources/account.rb line 51) had an error: Mixlib::ShellOut::ShellCommandFailed: Expected process to exit with [0], but received '8' 2---- Begin output of ["usermod", "-d", "/var/opt/gitlab", "git"] ---- 3STDOUT: 4STDERR: usermod: user git is currently used by process 1234 5---- End output of ["usermod", "-d", "/var/opt/gitlab", "git"] ---- 6Ran ["usermod", "-d", "/var/opt/gitlab", "git"] returned 8

请确保在移动主目录之前停止 runit。

更改 Git 用户或群组名称后极狐GitLab 返回 502#

如果您在现有安装上更改了 Git 用户或群组的名称, 这可能会导致许多副作用。

您可以检查与无法访问的文件相关的错误,并尝试 修复其权限:

shell
gitlab gitlab-ctl tail -f