[nginx] Fixed try_files to clear r->valid_unparsed_uri.

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


details:   http://freenginx.org/hg/nginx/rev/5158f263a08e
branches:  
changeset: 9506:5158f263a08e
user:      Maxim Dounin <mdounin at mdounin.ru>
date:      Sat Apr 18 01:44:33 2026 +0300
description:
Fixed try_files to clear r->valid_unparsed_uri.

Most notably, with this change any URI changes made by try_files are
visible with proxy_pass without URI, such as in the following
configuration:

    location / {
        try_files $uri $uri.html =404;
        proxy_pass http://u;
    }

Previously, such URI changes were only visible if URI was previously
changed by a rewrite or an internal redirect.

To minimize compatibility issues, this is only done when the new URI
is not equal to the original one.  That is, a configuration like
this will work as before, preserving the original URI as sent by the
client:

    location / {
        try_files $uri =404;
        proxy_pass http://u;
    }

Note that URI checking uses ngx_strncmp() rather than ngx_filename_cmp()
to properly reflect case-only changes on case-insensitive systems as well.

diffstat:

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

diffs (38 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
@@ -251,12 +251,13 @@ ngx_http_try_files_handler(ngx_http_requ
         path.len -= root;
         path.data += root;
 
+        name = r->uri.data;
+        len = r->uri.len;
+
         if (!alias) {
             r->uri = path;
 
         } else {
-            name = r->uri.data;
-
             r->uri.len = alias + path.len;
             r->uri.data = ngx_pnalloc(r->pool, r->uri.len);
             if (r->uri.data == NULL) {
@@ -283,6 +284,18 @@ ngx_http_try_files_handler(ngx_http_requ
                    != 0)
             {
                 r->valid_location = 0;
+                r->valid_unparsed_uri = 0;
+            }
+        }
+
+        if (r->valid_unparsed_uri) {
+
+            /* clear r->valid_unparsed_uri if URI was changed */
+
+            if (r->uri.len != len
+                || ngx_strncmp(r->uri.data, name, len) != 0)
+            {
+                r->valid_unparsed_uri = 0;
             }
         }
 


More information about the nginx-devel mailing list