nginx redirect params to php index in subfolder?

I have a server block to execute PHP files:

server {
 index index.php
 location / {
    try_files $uri $uri/ /index.php;
 }

 location ~ \.php$ {
    fastcgi_pass unix:/run/php/php7.3-fpm.sock;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_index index.php;
    fastcgi_param HTTP_AUTHORIZATION $http_authorization;
    include fastcgi_params;
 }
 ## ... etc

}

I'd like sub folders to execute their own index.php files with anything [without an extension] under a docs sub-folder path to be redirected to an index.php within the docs folder with the rest of the parameters coming in through the querystring, without affecting the same process for the root index.php file ... wow I explained that badly. Here:

mysite.com/docs/introduction/getting-started would become mysite.com/docs/?path=introduction/getting-started

mysite.com/foo/bar/ would become mysite.com/?path=foo/bar/

but

mysite.com/docs/introduction/getting-started/screenshot.jpg would serve that file (because it has an extension).

so for a given sub-folder I want to have its own index.php processor for the remaining parameters, if that makes any sense. I have tried

    location /docs {
        try_files $uri $uri/ /docs/index.php$query_string;
    }

which is fine IF there is a querystring value which there are not - only /docs/folder/subfolder/subsubfolder/etc . I can see it need an expression including a (.*) in there somehow but try as I might I can't make it work.

How do I do this?


You can use try_files with a named location, and place one or more rewrite rules within the named location.

For example:

location /docs {
    try_files $uri $uri/ @docs;
}
location @docs {
    rewrite ^(/[^/]+)/(.*)$ $1/index.php?path=$2 last;
}

See the manual pages for try_files and rewrite.