title: nginx location with unknown subfolder depth
body: <p>I would like to have a custom location for images in order to set a better <code>expires</code> value and remove them from <code>access.log</code>.</p>
  
  <p>The local path for an image would be sth like this <code>/var/www/html/public/image/user/{username}/{?subfolder}/filename.jpg</code> </p>
  
  <p>There can be one or more subfolders where the image is located.</p>
  
  <p>The url should look like <code>http://example.com/image/user/{username}/{?subfolder}/filename.jpg</code>. Since there can be different folders to <code>user</code> with a different folder depth, I cannot hardcode it.</p>
  
  <p>I tried it with this location rule, but nginx tries to locate it in the default root and not in the specified one. </p>
  
  <pre><code>location /image/[^\\]+/.*\.(jpg|jpeg|gif|png|css|ico)$ {
      root /var/www/html/public/image;
      expires           30d;
  }
  </code></pre>
  
  <p>I tried the expression against a PCRE tester, and it was successful. Appreciate any help with this.</p>