title: nginx url redirect from query parameter
body: <p>Hi I am using oauth on a number of sub domains I control. To handle multiple sub domains that all need authentication I am using oauth2_proxy (<a href="https://github.com/bitly/oauth2_proxy" rel="nofollow">https://github.com/bitly/oauth2_proxy</a>) in a Docker container. My web apps are also in docker containers. Everything works when I turn auth off.</p>
  
  <p>The inspiration for the below nginx configuration where the redirect should occur came from <a href="https://github.com/18F/hub/blob/master/deploy/SSO.md" rel="nofollow">https://github.com/18F/hub/blob/master/deploy/SSO.md</a></p>
  
  <p>It all works for the first domain, but the second gets forwarded to the first. I don't think the redirect is configured correctly?</p>
  
  <p>I think I'm close!</p>
  
  <pre><code>upstream dashboard.example.com {
              # dashboard
              server 172.17.0.6:9000;
  }
  server {
      gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
  
      server_name dashboard.example.com;
      proxy_buffering off;
      error_log /proc/self/fd/2;
      access_log /proc/self/fd/1;
  
      location = /oauth2/start {
        proxy_pass http://172.17.0.4:4180/oauth2/start?rd=%2F$server_name$arg_rd;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Scheme $scheme;
        proxy_connect_timeout 1;
        proxy_send_timeout 30;
        proxy_read_timeout 30;
      }
      location / {
          proxy_pass http://172.17.0.4:4180/;
          proxy_set_header Host $host;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Scheme $scheme;
          proxy_connect_timeout 1;
          proxy_send_timeout 30;
          proxy_read_timeout 30;
      }
  }
  upstream internal.example.com {
              # wiki
              server 172.17.0.5:5000;
  }
  server {
      gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
      server_name internal.example.com;
      proxy_buffering off;
      error_log /proc/self/fd/2;
      access_log /proc/self/fd/1;
  
      location = /oauth2/start {
        proxy_pass http://172.17.0.4:4180/oauth2/start?rd=%2F$server_name$arg_rd;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Scheme $scheme;
        proxy_connect_timeout 1;
        proxy_send_timeout 30;
        proxy_read_timeout 30;
      }
      location / {
          proxy_pass http://172.17.0.4:4180/;
          proxy_set_header Host $host;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Scheme $scheme;
          proxy_connect_timeout 1;
          proxy_send_timeout 30;
          proxy_read_timeout 30;
      }
  }
  
  server {
      server_name auth.example.com;
      location = /oauth2/callback {
          proxy_pass http://172.17.0.4:4180;
          proxy_connect_timeout 1;
          proxy_send_timeout 30;
          proxy_read_timeout 30;
      }
      location = /oauth2/start {
          proxy_pass http://172.17.0.4:4180;
          proxy_connect_timeout 1;
          proxy_send_timeout 30;
          proxy_read_timeout 30;
      }
      location "~^/(?&lt;target_host&gt;[^/]+).example.com/(?&lt;remaining_uri&gt;.*)$" {
          rewrite ^ $scheme://$target_host.example.com/$remaining_uri;
      }
  
      location / {
          deny all;
      }
  }
  </code></pre>