title: nginx reverse proxy to different applications based on an index in the host name
body: <p>Previously, I had a single staging environment reachable behind a DNS <code>staging.example.com/</code>. Behind this address is a nginx proxy with the following config. Note that my proxy either redirects</p>
  
  <ul>
  <li>To a (s3 behind) cloudfront distribution (app1)</li>
  <li>To a loadbalancer by forwarding the host name (and let's consider my ALB is able to pick the appropriate app based on the host name)  (app2)</li>
  </ul>
  
  <pre><code>server {
    listen 80;
    listen 443 ssl;
    server_name
        staging.example.com
    ;
  
    location / {
      try_files /maintenance.html @app1;
    }
  
    location ~ /(faq|about_us|terms|press|...) {
        try_files /maintenance.html @app2;
    }
  
    [...] # Lots of similar config than redirects either to app1 or app2
  
    # Application hosted on s3 + CloudFront
    location @app1 {
      proxy_set_header Host app1-staging.example.com;
      proxy_pass http://d2c72vkj8qy1kv.cloudfront.net;
    }
  
    # Application hosted behind a load balancer
    location @app2 {
      proxy_set_header Host app2-staging.example.internal;
      proxy_set_header X-ALB-Host $http_host;
      proxy_pass https://staging.example.internal;
    }
  }
  </code></pre>
  
  <p>Now, my team needs a couple more staging environments. We are not yet ready to transition to docker deployments (the ultimate goal of being able to spawn a complete infra per branch that we need to test... is a bit overkill given our team size) and I'm trying to pull out some tricks instead so we can easily get a couple more staging environments <strong>using roughly the same nginx config</strong>.</p>
  
  <p>Assume I have a created a few more DNS names with a <code>index_i</code> like <code>staging1.example.com</code>, <code>staging2.example.com</code>. So my nginx proxy will receive requests with a host header that looks like <code>staging#{index_i}.example.com</code></p>
  
  <p>What I'm thinking of doing :</p>
  
  <ul>
  <li>For my s3 + Cloudfront app, I'm thinking of nesting my files under <code>[bucket_id]/#{index_i}/[app1_files]</code> (previously they were directly in the root folder <code>[bucket_id]/[app1_files]</code>)</li>
  <li>For my load balancer app, let's assume my load balancer knows where to dispatch <code>https://staging#{iindex_i}.example.com</code> requests.</li>
  </ul>
  
  <p>I'm trying to pull something like this</p>
  
  <pre><code># incoming host : staging{index_i}.example.com`
  server {
    listen 80;
    listen 443 ssl;
    server_name
        staging.example.com
        staging1.example.com 
        staging2.example.com # I can list them manually, but is it possible to have something like `staging*.example.com` ?
    ;
  [...]
  location @app1 {
      proxy_set_header Host app1-staging$index_i.example.com; # Note the extra index_i here
      proxy_pass http://d2c72vkj8qy1kv.cloudfront.net/$index_i; # Here proxy_passing to a subfolder named index_i
    }
  
    location @app2 {
      proxy_set_header Host app2-staging$index_i.example.internal; # Note the extra index_i here
      proxy_set_header X-ALB-Host $http_host;
      proxy_pass http://staging$index_i.example.internal; # Here I am just forwarding the host header basically
    }
  </code></pre>
  
  <p>So ultimately my questions are
   - When my nginx server receives a connexion, can I extract the <code>index_i</code> variable from the request host header (using maybe some regex ?)
   - If yes, how can effectively implement the app1 and app2 blocks with <code>index_i</code> ?</p>