Score:0

NGINX location rewrite for Gitlab

pk flag

I'm trying to make Gitlab work behind Nginx. my configs:

Nginx:

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

location /gitlab/ {
       rewrite ^/gitlab(/.*)$ $1 break;
       client_max_body_size 0;
       gzip off;
       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-Ssl     on;
       proxy_set_header    X-Forwarded-For     $proxy_add_x_forwarded_for;
       proxy_set_header    X-Forwarded-Proto   $scheme;
       proxy_pass http://gitlab-workhorse;
}

/etc/gitlab/gitlab.rb:

external_url "https://myhostname.com/gitlab"
web_server['external_users'] = ['www-data']
nginx['enable'] = false
gitlab_workhorse['enable'] = true
gitlab_workhorse['listen_network'] = "unix"
gitlab_workhorse['listen_addr'] = "/var/opt/gitlab/gitlab-workhorse/sockets/socket"

at request to myhost.com/gitlab/users/sign_in I get "/users/sign_in" not found

if I change in gitlab configuration

external_url "https://myhostname.com/"

(without /gitlab path in the end) it starts to work but it's always redirects to the / of the server instead of /gitlab

Score:0
gr flag

Since you already configured GitLab to use an URI prefix /gitlab with the

external_url "https://myhostname.com/gitlab"

(which is the only proper way to host anything bigger that a SPA under an URI prefix), you don't need to strip that /gitlab prefix from the request URI before passing the request to the GitLab backend. Remove that rewrite ^/gitlab(/.*)$ $1 break; line from your nginx config. What made you think it is needed at all?

Stefan NovakDev avatar
pk flag
apparently gitlab does not support relative urls
Ivan Shatsky avatar
gr flag
What makes you think so? The documentation [states](https://docs.gitlab.com/omnibus/settings/configuration.html#configure-a-relative-url-for-gitlab) exactly the opposite. Did you test your enviroment without the `rewrite` directive using the `external_url "https://myhostname.com/gitlab"`?
Stefan NovakDev avatar
pk flag
yeah thank you it actually helped!
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.