title: Why are these Swagger endpoints behind a reverse-proxy not found?
body: <p>I have a small number of ASP.NET Core services, all running in Docker (via Docker Compose). All services are currently using a prefixed route (their own service name). And they're all setup in Docker Compose to use their own service name as their hostname (connectivity between service containers is OK).</p>
  
  <p>The <code>/api-docs</code> endpoint is provided by <a href="https://github.com/domaindrivendev/Swashbuckle.AspNetCore" rel="nofollow noreferrer">Swashbuckle</a>; we setup the prefixed route here, too.</p>
  
  <pre><code>app.UseSwagger(options =&gt;
  {
      options.RouteTemplate = "scheduler/api-docs/{documentName}/swagger.json";
  });
  
  app.UseSwaggerUI(options =&gt;
  {
      options.RoutePrefix = "scheduler/api-docs";
      options.SwaggerEndpoint("/scheduler/api-docs/v1/swagger.json", "Scheduler API v1");
  });
  </code></pre>
  
  <p>I am trying to configure an Nginx reverse-proxy in the container network so that I can go to, say...</p>
  
  <pre><code>http://localhost/&lt;service-name&gt;/api-docs
  </code></pre>
  
  <p>and it will redirect, inside the container network, to...</p>
  
  <pre><code>http://&lt;service-name&gt;:5000/&lt;service-name&gt;/api-docs
  </code></pre>
  
  <p>So, here's the Nginx configuration I've come up... basically, match the first part of the request URI, which should be the service name, and proxy to a host called the same and Nginx <em>should</em> add the <code>$request_uri</code> on automatically.</p>
  
  <pre><code>server {
      listen 80;
  
      location ~* ^/(?&lt;target&gt;.+)/ {
          proxy_pass http://$target:5000;
          proxy_redirect off;
          resolver 127.0.0.11;
      }
  }
  </code></pre>
  
  <p>Here's what I get for a <code>/scheduler/healthcheck</code> endpoint. All good!</p>
  
  <pre><code>api-gateway_1               | 172.19.0.1 - - [27/Mar/2018:17:50:24 +0000] "GET /scheduler/healthcheck HTTP/1.1" 200 491 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36" "-"
  </code></pre>
  
  <p>But, when I try to go to <code>/scheduler/api-docs</code>, I have problems. We get to the service container and Swashbuckle does a 301 Redirect from <code>/scheduler/api-docs</code> to <code>/scheduler/api-docs/</code>.</p>
  
  <pre><code>api-gateway_1               | 172.19.0.1 - - [27/Mar/2018:17:51:18 +0000] "GET /scheduler/api-docs HTTP/1.1" 301 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36" "-"
  </code></pre>
  
  <p>And, then, things go bad... we "lose" the <code>/scheduler</code> part of the route!</p>
  
  <pre><code>api-gateway_1               | 172.19.0.1 - - [27/Mar/2018:17:51:18 +0000] "GET /scheduler/api-docs/ HTTP/1.1" 502 576 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36" "-"
  api-gateway_1               | 2018/03/27 17:51:18 [error] 5#5: *2 connect() failed (111: Connection refused) while connecting to upstream, client: 172.19.0.1, server: , request: "GET /scheduler/api-docs/ HTTP/1.1", upstream: "http://172.19.0.5:80/api-docs:5000", host: "localhost:4000"
  </code></pre>
  
  <p>Why does the Swashbuckle redirect send the request back through Nginx, I thought this would all be handled by the local service, and why is Nginx stripping the necessary route prefix from this request?</p>
  
  <p>How do I get this to behave?!</p>
  
  <hr>
  
  <p>I tried to reconstruct the "whole" URI, just to see what happens...</p>
  
  <pre><code>-   proxy_pass http://$target:5000;
  +   proxy_pass http://$target:5000$request_uri;
  </code></pre>
  
  <p>And that got <em>even</em> worse!</p>
  
  <pre><code>api-gateway_1               | 172.19.0.1 - - [27/Mar/2018:18:03:48 +0000] "GET /scheduler/api-docs HTTP/1.1" 301 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36" "-"
  api-gateway_1               | 172.19.0.1 - - [27/Mar/2018:18:03:48 +0000] "GET /scheduler/api-docs/ HTTP/1.1" 502 576 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36" "-"
  api-gateway_1               | 2018/03/27 18:03:48 [error] 5#5: *2 connect() failed (111: Connection refused) while connecting to upstream, client: 172.19.0.1, server: , request: "GET /scheduler/api-docs/ HTTP/1.1", upstream: "http://172.19.0.5:80/api-docs:5000/scheduler/api-docs/", host: "localhost:4000"
  </code></pre>
  
  <hr>
  
  <p>FYI, everything works fine in my browser if I visit the sites directly (after publishing the ports via Docker, etc.)</p>