'How do I handle redirects when using nginx proxy pass?

I have the following two docker containers. The problem I'm having is when accessing the gitlab it returns a 302. I used insomnia and this is the timeline:

> GET /gitlab HTTP/1.1
> Host: 192.168.158.150
> User-Agent: insomnia/2022.3.0
> Cookie: _gitlab_session=f8989ed639173dad0d881a284165e03d
> Accept: */*

* STATE: DO => DID handle 0x7fe2508f5208; line 2077 (connection #141)
* STATE: DID => PERFORMING handle 0x7fe2508f5208; line 2196 (connection #141)
* Mark bundle as not supporting multiuse
* HTTP 1.1 or later with persistent connection

< HTTP/1.1 302 Found
< Server: nginx/1.21.6
< Date: Mon, 16 May 2022 14:54:15 GMT
< Content-Type: text/html; charset=utf-8
< Content-Length: 102
< Connection: keep-alive
< Cache-Control: no-cache
< Content-Security-Policy: 
< Location: http://192.168.158.150/users/sign_in
< Permissions-Policy: interest-cohort=()
< Pragma: no-cache

It appears the redirect is dropping the port and instead of redirecting to http://192.168.158.150:92/users/sign_in it redirects to http://192.168.158.150/users/sign_in. Does anyone have any ideas on how I can deal with this? This is my nginx.conf:

server {
listen       80;
listen  [::]:80;
server_name localhost;
    location / {
            # Redirects to docker container 1
            set $upstream_app "192.168.158.150";
            set $upstream_port '90';
            set $upstream_proto http;
            proxy_pass $upstream_proto://$upstream_app:$upstream_port;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header Referer $http_referer;
            proxy_set_header X-Forwarded-For $remote_addr;
            proxy_set_header X-Forwarded-Proto $scheme;
    }

location /gitlab {
            # Redirects to gitlab docker container
            set $upstream_app "192.168.158.150";
            set $upstream_port '92';
            set $upstream_proto http;
            proxy_pass "http://192.168.158.150:92";
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header Referer $http_referer;
            proxy_set_header X-Forwarded-For $remote_addr;
            proxy_set_header X-Forwarded-Proto $scheme;
    }
}


Solution 1:[1]

Setting the the external url to match the nginx.conf resolved the problem.

gitlab.rb:

external_url "http://192.168.158.150:92/gitlab"

nginx.conf:

location /gitlab {
            # Redirects to gitlab docker container
            set $upstream_app "192.168.158.150";
            set $upstream_port '92';
            set $upstream_proto http;
            proxy_pass "http://192.168.158.150:92";
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header Referer $http_referer;
            proxy_set_header X-Forwarded-For $remote_addr;
            proxy_set_header X-Forwarded-Proto $scheme;
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 itsOnly1_Jah