Search code examples
ubuntunginxreverse-proxygitlab

Forwarding to GitLab Subdomain with Existing Nginx Installation


I've been following the instructions from the GitLab wiki, however, it seems as if some key pieces of information are missing. In the section "Using a Non-Bundled Web Server" it never explains how I need to reconfigure my Nginx installation to reverse proxy over to GitLab.

Basically, I'd like to have GitLab installed under git.example.com, but I can't seem to find the configuration settings for my existing Nginx installation that'll do that. The wiki page goes on to talk about configuring an existing Passenger/Nginx installation, but I don't have Passenger, so I don't think that applies to my situation.

I suppose the easiest solution would be if there were a way to tell Gitlab to use it's built-in Nginx and just listen on an internal port, and then have my other Nginx forward to that port, but I can't seem to figure out how to configure Gitlab to handle that.

Any help would be greatly appreciated.


Solution

  • Based on @cyberchis's answer i simplified the process, and I have got through the same setup twice. I hope that it also works for you.

    1. Check the user of nginx

      1.1. Open nginx.conf with nano /etc/nginx/nginx.conf.

      1.2. Check the 1st. line user www-data;, and the user here is www-data.

    2. Edit external_url of gitlab

      2.1. Open gitlab.rb with nano /etc/gitlab/gitlab.rb.

      2.2. Edit the line external_url 'GENERATED_EXTERNAL_URL' to external_url 'http://gitlab.yourdomain.com'.

      2.3. Uncomment and change the line nginx['enable'] = true to nginx['enable'] = false.

      2.4. Uncomment and change the line web_server['external_users'] = [] to web_server['external_users'] = ['www-data'].

    3. Add a configuration file for gitlab

      3.1. Download the gitlab-omnibus-nginx.conf from gitlab repository.

      3.2. Go to the directory where the file is, and copy this file to nginx with cp /directory-to-this-file/gitlab-omnibus-nginx.conf /etc/nginx/sites-enabled.

      3.3. Open this file with nano /etc/nginx/sites-enabled/gitlab-omnibus-nginx.conf.

      3.4. Change this line listen 0.0.0.0:80 default_server; to listen 0.0.0.0:7001; // gitlab runs on port 7001

      3.5. Change this line listen [::]:80 default_server; to listen [::]:7001; // gitlab runs on port 7001

      3.6. Change this line server_name YOURSERVER_FQDN to server_name www.yourdomain.com.

    4. Configure nginx

      4.1. Open nginx.conf with nano /etc/nginx/nginx.conf.

      4.2. Add this configuration

    http {
    
       ...
      
       server {
           listen 80;
           server_name gitlab.yourdomain.com;
           location / {
               proxy_pass http://127.0.0.1:7001;
           }
       }
    }
    
    1. Reconfigure gitlab and reload nginx

      5.1. sudo gitlab-ctl reconfigure

      5.2. sudo systemctl reload nginx

    2. Configure firewall to export port 7001 (Optional)

      Since the gitlab runs on my local server, therefore the port 7001 has to been allowed to reach from the outside. Easiest way to enable it is to run ufw allow 7001.

    Now the gitlab runs on your subdomain gitlab.yourdomain.com which you should access.