title: Nginx reverse proxy subdirectory rewrites for sourcegraph
body: <p>I'm trying to have a self hosted sourcegraph server being served on a subdirectory of my domain using a reverse proxy to add an SSL cert.</p>
  
  <p>The target is to have <a href="http://example.org/source" rel="noreferrer">http://example.org/source</a> serve the sourcegraph server</p>
  
  <p>My rewrites and reverse proxy look like this:</p>
  
  <pre><code>  location /source {
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Scheme $scheme;
  
      rewrite ^/source/?(.*) /$1 break;
      proxy_pass http://localhost:8108;
    }
  </code></pre>
  
  <p>The problem I am having is that upon calling <a href="http://example.org/source" rel="noreferrer">http://example.org/source</a> I get redirected to <a href="http://example.org/sign-in?returnTo=%2F" rel="noreferrer">http://example.org/sign-in?returnTo=%2F</a></p>
  
  <p>Is there a way to rewrite the response of sourcegraph to the correct subdirectory?</p>
  
  <p>Additionally, where can I debug the rewrite directive? I would like to follow the changes it does to understand it better.</p>
  
  <p>-- Edit:</p>
  
  <p>I know my approach is probably wrong using rewrite and I'm trying the sub_filter module right now.</p>
  
  <p>I captured the response of sourcegraph using tcpdump and analyzed using wireshark so I am at:</p>
  
  <pre><code>GET /sourcegraph/ HTTP/1.0
  Host: 127.0.0.1:8108
  Connection: close
  Upgrade-Insecure-Requests: 1
  DNT: 1
  User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36
  Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
  Referer: https://example.org/
  Accept-Encoding: gzip, deflate, br
  Accept-Language: de,en-US;q=0.9,en;q=0.8
  Cookie: sidebar_collapsed=false; 
  
  HTTP/1.0 302 Found
  Cache-Control: no-cache, max-age=0
  Content-Type: text/html; charset=utf-8
  Location: /sign-in?returnTo=%2Fsourcegraph%2F
  Strict-Transport-Security: max-age=31536000
  Vary: Cookie
  X-Content-Type-Options: nosniff
  X-Frame-Options: DENY
  X-Trace: #tracer-not-enabled
  X-Xss-Protection: 1; mode=block
  Date: Sat, 07 Jul 2018 13:59:06 GMT
  Content-Length: 58
  
  &lt;a href="/sign-in?returnTo=%2Fsourcegraph%2F"&gt;Found&lt;/a&gt;.
  </code></pre>