[nginx] Support for "try_files $uri ..." in regex locations with...

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


details:   http://freenginx.org/hg/nginx/rev/4c206d68284e
branches:  
changeset: 9503:4c206d68284e
user:      Maxim Dounin <mdounin at mdounin.ru>
date:      Sat Apr 18 01:44:26 2026 +0300
description:
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.

diffstat:

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

diffs (20 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
@@ -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