It is possible, but it is hackish and hard to maintain... You can use priorities in evaluating locations in nginx and replace them with your own. This needs to be done for PHP files in the website root and for individual folders.
First you need the named location e.g.:
location @php {
try_files $uri $uri/index.php /index.php =404;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass <FPM>:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
add_header X-FastCGI-Cache $upstream_cache_status;
add_header X-Location "I'm custom / location!" always;
fastcgi_cache_bypass $skip_cache;
fastcgi_no_cache $skip_cache;
fastcgi_cache <CACHE>;
fastcgi_cache_valid 60m;
fastcgi_send_timeout 600s;
fastcgi_read_timeout 600s;
}
Then you need need try_files pointed to the named location for each of the folders with a switch for static files you want to serve.
location ^~ /wp-admin/ {
location ~* \.(css|js|png|jpe?g|gif|ico|svg|woff2?|ttf|eot|webp|avif)$ {
try_files $uri =404;
}
try_files /nonexistent @php;
}
location ^~ /wp-content/ {
location ~* \.(css|js|png|jpe?g|gif|ico|svg|woff2?|ttf|eot|webp|avif)$ {
try_files $uri =404;
}
try_files /nonexistent @php;
}
location ^~ /wp-includes/ {
location ~* \.(css|js|png|jpe?g|gif|ico|svg|woff2?|ttf|eot|webp|avif)$ {
try_files $uri =404;
}
try_files /nonexistent @php;
}
Then you need rules for the PHP files in document root:
location = /wp-login.php { try_files /nonexistent @php; }
location = /wp-cron.php { try_files /nonexistent @php; }
location = /xmlrpc.php { try_files /nonexistent @php; }
location = /wp-signup.php { try_files /nonexistent @php; }
location = /wp-comments-post.php { try_files /nonexistent @php; }
location = /wp-trackback.php { try_files /nonexistent @php; }
And finally the / override (except specific locations):
location ~ ^/(?!enhancecp|\.well-known/acme-challenge|wp-admin/|wp-content/|wp-includes/) {
try_files $uri @php;
}
It is doable, but ugly, weird and not very versatile. I don't recommend using something like this, but if you really have to...