try_files and a nested location regexp

igorb nginx-forum at nginx.us
Thu Oct 16 09:38:13 UTC 2014


I could not figure out why try_files in a nested location defined with a
regexp does not work in nginx/1.4.6 under Ubuntu 14.04.  Consider the
following config:

server {
        listen 8080 default_server;
        root /usr/share/nginx/html;
        autoindex on;

        location /x/ {
                alias /test/;
                location ~ ^/x/test {
                        try_files $uri =404;
                }
        }
}

With /test containing publically readable file test.html and directory
test_dir  this does not works as expected. While localhost:/x/ properly
lists directory context of /test and localhost:/x/test_dir/ is reported as
404 not found , nginx also reported as 404 not found localhost:/x/test.html
even if the file exists.

Now, if replace the regexp with a simple prefix so the location reads:

        location /x/ {
                alias /test/;
                location /x/test {
                        try_files $uri =404;
                }
        }

then everything work. That is, both localhost:/x/ and localhost:/x/test.html
are accessible and only localhost:/x/test_dir/ is 404 not found.

So what is wrong with the usage of try_files in the initial regexp-based
location config?

Posted at Nginx Forum: http://forum.nginx.org/read.php?2,254033,254033#msg-254033



More information about the nginx mailing list