Gitlab 使用外部 Nginx


背景:在多數企業中,都會選擇 GitLab 作為代碼倉庫存儲公司的代碼,關於 GitLab 的優點、牛逼的功能屬於常識了,不了解的可以自行查詢資料。本文假設你已經知道了 GitLab,並能熟練的操作和管理它。

 

一、Nginx CVE-2021-23017 漏洞說明

2021年5月25日,F5公司發布了影響NGINX DNS解析的低風險漏洞 CVE-2021-23017,受影響版本為NGINX開源和NGINX Plus版本。通過更新或升級NGINX的指定版本均可以得到解決。

CVE-2021-23017風險描述:

        針對CVE-2021-23017漏洞,如果攻擊者偽造來自指定的DNS服務器發出的UDP包,NGINX 解析器(resolver)可能引起一個字節的內存重寫,從而可能導致NGINX worker進程崩潰或其他未指定的影響。

CVE-2021-23017影響范圍:

        遠程攻擊者可能導致NGINX worker進程停止響應,從而拒絕某些用戶的訪問。僅當已配置一個或多個解析程序指令時,才存在此漏洞。默認情況下,NGINX不會配置任何解析器。

        如果客戶上線配置中不包含resolver相關的配置,不會受到此風險的影響,可以暫時對本次的CVE低風險漏洞不做考慮,也可以通過升級到新的版本解決這個問題。

 

        原文作者:NGINX官方賬號

        對於NGINX開源用戶,受影響版本為:0.6.18 - 1.20.0,升級到如下版本可修復此CVE漏洞:

  • NGINX 1.21.0

  • NGINX 1.20.1

 

二、GitLab 內置 Nginx

        GitLab 是一個用於倉庫管理系統的開源項目,使用Git作為代碼管理工具,並在此基礎上搭建起來的Web服務。GitLab 內置了一系列的服務,像 Nginx、DNS、SSL、SMTP、postgresql、redis、registry、prometheus等,可以在配置文件 /etc/gitlab/gitlab.rb 中找到各自的默認配置。

內置 Nginx 版本檢查

        在低版本的 GitLab 中,內置的 Nginx 版本也比較低,就會存在安全風險,nginx 的版本可以通過命令查看

$ sudo /opt/gitlab/embedded/sbin/nginx -v

nginx version: nginx/1.18.0

 

三、GitLab 內置 Nginx 漏洞的解決辦法

一般有以下三種解決方案:

  1. 升級 GitLab 版本,升級過程復雜,有丟失數據的風險,如果要升級,一定做好備份

  2. 替換 GitLab 自帶的 nginx,方案比較復雜,會出現較多問題

  3. 在 GitLab 外用 nginx 最新版再負載一層,這個方案不需要修改很多 GitLab 原有的配置,目前使用的人多

 

        我用的 GitLab 版本是13.7.0,比較老舊,升級的話,要先升級到13.8.8,再升級到12.12.15...,具體可以參考官網介紹。過程繁瑣,因此暫時不打算升級。況且,GitLab 能夠穩定運行,升級也存在風險,有其他辦法可以解決Nginx 漏洞,也沒必要一定要升級。

13.0.14 -> 13.1.11 -> 13.8.8 -> 13.12.15 -> 14.0.12 -> latest 14.Y.Z

 

        第三種方案,比較簡單,懂點 nginx 知識的人,配置下來應該沒問題。這里我使用的是第二種方案,替換 GitLab 自帶的 nginx

 

四、替換 GitLab 自帶的 nginx 步驟

1、修改 GitLab 配置文件

        GitLab 主要設置都在/etc/gitlab/gitlab.rb文件里面,直接 vim 打開,修改幾處內容

    $ sudo vim /etc/gitlab/gitlab.rb
external_url 'http://gitlab.abcd.com'
nginx['enable'] = false
web_server['external_users'] = ['nginx']

        external_url 就是給 GitLab 准備的域名,如果配置好了的話,可以不用修改

        nginx['enable'] 是替換自帶 Nginx 的關鍵,true 是選擇自帶的 Nginx,false 是選擇自己安裝的 Nginx

        web_server['external_users']  這里配置自帶 Nginx 的啟動用戶,建議留空,不配置

 

2、重新配置 GitLab

        修改 GitLab 配置文件后,執行命令讓配置生效,如果遇到問題的話,更多參考內容看這里(英文)。

      $ sudo gitlab-ctl reconfigure

        如果 reconfigure 報錯,需要檢查配置。不報錯,再進行下面的操作。

 

3、安裝 Nginx

        這一步比較簡單,可以直接使用官方的文檔

        https://nginx.org/en/linux_packages.html#RHEL-CentOS

        最簡單的方法,配置官方 yum 源,yum install 安裝

 

4、配置 Nginx 代理Gitlab服務

        這一步是重點,安裝好 Nginx 后,先別啟動,需要對 Nginx 配置文件進行配置。

        GitLab 官方為我們准備好了 Nginx 需要的配置文件,訪問這里GitLab recipes repository去下載。

upstream gitlab {
server unix:/var/opt/gitlab/gitlab-workhorse/socket;  ## 這里的 socket 路徑,要在實際環境中適配
}

server {
listen *:80;
server_name gitlab.abcd.com; # 請修改為你的域名

server_tokens off;     # don't show the version number, a security best practice
root /opt/gitlab/embedded/service/gitlab-rails/public;

 # Increase this if you want to upload large attachments
 # Or if you want to accept large git objects over http
client_max_body_size 250m;

 # individual nginx logs for this gitlab vhost
access_log /var/log/gitlab/gitlab_access.log;
error_log   /var/log/gitlab/gitlab_error.log;

location / {
   # serve static files from defined root folder;.
   # @gitlab is a named location for the upstream fallback, see below
  try_files $uri $uri/index.html $uri.html @gitlab;
}

 # if a file, which is not found in the root folder is requested,
 # then the proxy pass the request to the upsteam (gitlab unicorn)
location @gitlab {
   # If you use https make sure you disable gzip compression
   # to be safe against BREACH attack

  proxy_read_timeout 300; # Some requests take more than 30 seconds.
  proxy_connect_timeout 300; # Some requests take more than 30 seconds.
  proxy_redirect     off;

  proxy_set_header   X-Forwarded-Proto $scheme;
  proxy_set_header   Host              $http_host;
  proxy_set_header   X-Real-IP         $remote_addr;
  proxy_set_header   X-Forwarded-For   $proxy_add_x_forwarded_for;
  proxy_set_header   X-Frame-Options   SAMEORIGIN;

   #proxy_pass http://gitlab;
  proxy_pass http://gitlab-workhorse;
}

 # Enable gzip compression as per rails guide: http://guides.rubyonrails.org/asset_pipeline.html#gzip-compression
 # WARNING: If you are using relative urls do remove the block below
 # See config/application.rb under "Relative url support" for the list of
 # other files that need to be changed for relative url support
location ~ ^/(assets)/ {
  root /opt/gitlab/embedded/service/gitlab-rails/public;
   # gzip_static on; # to serve pre-gzipped version
  expires max;
  add_header Cache-Control public;
}

error_page 502 /502.html;
}

  配置完后,檢查 nginx 配置文件語法
  $ nginx -t

        這些配置 GitLab 官方也有給,需要自己根據實際情況修改,才能最終完成 Nginx 配置。

5、重啟 GitLab

  $ sudo gitlab-ctl restart

       重啟過程需要一點時間

6、啟動 Nginx


$ sudo systemctl restart nginx

       啟動好 Nginx 之后,瀏覽器訪問 GitLab 域名,成功即可!!!

 

 

如果對你的學習和使用有幫助,請轉發,謝謝!

也可以關注公眾號,加微信:yf13476132778,一起交流!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM