原创

解决 GitLab 自带 Nginx 与系统 Nginx 端口冲突的两种方案

温馨提示:
本文最后更新于 2025年07月23日,已超过 2 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我

在使用 GitLab Omnibus 包时,默认会启用内置的 Nginx 并监听 80 端口,这往往会与系统中已安装的 Nginx 产生端口冲突,导致其中一个实例无法启动。本文将围绕这一问题,提供两种常见且实用的解决方案,并附上详细操作步骤。


问题描述

  • 场景:服务器上已运行系统级 Nginx,后续安装 GitLab Omnibus 包。
  • 冲突表现

    • GitLab 内置 Nginx 占用 80 端口,系统 Nginx 启动时报错:

      nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)
      nginx (pid ...) already running.
      
    • 无法同时运行两套 Nginx 服务。

方案一:修改 GitLab 内置 Nginx 监听端口

  1. 编辑 GitLab Nginx 配置

    sudo vim /var/opt/gitlab/nginx/conf/gitlab-http.conf
    
  2. 将监听端口 80 修改为其他端口,例如 8022:

    server {
      listen *:8022;
      server_name localhost;
      server_tokens off;
      ...
    }
    
  3. 重启 GitLab 服务

    sudo gitlab-ctl restart
    
  4. 访问
    打开浏览器,访问 http://<服务器IP>:8022 即可。

方案二:禁用 GitLab 内置 Nginx,使用系统 Nginx 反向代理

  1. gitlab.rb 中禁用内置 Nginx

    sudo vim /etc/gitlab/gitlab.rb
    

    nginx['enable'] = true
    

    修改为

    nginx['enable'] = false
    
  2. 配置 GitLab Workhorse 监听 TCP
    在同一文件中,添加或修改:

    gitlab_workhorse['listen_network'] = "tcp"
    gitlab_workhorse['listen_addr']    = "127.0.0.1:8021"
    
  3. 应用配置

    sudo gitlab-ctl reconfigure
    
  4. 使用系统 Nginx 反向代理
    在系统 Nginx 配置目录下(如 /usr/local/nginx/conf/vhost/)新建 gitlab.conf

    server {
      listen       8022;
      server_name  localhost;
    
      location / {
        proxy_pass http://127.0.0.1:8021;
        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 $scheme;
      }
    }
    
  5. 重启系统 Nginx

    sudo systemctl restart nginx
    # 或
    sudo service nginx restart
    
  6. 访问
    浏览器打开 http://<服务器IP>:8022,即可通过系统 Nginx 访问 GitLab。

正文到此结束
本文目录