Christian
Christian

Reputation: 4249

try_files, alias, and regexp locations

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)?

Upvotes: 1

Views: 2432

Answers (1)

Christian
Christian

Reputation: 4249

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.

Upvotes: 1

Related Questions