try_files doubleslash mystery

Francis Daly francis at daoine.org
Sat Aug 17 18:04:23 UTC 2019


On Sat, Aug 17, 2019 at 10:23:46AM +0200, Maik Beckmann via nginx wrote:

Hi there,

>         location  /blog {
>             root "blog-public";
>             set $foo /;
>             try_files $foo $foo/ $foo/index.html =404;
>         }

> When requesting / via curl, we get "Hompage" as expected.  However, if we
> request /blog/ we get "Homepage as well. 

> Now my Question:  Is there something about double slash as the $uri that
> causes nginx to do a magical internal redirect?  I don't understand.

It is not "double slash". It is "the argument to try files (before
variable expansion) ends in slash".

https://nginx.org/r/try_files

"""
The path to a file is constructed from the file parameter according to
the root and alias directives. It is possible to check directory’s
existence by specifying a slash at the end of a name, e.g. “$uri/”.
"""

$foo does not end in slash, so try_files looks for a file of that
(expanded) name, and fails to find it.

$foo/ does end in slash, so try_files looks for a directory of that
(expanded) name, and finds it and serves it (which involves a
subrequest/internal redirect).

	f
-- 
Francis Daly        francis at daoine.org


More information about the nginx mailing list