# HG changeset patch
# User Naman Jagdish Gala <ngala@gitlab.com>
# Date 1716405124 0
#      Wed May 22 19:12:04 2024 +0000
# Node ID 8e5b48ccec7b0fe768430bc21ceb74397f5e7e45
# Parent  e2a421ca782af7abcb5957bb275529ff35c5c988
Fix proxy_redirect when listen_port is specified for GitLab Pages

- Adds cases to the proxy_redirect statements to account for
  when the administrator configured a custom listen_port
  for the GitLab Pages service.

Related: https://gitlab.com/gitlab-org/gitlab/-/issues/458587

Changelog: fixed

diff --git a/files/gitlab-cookbooks/gitlab-pages/libraries/gitlab_pages.rb b/files/gitlab-cookbooks/gitlab-pages/libraries/gitlab_pages.rb
--- a/files/gitlab-cookbooks/gitlab-pages/libraries/gitlab_pages.rb
+++ b/files/gitlab-cookbooks/gitlab-pages/libraries/gitlab_pages.rb
@@ -157,15 +157,15 @@
           {
             "~^(#{redirect_uri})(.*)$" => "$1$2",
             "~^#{url_scheme}://([^/]*)\\.(#{Gitlab['pages_nginx']['fqdn_regex']})/(.*)$" => "#{url_scheme}://$2/$1/$3",
-            "~^//([^/]*)\\.(#{Gitlab['pages_nginx']['fqdn_regex']})/(.*)$" => "/$1/$3",
-            "~^/(.*)$" => "/$namespace/$1",
+            "~^//([^/]*)\\.(#{Gitlab['pages_nginx']['fqdn_regex']})/(.*)$" => "#{url_scheme}://$2/$1/$3",
+            "~^/(.*)$" => "#{url_scheme}://#{Gitlab['pages_nginx']['fqdn_regex']}/$namespace/$1",
           }
         else
           {
             "~^(#{redirect_uri})(.*)$" => "$1$2",
             "~^#{url_scheme}://([^/]*)\\.(#{Gitlab['pages_nginx']['fqdn_regex']}:#{pages_port})/(.*)$" => "#{url_scheme}://$2/$1/$3",
-            "~^//([^/]*)\\.(#{Gitlab['pages_nginx']['fqdn_regex']}:#{pages_port})/(.*)$" => "/$1/$3",
-            "~^/(.*)$" => "/$namespace/$1",
+            "~^//([^/]*)\\.(#{Gitlab['pages_nginx']['fqdn_regex']}:#{pages_port})/(.*)$" => "#{url_scheme}://$2/$1/$3",
+            "~^/(.*)$" => "#{url_scheme}://#{Gitlab['pages_nginx']['fqdn_regex']}:#{pages_port}/$namespace/$1",
           }
         end
     end
diff --git a/spec/chef/cookbooks/gitlab/recipes/nginx_spec.rb b/spec/chef/cookbooks/gitlab/recipes/nginx_spec.rb
--- a/spec/chef/cookbooks/gitlab/recipes/nginx_spec.rb
+++ b/spec/chef/cookbooks/gitlab/recipes/nginx_spec.rb
@@ -923,8 +923,8 @@
           expect(content).to include('proxy_set_header X-Gitlab-Namespace-In-Path $namespace;')
           expect(content).to include('proxy_redirect ~^(https://projects\.pages\.localhost/auth)(.*)$ $1$2;')
           expect(content).to include('proxy_redirect ~^https://([^/]*)\.(pages\.localhost)/(.*)$ https://$2/$1/$3;')
-          expect(content).to include('proxy_redirect ~^//([^/]*)\.(pages\.localhost)/(.*)$ /$1/$3;')
-          expect(content).to include('proxy_redirect ~^/(.*)$ /$namespace/$1;')
+          expect(content).to include('proxy_redirect ~^//([^/]*)\.(pages\.localhost)/(.*)$ https://$2/$1/$3;')
+          expect(content).to include('proxy_redirect ~^/(.*)$ https://pages\.localhost/$namespace/$1;')
           expect(content).to include('proxy_hide_header X-Gitlab-Namespace-In-Path;')
           # Below checks are to verify proper render entries are made
           expect(content).to include('proxy_http_version 1.1;').twice
@@ -959,8 +959,8 @@
           expect(content).to include('proxy_set_header X-Gitlab-Namespace-In-Path $namespace;')
           expect(content).to include('proxy_redirect ~^(https://projects\.pages\.localhost/auth)(.*)$ $1$2;')
           expect(content).to include('proxy_redirect ~^https://([^/]*)\.(pages\.localhost:25800)/(.*)$ https://$2/$1/$3;')
-          expect(content).to include('proxy_redirect ~^//([^/]*)\.(pages\.localhost:25800)/(.*)$ /$1/$3;')
-          expect(content).to include('proxy_redirect ~^/(.*)$ /$namespace/$1;')
+          expect(content).to include('proxy_redirect ~^//([^/]*)\.(pages\.localhost:25800)/(.*)$ https://$2/$1/$3;')
+          expect(content).to include('proxy_redirect ~^/(.*)$ https://pages\.localhost:25800/$namespace/$1;')
           expect(content).to include('proxy_hide_header X-Gitlab-Namespace-In-Path;')
           # Below checks are to verify proper render entries are made
           expect(content).to include('proxy_http_version 1.1;').twice
@@ -988,10 +988,7 @@
           expect(content).not_to include('rewrite ^/([^/]+)/(.*)$ /$2 break;')
           expect(content).not_to include('proxy_set_header Host $1.$http_host;')
           expect(content).not_to include('proxy_set_header X-Gitlab-Namespace-In-Path $namespace;')
-          expect(content).not_to include('proxy_redirect ~^https://(projects\.pages\.localhost)/(.*)$ https://$1/$2;')
-          expect(content).not_to include('proxy_redirect ~^https://([^/]*)\.(pages\.localhost)/(.*)$ https://$2/$1/$3;')
-          expect(content).not_to include('proxy_redirect ~^//([^/]*)\.(pages\.localhost)/(.*)$ /$1/$3;')
-          expect(content).not_to include('proxy_redirect ~^/(.*)$ /$namespace/$1;')
+          expect(content).not_to include('proxy_redirect')
           expect(content).to include('proxy_hide_header X-Gitlab-Namespace-In-Path;')
           # Below checks are to verify proper render entries are made
           expect(content).to include('proxy_http_version 1.1;').once
@@ -1024,8 +1021,8 @@
           expect(content).to include('proxy_set_header X-Gitlab-Namespace-In-Path $namespace;')
           expect(content).to include('proxy_redirect ~^(https://customauth\.pages\.localhost/auth)(.*)$ $1$2;')
           expect(content).to include('proxy_redirect ~^https://([^/]*)\.(pages\.localhost)/(.*)$ https://$2/$1/$3;')
-          expect(content).to include('proxy_redirect ~^//([^/]*)\.(pages\.localhost)/(.*)$ /$1/$3;')
-          expect(content).to include('proxy_redirect ~^/(.*)$ /$namespace/$1;')
+          expect(content).to include('proxy_redirect ~^//([^/]*)\.(pages\.localhost)/(.*)$ https://$2/$1/$3;')
+          expect(content).to include('proxy_redirect ~^/(.*)$ https://pages\.localhost/$namespace/$1;')
           expect(content).to include('proxy_hide_header X-Gitlab-Namespace-In-Path;')
           # Below checks are to verify proper render entries are made
           expect(content).to include('proxy_http_version 1.1;').twice