[PATCH 2 of 7] Tests: try_files in a regex location with alias additional tests

Maxim Dounin mdounin at mdounin.ru
Fri Apr 17 22:50:21 UTC 2026


# HG changeset patch
# User Maxim Dounin <mdounin at mdounin.ru>
# Date 1775283321 -10800
#      Sat Apr 04 09:15:21 2026 +0300
# Node ID 764b07be5a74047456643df286ffb0b9149bcea4
# Parent  2e3ccde7b2a0e44ed512d324333424057a57d4fe
Tests: try_files in a regex location with alias additional tests.

When alias is used in a regex location, alias is expected to specify full
file system path to the file corresponding to the request.  When try_files
is used in such a location, it is expected to be able to add extensions
to the path, such as in the following configuration (see 3534:3711bb1336c3):

    location ~ ^/users/(.*)/files/(.*) {
        alias /files/$1/$2;
        try_files .html "" / =404;
    }

Previously, the only test was for the bug (fixed by 6226:4bc94faeff66),
where a configuration is essentially misuses alias with try_files instead
of root.  That is, all requests are mapped to a single file path using
alias, and then try_files used to add $uri to this path:

    location ~ /mail {
        alias /path/to/directory;
        try_files $uri =404;
    }

While handling such case without a segmentation fault is important, it
is not to be confused with intended try_files usage.  Further, handling
of this specific case is expected to be changed to universally support
aliased prefix replacement and configurations like this:

    location ~ ^/users/(.*)/files/(.*) {
        alias /files/$1/$2;
        try_files $uri.html $uri $uri/ =404;
    }

That is, with matching URI part (the whole URI for regex locations) replaced
with alias, so "try_files $uri.html ..." will be universally interpreted
as $request_filename with the ".html" extension added in all cases (with
root, alias, and alias in regex location).

diff --git a/http_try_files.t b/http_try_files.t
--- a/http_try_files.t
+++ b/http_try_files.t
@@ -21,7 +21,7 @@ use Test::Nginx;
 select STDERR; $| = 1;
 select STDOUT; $| = 1;
 
-my $t = Test::Nginx->new()->has(qw/http proxy rewrite/)->plan(10)
+my $t = Test::Nginx->new()->has(qw/http proxy rewrite/)->plan(12)
 	->write_file_expand('nginx.conf', <<'EOF');
 
 %%TEST_GLOBALS%%
@@ -79,6 +79,16 @@ http {
             try_files $uri =404;
         }
 
+        location ~ /alias-re-add/(.*) {
+            alias %%TESTDIR%%/$1;
+            try_files .htm .html =404;
+        }
+
+        location ~ /alias-re-prefix/(.*) {
+            alias %%TESTDIR%%/$1;
+            try_files $uri.htm $uri.html =404;
+        }
+
         location /alias-nested/ {
             alias %%TESTDIR%%/;
             location ~ html {
@@ -117,7 +127,19 @@ like(http_get('/file-dir/'), qr!404 Not!
 like(http_get('/dir-dir/'), qr!301 Moved Permanently!, 'dir matches dir');
 like(http_get('/dir-file/'), qr!404 Not!, 'dir does not match file');
 
-like(http_get('/alias-re.html'), qr!SEE THIS!, 'alias in regex location');
+like(http_get('/alias-re.html'), qr!SEE THIS|404 Not!,
+	'alias in regex location as root');
+like(http_get('/alias-re-add/found'), qr!SEE THIS!,
+	'alias in regex location with just extension');
+
+TODO: {
+local $TODO = 'not yet' unless $t->has_version('1.31.0');
+
+like(http_get('/alias-re-prefix/found'), qr!SEE THIS!,
+	'alias in regex location with uri prefix');
+
+}
+
 like(http_get('/alias-nested/found.html'), qr!SEE THIS!,
 	'alias with nested location');
 



More information about the nginx-devel mailing list