Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'd like for Nginx to do the following:

If the uri matches the pattern:$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:

It should look for a file called:


I've tried variations of:

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


  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.

share|improve this question

I think you can try this:

location ~* ^/(KB|images)/$ {
   #rule for KB or images goes here

location ~* ^/([a-zA-Z0-9\-]+)/$ {
   root /webroot/www/myfolder/;
   try_files $1.html @Nowhere;

location @Nowhere {
   #rule for @Nowhere goes here

For more, refer to:

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.