title: Infinite loop after logging in with Keycloak behind a reverse proxy
body: <p><strong>My scenario</strong></p>
  
  <p>I have a server with IP x.x.x.x</p>
  
  <p>My web application is running on port 8083 of the server.</p>
  
  <p>Keycloak (deployed with docker) is running on port 8080 of the server.</p>
  
  <p>I want to setup nginx so that when I visit <code>my-website.com</code>, it will redirect to my web app at <code>x.x.x.x:8083</code>, and when I visit <code>my-website.com/auth</code>, it will redirect to Keycloak at <code>x.x.x.x:8080</code>. I also use SSL for my website.</p>
  
  <p>Here's my nginx ssl.conf file</p>
  
  <pre><code>upstream upstream_auth {
      server 127.0.0.1:8080;
  }
  
  server {
      listen 80;
      listen [::]:80;
      server_name my-website.com www.my-website.com;
      return 301 https://my-website.com;
  }
  
  server {
  
      listen       443;
      ssl          on;
      server_name my-website.com;
  
      ssl_certificate      /etc/ssl/my-website.crt;
      ssl_certificate_key  /etc/ssl/my-website.key;
  
      ssl_protocols        TLSv1 TLSv1.1 TLSv1.2;
  
      ssl_prefer_server_ciphers on;
      ssl_dhparam /etc/nginx/dhparams.pem;
  
      keepalive_timeout    60;
      ssl_session_cache    shared:SSL:10m;
      ssl_session_timeout  10m;
  
  
      root /var/lib/tomcat7/webapps/ROOT/;
      client_max_body_size 50M;
  
      location / {
          proxy_pass  http://127.0.0.1:8083/;
          proxy_set_header X-Forwarded-Host $host;
          proxy_set_header X-Forwarded-Server $host;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header X-Forwarded-Ssl on;
          proxy_set_header X-Forwarded-Proto $scheme;
      }
      location /auth/{
         proxy_pass  http://upstream_auth;
         proxy_http_version 1.1;
         proxy_set_header URI_REQUEST_ORIGIN $request_uri;
         proxy_set_header Host            $host;
         proxy_set_header X-Real-IP       $remote_addr;
         proxy_set_header X-Forwarded-Server $host;
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         proxy_set_header X-Forwarded-Ssl on;
         proxy_set_header X-Forwarded-Proto $scheme;
  
        #proxy_pass_header Set-Cookie;
  
        proxy_set_header Connection **;
        proxy_set_header Proxy **;
      }
  }
  </code></pre>
  
  <p><strong>My problem:</strong></p>
  
  <p>When I visit <code>my-website.com/auth</code>, it takes me to <a href="https://i.stack.imgur.com/meB2Y.png" rel="nofollow noreferrer">keycloak landing page</a> (as I expect). Then I click on Administrator Console and then login to Master Realm of Keycloak, the browser seems to stuck in an infinite loop of <code>https://my-website.com/auth/admin/master/console/</code> and <code>https://my-website.com/auth/admin/master/console/#state={constantly-changing-string}&amp;session_state=xxx&amp;code={another-constantly-changing-string}</code>.</p>
  
  <p>I've been searching for solution and there seems to be many reasons that may cause this happen. </p>
  
  <p>What am I doing wrong here?</p>