try_files doubleslash mystery

Maik Beckmann beckmann.maik at
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

    - 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 "";

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

    - 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.

Maik Beckmann
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the nginx mailing list