[nginx] Fixed try_files with alias on case-insensitive systems.

Maxim Dounin mdounin at mdounin.ru
Sat Apr 25 05:47:52 UTC 2026


details:   http://freenginx.org/hg/nginx/rev/27388647e0cd
branches:  
changeset: 9501:27388647e0cd
user:      Maxim Dounin <mdounin at mdounin.ru>
date:      Sat Apr 18 01:44:23 2026 +0300
description:
Fixed try_files with alias on case-insensitive systems.

Previously, checking that try_files with variables matches an aliased
location was done using ngx_strncmp() rather than ngx_filename_cmp().
This works correctly in configurations with $uri, such as:

    location /prefix/ {
        alias /foo/;
        try_files $uri =404;
    }

But this can produce unexpected results on systems with case-insensitive
filesystems, such as Windows and macOS, when configuration-provided
variables are used to construct file names, such as the following
configuration, and assuming a request using different case, such as
"GET /Prefix/":

    location /prefix/ {
        alias /foo/;
        set $maintenance "/prefix/maintenance.html";
        try_files $maintenance $uri =404;
    }

The fix is to use ngx_filename_cmp(), which properly ignores case on
relevant systems.

diffstat:

 src/http/modules/ngx_http_try_files_module.c |  2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

diffs (12 lines):

diff --git a/src/http/modules/ngx_http_try_files_module.c b/src/http/modules/ngx_http_try_files_module.c
--- a/src/http/modules/ngx_http_try_files_module.c
+++ b/src/http/modules/ngx_http_try_files_module.c
@@ -176,7 +176,7 @@ ngx_http_try_files_handler(ngx_http_requ
             *e.pos = '\0';
 
             if (alias && alias != NGX_MAX_SIZE_T_VALUE
-                && ngx_strncmp(name, r->uri.data, alias) == 0)
+                && ngx_filename_cmp(name, r->uri.data, alias) == 0)
             {
                 ngx_memmove(name, name + alias, len - alias);
                 path.len -= alias;


More information about the nginx-devel mailing list