title: how to issue a relative url redirect from nginx?
body: <p>How can a redirect be configured in nginx to respond to a certain url with a relative redirect to a specific path?</p>
  
  <p>The <a href="https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/#rewrite-missing-http" rel="noreferrer">nginx documentation</a> suggests that this is the default mode for nginx, but in reality if the location being redirected to starts with a / nginx is responding with an absolute url in the Location field.</p>
  
  <p>For local server configuration containing locations:</p>
  
  <pre><code>location = /a {
    return 301 some/path;
  }
  location = /b {
    return 301 /qwerty;
  }
  </code></pre>
  
  <p>the response Location to a request to /a is:</p>
  
  <pre><code>Location: some/path
  </code></pre>
  
  <p>and for /b it is:</p>
  
  <pre><code>Location: http://127.0.0.1/qwerty
  </code></pre>
  
  <p>however, we would like /b to respond with:</p>
  
  <pre><code>Location: /qwerty
  </code></pre>
  
  <p>The reason we would like to use relative redirects is that we would like to access nginx from different domains and proxies, e.g. in clear in dev or via ssl-terminating load-balancer, and would rather keep things simple by relieving nginx of needing to understand that context.</p>
  
  <p>FYI these examples were tested against nginx versions 1.4.6 and 1.9.6, using curl e.g.:</p>
  
  <pre><code>curl --head http://127.0.0.1/b
  </code></pre>