# Oauth2 integration with gitlab when sonar is running behind an nginx proxy

**URL:** <https://community.sonarsource.com/t/oauth2-integration-with-gitlab-when-sonar-is-running-behind-an-nginx-proxy/36990>\
**Category:** SonarQube Server / Community Build\
**Created:** [January 13, 2021, 10:19pm UTC](https://community.sonarsource.com/t/oauth2-integration-with-gitlab-when-sonar-is-running-behind-an-nginx-proxy/36990 "2021-01-13T22:19:12Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![knolish](https://avatars.discourse-cdn.com/v4/letter/k/4491bb/32.png) [@knolish](https://community.sonarsource.com/u/knolish)\
**Post date:** [January 13, 2021, 10:19pm UTC](https://community.sonarsource.com/t/oauth2-integration-with-gitlab-when-sonar-is-running-behind-an-nginx-proxy/36990/1 "2021-01-13T22:19:12Z")

</div>

This refers to SonarQube 8.6 developer edition.

I’m running sonar behind an NGINX proxy in order to use https:. That works. I’m now attempting to integrate sonarqube with our gitlab server for authentication, group sharing, etc…

I followed the instructions to set up the application on both gitlab and sonar.

When I attempt to sign in using my gitlab account, I receive the “Redirect URI included is not valid”

I pulled the gitlab logs for the request and found the following:

“GET /oauth/authorize?response\_type=code&client\_id=\<TOKEN\_OMITTED\>&redirect\_uri=http%3A%2F%2Flocalhost%3A9000%2Foauth2%2Fcallback%2Fgitlab&scope=read\_user&state=f2gfcctjlnt1kbk59og3felnqu HTTP/2.0” 200 8312 “[https://sonar.powercastco.com/sessions/new](https://sonar.powercastco.com/sessions/new)” “Mozilla/5.0 (X11; Linux x86\_64; rv:78.0) Gecko/20100101 Firefox/78.0” 3.80

While I’m not sure what the %3A stuff is, the redirect\_uri as reported by gitlab seems to be  
[http://localhost:9000/](http://localhost:9000/)

This is the port that sonar itself is executing on, locally behind the nginx proxy.

Can someone who has successfully configured sonar behind an nginx proxy and gotten gitlab oauth2 to work done this post their nginx.conf file that they use on their sonar server? I suspect that the problem is with that because that’s the place where [https://sonar.myhost/](https://sonar.myhost/) is mapped to [http://localhost:9000](http://localhost:9000).

---

<div class="post-metadata">

**Author:** ![knolish](https://avatars.discourse-cdn.com/v4/letter/k/4491bb/32.png) [@knolish](https://community.sonarsource.com/u/knolish)\
**Post date:** [January 13, 2021, 11:25pm UTC](https://community.sonarsource.com/t/oauth2-integration-with-gitlab-when-sonar-is-running-behind-an-nginx-proxy/36990/2 "2021-01-13T23:25:02Z")

</div>

I have some further analysis.

In my setting, sonarqube thinks it is executing at [http://locahost:9000](http://locahost:9000). From the outside, however, it should be executing at [https://sonar.powercastco.com/](https://sonar.powercastco.com/). What it puts in its GET request is  
redirect\_uri=“[http://localhost:9000/oauth2/callback/gitlab](http://localhost:9000/oauth2/callback/gitlab)” because that’s what it thinks the URL ought to be.

So, my guess is that there is some setting in the configuration file that ought to tell sonar that from the perspective of the outside world it’s executing at [https://sonar.powercastco.com](https://sonar.powercastco.com)[:443]/ and that’s what it should be using in the oauth parameters.

What would this setting be?

---

<div class="post-metadata">

**Author:** ![pierreguillot](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/pierreguillot/32/46022_2.png) [@pierreguillot](https://community.sonarsource.com/u/pierreguillot)\
**Post date:** [January 14, 2021, 8:16am UTC](https://community.sonarsource.com/t/oauth2-integration-with-gitlab-when-sonar-is-running-behind-an-nginx-proxy/36990/3 "2021-01-14T08:16:30Z")

</div>

Hi, this settings is accessible in SonarQube with admin permission, in Administration-\>configuration-\>general, and is named “Server Base URL” (sonar.core.serverBaseURL).

---

<div class="post-metadata">

**Author:** ![knolish](https://avatars.discourse-cdn.com/v4/letter/k/4491bb/32.png) [@knolish](https://community.sonarsource.com/u/knolish)\
**Post date:** [January 15, 2021, 4:45pm UTC](https://community.sonarsource.com/t/oauth2-integration-with-gitlab-when-sonar-is-running-behind-an-nginx-proxy/36990/4 "2021-01-15T16:45:19Z")

</div>

Thank you. That got me to the next problem. This post will probably make me look foolish, but I’ve added all of the mistakes that I made in the hope that this checklist will help somebody else.

1. Ensure that the sonar system and the gitlab system recognize each other’s SSL certificates. Note that sonar is a java application so you need to add the certificate to the JVM key store in addition to the system’s key store used for web browsers and wget.

2. Ensure that the NGINX settings are proper. Right now the settings that I’m using are:

With these settings, where things stand are:

- The sonar log in page shows the gitlab login as an option
- When I click on the gitlab button, I’m redirected to the gitlab login page
- When I log into gitlab, I bounce back to a not authorized page in sonar.
- When I browse back to gitlab, I find that I’m logged into gitlab.

So…

- SonarQube is talking to gitlab, at least to the point where it goes to a gitlab login page.
- I can successfully log into gitlab
- Once I log into gitlab, something goes wrong with the return OAUTH2 process for actually logging into sonarqube.

---

<div class="post-metadata">

**Author:** ![knolish](https://avatars.discourse-cdn.com/v4/letter/k/4491bb/32.png) [@knolish](https://community.sonarsource.com/u/knolish)\
**Post date:** [January 15, 2021, 8:51pm UTC](https://community.sonarsource.com/t/oauth2-integration-with-gitlab-when-sonar-is-running-behind-an-nginx-proxy/36990/5 "2021-01-15T20:51:27Z")

</div>

The lastest failure is accompanied with a stack trace in the web.log file. The error is:

2021.01.15 20:44:54 WARN web[AXcHyxc+YhHC0KS5AAAP][o.s.s.a.AuthenticationError] Fail to callback authentication with ‘gitlab’  
java.lang.IllegalStateException: java.io.IOException: Unable to tunnel through proxy. Proxy returns “HTTP/1.1 400 Bad Request”  
at org.sonar.auth.gitlab.GitLabIdentityProvider.callback(GitLabIdentityProvider.java:104)

So what is going on is an interaction between the oauth2 process and the proxy support.

---

<div class="post-metadata">

**Author:** ![pierreguillot](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/pierreguillot/32/46022_2.png) [@pierreguillot](https://community.sonarsource.com/u/pierreguillot)\
**Post date:** [January 18, 2021, 9:56am UTC](https://community.sonarsource.com/t/oauth2-integration-with-gitlab-when-sonar-is-running-behind-an-nginx-proxy/36990/6 "2021-01-18T09:56:27Z")

</div>

> [@knolish](#):
>
> When I log into gitlab, I bounce back to a not authorized page in sonar.

At this specific step, do you have any interesting information in your browser inspector? You want to look at parameters, headers, to find maybe an incorrect redirect URL or a weird payload.

You can also increase log verbosity to DEBUG in SonarQube, to catch more information about the steps that fails.

---

<div class="post-metadata">

**Author:** ![knolish](https://avatars.discourse-cdn.com/v4/letter/k/4491bb/32.png) [@knolish](https://community.sonarsource.com/u/knolish)\
**Post date:** [January 20, 2021, 3:57pm UTC](https://community.sonarsource.com/t/oauth2-integration-with-gitlab-when-sonar-is-running-behind-an-nginx-proxy/36990/7 "2021-01-20T15:57:11Z")

</div>

Sonarqube’s web.log shows that the login failed because the proxy returned a 400. So I believe the error is coming from the copy of NGINX running on my sonar host that is proxying connections behind an https connection, running on the default port 443.

I examined the NGINX logs for the nginx instance running on my gitlab server and it never generated an error, so believe this is confirming evidence that isolates the problem to the nginx server running on the sonar system.

I enabled NGINX error debugging and this is the relevant request where things started to go off the rails.

http upstream request: “/oauth2/callback/gitlab?code=653a8397bb24e75130f57956585371c4574f8b95ba65c42bb288f4816273317a&state=pl9m22qi8otbse69tarbtokpfq”

… elided a bunch of debugging traces that seemed to be tracing the setup of the HTTP request  
for the proxy …

client sent invalid request while reading client request line, client: 192.168.0.4, server: , request: “CONNECT [gitlab.powercastco.com:443](http://gitlab.powercastco.com:443) HTTP/1.1”

In this case, 192,168.0.4 is the IP address of our sonar server, so this seems to be an outgoing request from “localhost” to [gitlab.powercastco.com](http://gitlab.powercastco.com), port 443.

2021/01/20 15:37:25 [debug] 3833#3833: \*769 HTTP/1.1 400 Bad Request  
Server: nginx/1.14.0 (Ubuntu)  
Date: Wed, 20 Jan 2021 15:37:25 GMT  
Content-Type: text/html  
Content-Length: 182  
Connection: close

I agree that the problem is likely a proxy setting of some form.
