[PATCH 4 of 7] Support for "try_files $uri ..." in regex locations with alias

Maxim Dounin mdounin at mdounin.ru
Fri Apr 17 22:45:39 UTC 2026


# HG changeset patch
# User Maxim Dounin <mdounin at mdounin.ru>
# Date 1776465866 -10800
#      Sat Apr 18 01:44:26 2026 +0300
# Node ID 4c206d68284ead757da213f8ff2a55e960b64a4a
# Parent  aad0fdcc2e33df5ae75f6753028b1bf51bcf4d87
Support for "try_files $uri ..." in regex locations with alias.

Previously, only adding extensions were supported by try_files when used
in a regex location with alias, such as in (3534:3711bb1336c3):

    location ~ /files/(.*) {
        alias /path/to/$1;
        try_files .html "" / =404;
    }

With this change, try_files now able to properly handle configurations
with $uri being used, similarly to other configurations with try_files,
such as in:

    location ~ /files/(.*) {
        alias /path/to/$1;
        try_files $uri.html $uri $uri/ =404;
    }

Note though that this change will break configurations similar to the
one in 6226:4bc94faeff66, where alias and try_files in a regex location
are used instead of root.

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
@@ -202,8 +202,14 @@ ngx_http_try_files_handler(ngx_http_requ
             return NGX_DONE;
         }
 
-        if (alias && alias != NGX_MAX_SIZE_T_VALUE
-            && ngx_filename_cmp(name, r->uri.data, alias) == 0)
+        if (alias == NGX_MAX_SIZE_T_VALUE
+            && ngx_filename_cmp(name, r->uri.data, r->uri.len) == 0)
+        {
+            ngx_memmove(name, name + r->uri.len, len - r->uri.len);
+            path.len -= r->uri.len;
+
+        } else if (alias
+                   && 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