try_files doubleslash mystery

Maik Beckmann beckmann.maik at googlemail.com
Sat Aug 17 08:23:46 UTC 2019


Hi everyone,

I'm putting some hours into better understanding how Nginx works.  While
doing so I came across something that I can't explain nor find anything
about in web-searches.  To reproduce on your Linux machine (I'm using Arch,
if you want to check how nginx was build on their Website)
do the following:

With your normal user account (we are never root in this), enter /tmp or a
place in your home folder where you like to experiment.  Execute these
commands

    - mkdir try_files_test && cd try_files_test
    - mkdir public blog-public client-body fastcgi uwsgi scgi
    - touch nginx.conf
    - echo "does not matter here" > public/index.html
    - echo "Blog" > blog-public/index.html

Put this content into the nginx.conf

## <nginx.conf> ##
pid "nginx.pid";

daemon off;
events {
    worker_connections 1024;
}
error_log /dev/stdout debug;

http {
    client_body_temp_path "client-body";
    fastcgi_temp_path "fastcgi";
    uwsgi_temp_path "uwsgi";
    scgi_temp_path "scgi";
    access_log /dev/stdout;
    rewrite_log on;

    root "public";
    server {
        listen 8080;

        location / {
            return 200 "Homepage\n";
        }

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

and start it with

    - nginx -p $PWD -c nginx.conf

When requesting / via curl, we get "Hompage" as expected.  However, if we
request /blog/ we get "Homepage as well.  For the convinence, here to curl
command

    - curl -i http://localhost:8080/blog/

If we change the try_files line inside /blog's location blog

    try_files $foo $foo/index.html =404;

by removing the $foo/ the curl request returns the intended "Blog".
Instead of putting in the $foo/ again, we just put // there, like this

    try_files $foo // $foo/index.html =404;

and we get "Homepage" for an /blog/ request again.

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.


Thanks in advance for your time and have a good weekend if you're reading
this today.

Regards
Maik Beckmann
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.nginx.org/pipermail/nginx/attachments/20190817/4e14c955/attachment-0001.html>


More information about the nginx mailing list