1

I have a regexp location for a subdirectory (/foo) and want Nginx to do this for a request to /foo/bar:

  1. Check if bar is a file or directory in the root defined in the location. If it is a directory, check for the index file.
  2. If there is no file or directory bar fallback to the given URI (there is another working handler for .php files).

It looks like this:

location ~ ^/foo(/.*)?$ {
  root /my/root/with/files;
  index index.php;
  try_files $1 $1/ /my/root/with/files/index.php?$args;
}

Should be simple, but I can't get this working, and the documentation does not help. I tried many combinations of alias, root and whatever.

Any suggestions, how to get this working? Or do you know where to get useful documentation about try_files, root, alias, locations (including notes about outstanding bugs)?

2
  • I would recommend moving your root and index directives outside of the location as suggested in nginx pitfalls. Commented Oct 14, 2014 at 14:08
  • Generaly yes, but this root and index belong (in my case) to this specific location (or application on this host). I first put them to a nested location, but as said, nested locations and Nginx are considered evil ;)
    – Christian
    Commented Oct 15, 2014 at 7:35

1 Answer 1

1

I got rid of most of my regexp and nested locations (seem to be buggy, especially in combination with the alias and try_files directive), and now it works like this:

location /foo/ {
    alias /my/root/with/files/;
    index index.php;
    try_files $uri $uri/ /foo//foo/index.php?$args;
}

The last argument to try_files seems odd, but the alias directive in combination with the location will replace the first /foo/.

Lesson learned: Keep things as simple as possible (forget about elegance in your configuration ;) ) in Nginx or it will blow your mind.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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