title: R/Plumber: Rerouting Swagger UI through Nginx Reverse Proxy
body: <p><strong>Background</strong></p>
  
  <p>I have a remote server on which an R API service is running by the use of Plumber. It is fully operational in the sense that I can call my service externally. </p>
  
  <p>I am using an Ubuntu/Nginx reverse proxy setup. The Plumber service is listening to port 1163. I have subsequently implemented the following Nginx rerouting such that the API listens to <a href="https://server.net/API/" rel="nofollow noreferrer">https://server.net/API/</a>:</p>
  
  <pre><code>location /API/ {
   proxy_pass http://localhost:1163/;
   proxy_redirect http://localhost:1163/ $scheme://$host/;
  }
  </code></pre>
  
  <p>This is working fine. Now, by default, the Swagger UI listens to </p>
  
  <pre><code>http://127.0.0.1:1163/__swagger__/
  </code></pre>
  
  <p>which, owing to the config above, means that I can successfully access the Swagger UI at </p>
  
  <pre><code>https://server.net/API/__swagger__/
  </code></pre>
  
  <p><strong>Issue</strong></p>
  
  <p>I want to configure Nginx such that the Swagger UI listens to <a href="https://server.net/API/documentation/" rel="nofollow noreferrer">https://server.net/API/documentation/</a>. I have attempted the following nested Nginx configuration:</p>
  
  <pre><code>location /API/ {
   proxy_pass http://localhost:1163/;
   proxy_redirect http://localhost:1163/ $scheme://$host/;
  
   location /API/documentation/ {
   proxy_pass http://localhost:1163/__swagger__/;
   proxy_redirect http://localhost:1163/__swagger__/ $scheme://$host/;
   }
  
  }
  </code></pre>
  
  <p>Now, while entering the desirable URL <a href="https://server.net/API/documentation/" rel="nofollow noreferrer">https://server.net/API/documentation/</a> into my browser, I retrieve the Swagger interface, however it returns the following error code: </p>
  
  <p><a href="https://i.stack.imgur.com/OwSlR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/OwSlR.png" alt="Swagger_error"></a></p>
  
  <p>Evidently, the incorrect swagger URL has been parsed during the Nginx redirection. This is clear considering that the url should state </p>
  
  <pre><code>https://server.net/API/swagger.json?schemes=https&amp;host=server.net&amp;path=/API/
  </code></pre>
  
  <p>instead of </p>
  
  <pre><code>http://petstore.swagger.io/v2/swagger.json
  </code></pre>
  
  <p>How can I ensure that the correct URL is parsed to the Swagger UI during the Nginx redirection? All the best, J.</p>