title: Nginx convert subdomain to path component without redirect
body: <p>The idea is to take incoming requests to <code>http://abc.example.com/...</code> and rewrite them to <code>http://example.com/abc/...</code></p>
  
  <p>That's easy enough to do with a 301/302 redirect:</p>
  
  <pre><code># rewrite via 301 Moved Permanently
  server {
    listen 80;
    server_name abc.example.com;
    rewrite ^ $scheme://example.com/abc$request_uri permanent;
  }
  </code></pre>
  
  <p>The trick is to do this URL change <strong>transparently</strong> to the client when <code>abc.example.com</code> and <code>example.com</code> point at the same Nginx instance. </p>
  
  <p>Put differently, can Nginx serve the contents from <code>example.com/abc/...</code> when <code>abc.example.com/...</code> is requested and <strong>without another client round trip</strong>?</p>
  
  <p><strong>Starting Point Config</strong></p>
  
  <p>Nginx config that accomplishes the task with a 301:</p>
  
  <pre><code># abc.example.com
  server {
    listen 80;
    server_name abc.example.com;
    rewrite ^ $scheme://example.com/abc$request_uri permanent;
  }
  
  # example.com
  server {
    listen 80;
    server_name example.com;
    location / { 
      # ...
    }
  }
  </code></pre>