X
X
X
X

How to Encrypt Nginx Directory / Folder

HomepageArticlesHostingHow to Encrypt Nginx Directory / Fo...

On our Nginx server, for some reason, we may want to encrypt a directory/folder so that it can only be accessed with our username and password. Since Vesta CP is a free web panel, we will explain this article about how to encrypt a site's directory on VestaCP on NGINX.

You can perform Nginx directory encryption on all Nginx installed VPS or VDS server packages in this article.

We connect to the SSH of our Linux server and run the following command;

openssl passwd

The purpose here is to create the hash, the password that we will use for the directory and encrypt it. In the Password: section, type and enter an unforgettable password, it will give you the hash code of that password as follows;

 

Now it's time to create the authorization file with this hash code and username. This is called auth_basic_user_file. You can create this file under any directory, for example with nano we open an empty file like below;

nano -w /etc/nginx/techsoftcenter_password
And we add, save and remove the hash code corresponding to our password with the admin username. You can change the admin username here;
admin:VUj2xhni7xGt.

 

We've completed the provisioning, now it's time to add the Nginx directory encryption code to our nginx.conf file. The important factor here is that if the nginx.conf file is kept separate for each domain, it will find the nginx.conf file for that domain.

For example, we can list the settings file for nginxdomains on the VestaCPpanel as follows:


cd /home/admin/conf/web

ll

We will see the files of the domain names with the above command nginxi. Here we will do an example encryption for the techsoftcenter.com domain in the file techsoftcenter.com.nginx.conf. We open with nano;


one
nano -w cliaweb.com.nginx.conf
We add our directory encryption code to the end of the { section of the server. For example, techsoftcenter.com/encrypteddirectory, the code below. We will encrypt a part of it with the auth_basic_user_file hashfile path;


one
2nd
3
4
location /encrypteddirectory {
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/techsoftcentercom_password;
}
The example above includes standard encryption. Does not include subfiles. If you want to include it in subfolders, you can enter an nginx rule like below ( This code is compatible with Vesta CP nginx structure);


one
2nd
3
4
5
6
7
8
9
10
11th
12
13
14
15
location /encrypteddirectory {
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/techsoftcentercom_password;

location ~ [^/]\.php(/|$) {
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
if (!-f $document_root$fastcgi_script_name) {
return 404;
}

fastcgi_pass 127.0.0.1:9001;
fastcgi_index index.php;
include /etc/nginx/fastcgi_params;
}
}
Then we start again nginx;


one
service nginx restart
When we access our site http://sitename.com/encrypteddirectory with directory protection, it will appear as follows, we can log in with our username and password;

I hope it was a useful article.


Hosting Billing Software by WISECP
Top