How to redirect old to new `external_url` after changing the `external_url`?

We used to operate our local GitLab instance with a self-signed certificate at https://gitlab (where gitlab was the hostname of the server), which was accessible from our internal network.

Due to a new requirement to make the instance accessible from our external servers (and to get rid of the occasional issues that come with the self-signed certificate), we wanted to migrate from https://gitlab to https://gitlab.example.com.

Basically this worked easily by changing the external_url accordingly and gitlab-ctl reconfigure && gitlab-ctl restart.

However this broke old links starting with https://gitlab/..., because GitLab presented the new certificate for gitlab.example.com, which was of course refused by the browser.

(I will post the solution in a second.)

Basically the solution was to add the following custom_nginx_config

# /etc/gitlab/nginx_custom_nginx_config.conf
server {
  listen 80;
  listen 443 ssl;
  server_name gitlab;
  ssl_certificate /etc/gitlab/ssl/gitlab.crt;
  ssl_certificate_key /etc/gitlab/ssl/gitlab.key;
  return 301 https://gitlab.example.com$request_uri;
}

… and include it in GitLab’s nginx config:

# /etc/gitlab/gitlab.rb
nginx['custom_nginx_config'] = "include /etc/gitlab/nginx_custom_nginx_config.conf;"

And it worked! :tada:

1 Like

Inside the server block, add the following location block:

location / {
    return 301 https://gitlab.example.com$request_uri;
}

Replace gitlab.example.com with your new domain or hostname.
Save and reconfigure GitLab to apply the changes:
sudo gitlab-ctl reconfigure
Now old URLs starting with https://gitlab/... will be redirected to the new https://gitlab.example.com/... URLs.