nginx: location, try_files, rewrite: Find pattern match in subfolder, else move on?

Posted by Nick on Server Fault See other posts from Server Fault or by Nick
Published on 2010-09-28T02:40:52Z Indexed on 2011/11/21 9:57 UTC
Read the original article Hit count: 172

Filed under:

I'd like for Nginx to do the following:

If the uri matches the pattern:

http://mysite.com/$string/

and $string is not 'KB', and not 'images', look for $string.html in a specific subfolder. If $string.html exists in the subfolder, return it. If it does not exist, move on to the next matching location.

$string = {any letters, numbers, or dash}



For example, if the user requests:

http://mysite.com/test/

It should look for a file called:

 /webroot/www/myfolder/test.html



I've tried variations of:

location ~ /[a-zA-Z0-9\-]+/
{
   try_files /myfolder/$uri.html @Nowhere;
}

But:

  1. It doesn't seem to find the file even when it does exist, and
  2. If it fails (which is always right now), it wants to jump to the @nowhere location, rather than moving on and trying to find another location that matches.

I'd like for it to consider the current location "not a match" if the file doesn't exist.

© Server Fault or respective owner

Related posts about nginx