[nginx] Reworked try_files in regex locations with alias.

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


details:   http://freenginx.org/hg/nginx/rev/8e4422e216d1
branches:  
changeset: 9504:8e4422e216d1
user:      Maxim Dounin <mdounin at mdounin.ru>
date:      Sat Apr 18 01:44:28 2026 +0300
description:
Reworked try_files in regex locations with alias.

Instead of using special cases for "alias == NGX_MAX_SIZE_T_VALUE" everywhere,
we now set alias to r->uri.len when working with such locations.  And the
r->add_uri_to_alias flag is replaced by the r->alias_in_uri flag, which
specifies the URI prefix already covered by alias.

This simplifies the code, and also fixes various issues with URI being
incorrect after try_files match in such locations.

diffstat:

 src/http/modules/ngx_http_try_files_module.c |  24 ++++++------------------
 src/http/ngx_http_core_module.c              |  26 +++++++++-----------------
 src/http/ngx_http_request.h                  |   3 ++-
 3 files changed, 17 insertions(+), 36 deletions(-)

diffs (153 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
@@ -111,6 +111,11 @@ ngx_http_try_files_handler(ngx_http_requ
 
     alias = clcf->alias;
 
+    if (alias == NGX_MAX_SIZE_T_VALUE) {
+        alias = r->uri.len;
+        r->alias_in_uri = alias;
+    }
+
     for ( ;; ) {
 
         if (tf->lengths) {
@@ -134,9 +139,6 @@ ngx_http_try_files_handler(ngx_http_requ
         if (!alias) {
             reserve = len > r->uri.len ? len - r->uri.len : 0;
 
-        } else if (alias == NGX_MAX_SIZE_T_VALUE) {
-            reserve = len;
-
         } else {
             reserve = len > r->uri.len - alias ? len - (r->uri.len - alias) : 0;
         }
@@ -202,15 +204,7 @@ ngx_http_try_files_handler(ngx_http_requ
             return NGX_DONE;
         }
 
-        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)
-        {
+        if (alias && ngx_filename_cmp(name, r->uri.data, alias) == 0) {
             ngx_memmove(name, name + alias, len - alias);
             path.len -= alias;
         }
@@ -261,12 +255,6 @@ ngx_http_try_files_handler(ngx_http_requ
         if (!alias) {
             r->uri = path;
 
-        } else if (alias == NGX_MAX_SIZE_T_VALUE) {
-            if (!test_dir) {
-                r->uri = path;
-                r->add_uri_to_alias = 1;
-            }
-
         } else {
             name = r->uri.data;
 
diff --git a/src/http/ngx_http_core_module.c b/src/http/ngx_http_core_module.c
--- a/src/http/ngx_http_core_module.c
+++ b/src/http/ngx_http_core_module.c
@@ -977,6 +977,7 @@ ngx_http_core_find_config_phase(ngx_http
 
     r->content_handler = NULL;
     r->uri_changed = 0;
+    r->alias_in_uri = 0;
 
     rc = ngx_http_core_find_location(r);
 
@@ -1905,6 +1906,10 @@ ngx_http_map_uri_to_path(ngx_http_reques
 
     alias = clcf->alias;
 
+    if (alias == NGX_MAX_SIZE_T_VALUE) {
+        alias = r->alias_in_uri ? r->alias_in_uri : r->uri.len;
+    }
+
     if (alias && !r->valid_location) {
         ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
                       "\"alias\" cannot be used in location \"%V\" "
@@ -1912,7 +1917,7 @@ ngx_http_map_uri_to_path(ngx_http_reques
         return NULL;
     }
 
-    if (alias > r->uri.len && alias != NGX_MAX_SIZE_T_VALUE) {
+    if (alias > r->uri.len) {
         ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
                       "URI shorter than aliased URI part");
         return NULL;
@@ -1933,12 +1938,7 @@ ngx_http_map_uri_to_path(ngx_http_reques
 
     } else {
 
-        if (alias == NGX_MAX_SIZE_T_VALUE) {
-            reserved += r->add_uri_to_alias ? r->uri.len + 1 : 1;
-
-        } else {
-            reserved += r->uri.len - alias + 1;
-        }
+        reserved += r->uri.len - alias + 1;
 
         if (ngx_http_script_run(r, path, clcf->root_lengths->elts, reserved,
                                 clcf->root_values->elts)
@@ -1955,15 +1955,6 @@ ngx_http_map_uri_to_path(ngx_http_reques
 
         *root_length = path->len - reserved;
         last = path->data + *root_length;
-
-        if (alias == NGX_MAX_SIZE_T_VALUE) {
-            if (!r->add_uri_to_alias) {
-                *last = '\0';
-                return last;
-            }
-
-            alias = 0;
-        }
     }
 
     last = ngx_copy(last, r->uri.data + alias, r->uri.len - alias);
@@ -2566,7 +2557,7 @@ ngx_http_internal_redirect(ngx_http_requ
 
     r->internal = 1;
     r->valid_unparsed_uri = 0;
-    r->add_uri_to_alias = 0;
+    r->alias_in_uri = 0;
     r->main->count++;
 
     ngx_http_handler(r);
@@ -2626,6 +2617,7 @@ ngx_http_named_location(ngx_http_request
             r->internal = 1;
             r->content_handler = NULL;
             r->uri_changed = 0;
+            r->alias_in_uri = 0;
             r->loc_conf = (*clcfp)->loc_conf;
 
             /* clear the modules contexts */
diff --git a/src/http/ngx_http_request.h b/src/http/ngx_http_request.h
--- a/src/http/ngx_http_request.h
+++ b/src/http/ngx_http_request.h
@@ -446,6 +446,8 @@ struct ngx_http_request_s {
     u_char                           *captures_data;
 #endif
 
+    size_t                            alias_in_uri;
+
     size_t                            limit_rate;
     size_t                            limit_rate_after;
 
@@ -492,7 +494,6 @@ struct ngx_http_request_s {
 
     unsigned                          invalid_header:1;
 
-    unsigned                          add_uri_to_alias:1;
     unsigned                          valid_location:1;
     unsigned                          valid_unparsed_uri:1;
     unsigned                          uri_changed:1;


More information about the nginx-devel mailing list