dockernginxgitlab

How to use Nginx (deployed with Docker) reverse proxy Gitlab (deployed with Docker too)


I installed Gitlab according to the official documentation.

sudo docker run --detach \
  --hostname git.xxx.com \
  --publish 8443:443 --publish 880:80 --publish 822:22 \
  --name gitlab \
  --restart always \
  --volume $GITLAB_HOME/config:/etc/gitlab \
  --volume $GITLAB_HOME/logs:/var/log/gitlab \
  --volume $GITLAB_HOME/data:/var/opt/gitlab \
  --shm-size 256m \
  gitlab/gitlab-ee:latest

Now I want to use Nginx (installed by myself) to reverse proxy Gitlab instead of the Nginx that comes with the Gitlab container.

According to official documentation I added some code in gitlab.rb

# Define the external url
external_url 'http://git.stupidpz.com'

# Disable the built-in nginx
nginx['enable'] = false

# Disable the built-in puma
puma['enable'] = false

# Set the internal API URL
gitlab_rails['internal_api_url'] = 'http://git.stupidpz.com'

# Define the web server process user (ubuntu/nginx)
web_server['external_users'] = ['nginx']

Then gitlab cannot be accessed, I found some error logs in this file /var/log/gitblab/gitlab_workhorse/current

{"correlation_id":"","duration_ms":0,"error":"badgateway: failed to receive response: dial tcp 127.0.0.1:8080: connect: connection refused","level":"error","method":"GET","msg":"","time":"2023-01-25T20:57:21Z","uri":""}
{"correlation_id":"","duration_ms":0,"error":"badgateway: failed to receive response: dial tcp 127.0.0.1:8080: connect: connection refused","level":"error","method":"GET","msg":"","time":"2023-01-25T20:57:31Z","uri":""}
{"correlation_id":"","duration_ms":0,"error":"badgateway: failed to receive response: dial tcp 127.0.0.1:8080: connect: connection refused","level":"error","method":"GET","msg":"","time":"2023-01-25T20:57:41Z","uri":""}
{"correlation_id":"","duration_ms":0,"error":"badgateway: failed to receive response: dial tcp 127.0.0.1:8080: connect: connection refused","level":"error","method":"GET","msg":"","time":"2023-01-25T20:57:51Z","uri":""}

Did nothing else except for adding some code in gitlab.rb.

I wonder where this dial tcp 127.0.0.1:8080 comes from?


Solution

  • Now i figure out why i could not make it works,I mixed up Using an existing Passenger/NGINX installation and Using a non-bundled web-server If you just need to use your own nginx to proxy gitlab(both of them was installed on docker) you just need to add two lines to gitlab.rb.

    # Disable the built-in nginx
    nginx['enable'] = false
    # Define the web server process user (ubuntu/nginx)
    web_server['external_users'] = ['nginx']
    

    and here is nginx's conf

    upstream gitlab-workhorse {
      server unix://var/opt/gitlab/gitlab-workhorse/sockets/socket fail_timeout=0;
    }
    
    server {
      listen *:80;
      server_name git.example.com;
      server_tokens off;
      root /opt/gitlab/embedded/service/gitlab-rails/public;
    
      client_max_body_size 250m;
    
      access_log  /var/log/gitlab/nginx/gitlab_access.log;
      error_log   /var/log/gitlab/nginx/gitlab_error.log;
    
      # Ensure Passenger uses the bundled Ruby version
      passenger_ruby /opt/gitlab/embedded/bin/ruby;
    
      # Correct the $PATH variable to included packaged executables
      passenger_env_var PATH "/opt/gitlab/bin:/opt/gitlab/embedded/bin:/usr/local/bin:/usr/bin:/bin";
    
      # Make sure Passenger runs as the correct user and group to
      # prevent permission issues
      passenger_user git;
      passenger_group git;
    
      # Enable Passenger & keep at least one instance running at all times
      passenger_enabled on;
      passenger_min_instances 1;
    
      location ~ ^/[\w\.-]+/[\w\.-]+/(info/refs|git-upload-pack|git-receive-pack)$ {
        # 'Error' 418 is a hack to re-use the @gitlab-workhorse block
        error_page 418 = @gitlab-workhorse;
        return 418;
      }
    
      location ~ ^/[\w\.-]+/[\w\.-]+/repository/archive {
        # 'Error' 418 is a hack to re-use the @gitlab-workhorse block
        error_page 418 = @gitlab-workhorse;
        return 418;
      }
    
      location ~ ^/api/v3/projects/.*/repository/archive {
        # 'Error' 418 is a hack to re-use the @gitlab-workhorse block
        error_page 418 = @gitlab-workhorse;
        return 418;
      }
    
      # Build artifacts should be submitted to this location
      location ~ ^/[\w\.-]+/[\w\.-]+/builds/download {
          client_max_body_size 0;
          # 'Error' 418 is a hack to re-use the @gitlab-workhorse block
          error_page 418 = @gitlab-workhorse;
          return 418;
      }
    
      # Build artifacts should be submitted to this location
      location ~ /ci/api/v1/builds/[0-9]+/artifacts {
          client_max_body_size 0;
          # 'Error' 418 is a hack to re-use the @gitlab-workhorse block
          error_page 418 = @gitlab-workhorse;
          return 418;
      }
    
      # Build artifacts should be submitted to this location
      location ~ /api/v4/jobs/[0-9]+/artifacts {
          client_max_body_size 0;
          # 'Error' 418 is a hack to re-use the @gitlab-workhorse block
          error_page 418 = @gitlab-workhorse;
          return 418;
      }
    
    
      # For protocol upgrades from HTTP/1.0 to HTTP/1.1 we need to provide Host header if its missing
      if ($http_host = "") {
      # use one of values defined in server_name
        set $http_host_with_default "git.example.com";
      }
    
      if ($http_host != "") {
        set $http_host_with_default $http_host;
      }
    
      location @gitlab-workhorse {
    
        ## https://github.com/gitlabhq/gitlabhq/issues/694
        ## Some requests take more than 30 seconds.
        proxy_read_timeout      3600;
        proxy_connect_timeout   300;
        proxy_redirect          off;
    
        # Do not buffer Git HTTP responses
        proxy_buffering off;
    
        proxy_set_header    Host                $http_host_with_default;
        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;
    
        proxy_pass http://gitlab-workhorse;
    
        ## The following settings only work with NGINX 1.7.11 or newer
        #
        ## Pass chunked request bodies to gitlab-workhorse as-is
        # proxy_request_buffering off;
        # proxy_http_version 1.1;
      }
    
      ## Enable gzip compression as per rails guide:
      ## http://guides.rubyonrails.org/asset_pipeline.html#gzip-compression
      ## WARNING: If you are using relative urls 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;
      }
    
      ## To access Grafana
      location /-/grafana/ {
        proxy_pass http://localhost:3000/;
      }
    
      error_page 502 /502.html;
    }
    

    last but not least,you need to add another bash to your nginx's container,

    -v /var/opt/gitlab:/var/opt/gitlab
    

    This will let your nginx container connect to gitlab container.Otherwise you will get "cannot find var/opt/gitlab/gitlab-workhorse/sockets/socket".