How to setup GitLab with Existing Nginx.

share on:
How to set GitLab server with existing Nginx.

First of all your already installed Nginx setup won’t work unless you have already installed it with nginx-extras package.

GitLab will require passenger to serve its files behind an Nginx proxy. For that you will need to install nginx from start with nginx-extras.

Lets install Ruby & Passenger mod for Nginx.

sudo apt-get install ruby
sudo gem install rubygems-update
sudo update_rubygems

Passenger setup.

You can also follow the instructions at:

if you are using another version of Ubuntu then please change the Distribution name in url at the end. for e.g

sudo apt-key adv --keyserver hkp:// --recv-keys 561F9B9CAC40B2F7
sudo apt-get install -y apt-transport-https ca-certificatessudo
sudo sh -c 'echo deb bionic main > /etc/apt/sources.list.d/passenger.list'
sudo apt-get update
apt-get install -y libnginx-mod-http-passenger nginx-extras 

if [ ! -f /etc/nginx/modules-enabled/50-mod-http-passenger.conf ]; then  sudo ln -s /usr/share/nginx/modules-available/mod-http-passenger.load  /etc/nginx/modules-enabled/50-mod-http-passenger.conf ; fi 
ls /etc/nginx/conf.d/mod-http-passenger.conf    

Make sure the command apt-get install -y libnginx-mod-http-passenger nginx-extras works without any issues.

If you have any issues with this command then remove every Nginx package i.e nginx-common, nginx-extras and every other nginx-* pacakge and start a clean install of these two packages.

once you are done with package installation, edit gitlab.rb.

vim /etc/gitlab/gitlab.rb

Comment everything and make sure only three lines are un-commented at the end of file.

nginx['enable'] = false

web_server['external_users'] = ['www-data','git','nginx']  #(user your nginx will be running as. if any other user then add it in the list.)

external_url '  #(virtualhost server_name in nginx you will use to access gitlab by nginx.)

Reconfigure Gitlab.

gitlab-ctl reconfigure

Create a VirtualHost in Nginx.

vim /etc/nginx/conf.d/gitlab01.example.conf

upstream gitlab-workhorse {
  server unix:/var/opt/gitlab/gitlab-workhorse/socket fail_timeout=0;

map $http_upgrade $connection_upgrade_gitlab {
    default upgrade;
    ''      close;

log_format gitlab_access $remote_addr - $remote_user [$time_local] "$request_method $gitlab_filtered_request_uri $server_protocol" $status $body_bytes_sent "$gitlab_filtered_http_referer" "$http_user_agent";

map $request_uri $gitlab_temp_request_uri_1 {
  default $request_uri;
  ~(?i)^(?<start>.*)(?<temp>[\?&]private[\-_]token)=[^&]*(?<rest>.*)$ "$start$temp=[FILTERED]$rest";

map $gitlab_temp_request_uri_1 $gitlab_temp_request_uri_2 {
  default $gitlab_temp_request_uri_1;
  ~(?i)^(?<start>.*)(?<temp>[\?&]authenticity[\-_]token)=[^&]*(?<rest>.*)$ "$start$temp=[FILTERED]$rest";

map $gitlab_temp_request_uri_2 $gitlab_filtered_request_uri {
  default $gitlab_temp_request_uri_2;
  ~(?i)^(?<start>.*)(?<temp>[\?&]feed[\-_]token)=[^&]*(?<rest>.*)$ "$start$temp=[FILTERED]$rest";

map $http_referer $gitlab_filtered_http_referer {
  default $http_referer;
  ~^(?<temp>.*)\? $temp;

server {
  server_tokens off;

  real_ip_header X-Real-IP;
  real_ip_recursive off;

  access_log  /var/log/nginx/gitlab_access.log gitlab_access;
  error_log   /var/log/nginx/gitlab_error.log;

  location / {
    client_max_body_size 0;
    gzip off;

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

    proxy_http_version 1.1;

    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-Forwarded-Proto   $scheme;
    proxy_set_header    Upgrade             $http_upgrade;
    proxy_set_header    Connection          $connection_upgrade_gitlab;

    proxy_pass http://gitlab-workhorse;

    error_page 404 /404.html;
    error_page 422 /422.html;
    error_page 500 /500.html;
    error_page 502 /502.html;
    error_page 503 /503.html;

  location ~ ^/(404|422|500|502|503)\.html$ {
    root /opt/gitlab/embedded/service/gitlab-rails/public;


Change server_name in file to your own domain name.

Add Nginx user in group of gitlab-www.

Check the owner of gitlab socket.

ls -la /var/opt/gitlab/gitlab-workhorse/socket

srwxrwxrwx 1 git git 0 Aug 21 19:04 /var/opt/gitlab/gitlab-workhorse/socket

id www-data 
uid=33(www-data) gid=33(www-data) groups=33(www-data),999(gitlab-www)

Now check the user under which your Nginx is running.

ps -ef | grep nginx
 root 13741 1 0 xyz? 00:00:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
 www-data 14592 13741 0  xyz  ? 00:00:05 nginx: worker process
 www-data 14597 13741 0 xyz? 00:00:00 nginx: worker process

Now Add the www-data user in git and gitlab-www groups.

usermod -aG gitlab-www www-data
usermod -aG git www-data

id www-data 
uid=33(www-data) gid=33(www-data) groups=33(www-data),999(gitlab-www, 998(git)

Now restart the Nginx.

nginx -t
nginx -s reload

Finally, Now you can access your gitlab at the url
If you have any issues, please feel free to contact me.

share on:


Hello there, My name is Rishi Guleria and I work as a Linux system administrator. I have created this blog to share what I have learned so far and to learn new things. Don't forget to leave the feedback. Have a great day. :)

Leave a Response

share on: