title: Nginx dynamic location path configuration
body: <p>Firstly I want to state that I'm rather new to nginx, I basically only know what I've learned over the last week.</p>
  
  <p>That said, I currently have a Nginx server with a standard configuration of:  </p>
  
  <pre><code>server {
    listen 80;
    server_name site.com;
  
    root /var/www/site.com/;
    index index.php;
  
    location / {
      try_files $uri $uri/ /index.php;
    }
  
    location /microsite/first {
      try_files $uri $uri/ /microsite/first/;
    }
  
    location /microsite/second {
      try_files $uri $uri/ /microsite/second/;
    }
  
    ...
  }
  </code></pre>
  
  <p>This works fine, although for every <em>microsite</em> I add to the existing ones, it requires that a new location be added referring to the path of the new <em>microsite</em>.</p>
  
  <p><strong>My question is:</strong> is it possible to dynamically set the <code>location</code> parameter in a way that it catches and references whatever sub-directory exists within the <strong>microsite/</strong> directory?</p>
  
  <p>e.g. something along the line of the rewrite rule <code>rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 last;</code> (taken from the nginx site) but applied to the <code>location</code> parameter, like:</p>
  
  <pre><code>location ~ ^/microsite/(.*)$ {
    try_files $uri $uri/ /microsite/$1/;
  }
  </code></pre>
  
  <p>In which the <code>$1</code> would catch the sub-directory name passed in <code>(.*)</code>?
  <em>(I tried this snippet that I built refering to the answer to (another) <a href="https://stackoverflow.com/questions/37414123/nginx-dynamic-location-configuration">Nginx dynamic location configuration</a> question, although it did not work)</em></p>
  
  <p>Also I'm not a Regex expert, I've tweaked a bit with it in the past but it was a while ago and I don't recall the precise terminology, so that may be part of the problem, perhaps?!</p>
  
  <p>Anyway, all help is appreciated.<br>
  Thanks in advance!!</p>