Talking about this well known solution:
I'm looking for a straightforward way working also on a Nginx installed WP and not only on Apache ones.
Is there any other code-based approach you would recommend, that works fine on any server environment?
So far, after testing various plugins from the official repository on a Nginx installed WP website, I also discovered that a bunch of them just fail when the web server is not accepting htaccess rules.
In my opinion, the best would be using the same approach for both:
setting a htaccess / nginx web server rule controlling any file request made toward a specific /uploads subfolder. The rule I found working well on my Apache based WP is the following:
RewriteCond %{REQUEST_URI} ^.*wp-content/uploads/restricted/.*
RewriteRule ^wp-content/uploads/(restricted/.*)$ dl.php?file=$1 [QSA,L]
I found - almost - the same rule for a Nginx environment, but despite my tests I'm still not able to make it work on a subfolder, so the following is just for the whole /uploads folder, not recommended:
location ~* /(?:uploads|files)/* {
rewrite /wp-content/uploads/(.*)$ /dl.php?file=$1;
}
Furthermore, as long as many clients of mines have their installations on a shared hosting, asking the provider to set a nginx rule for a single website is a lost cause most of the times.