Welcome! Log In Create A New Profile

Advanced

NGINX configuration folder protection of thousands of subdirectories

Posted by jenwest 
NGINX configuration folder protection of thousands of subdirectories
March 07, 2020 09:50PM
The following code allows me to protect a specific folder with the membership software we're using. The problem is if we have 1,000 folders we need to protect, we need to copy/paste this same code for each and every folder.

I'm wondering, how can I change the "location ^~ /FOLDER/" location so it encompasses every folder with the parent folder we want, and the "FOLDERID" so that it encompasses every number? (1 through infinite)

location ^~ /FOLDER/ {
if ($cookie_amember_nr !~* [a-zA-Z0-9]+) { #not authorized
rewrite ^(.*)$ http://EXAMPLE.COM/AMEMBER/protect/new-rewrite?f=FOLDERID&url=$request_uri?$args redirect;
}
set $file $document_root/AMEMBER/data/new-rewrite/$cookie_amember_nr-FOLDERID;
if (!-f $file) { #have not access
rewrite ^(.*)$ http://EXAMPLE.COM/AMEMBER/no-access/folder/id/FOLDERID?url=$request_uri?$args redirect;
}
#everything is ok
}

FOLDER with actual path to your protected folder
http://EXAMPLE.COM/AMEMBER with your actual aMember installation url
/AMEMBER/ with path to aMember installation on your server
FOLDERID with ID (integer value) for this folder in aMember CP



Example:

location ^~ /protect-folders/folder-1/ {
if ($cookie_amember_nr !~* [a-zA-Z0-9]+) { #not authorized
rewrite ^(.*)$ http://stackexamples.com/amember/protect/new-rewrite?f=513&url=$request_uri?$args redirect;
}
set $file $document_root/amember/data/new-rewrite/$cookie_amember_nr-513;
if (!-f $file) { #have not access
rewrite ^(.*)$ http://stackexamples.com/amember/no-access/folder/id/513?url=$request_uri?$args redirect;
}
#everything is ok
}
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 78
Record Number of Users: 6 on February 13, 2018
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready