Install htpasswd utility:
Create the .htpasswd file: This file will store usernames and the hashed passwords:
apt install apache2-utils # For Debian/Ubuntu
dnf install httpd-tools # For Fedora/RHEL
To add more users later without overwriting the file, omit the -c flag.htpasswd -c /etc/nginx/.htpasswd your_username
Edit your nginx configuration file:
Test and reload nginx:
location /protected/ {
auth_basic "Restricted Area";
auth_basic_user_file /etc/nginx/.htpasswd;
autoindex on; # To enable directory listing (indexes)
}
nginx -t
sytemctl reload nginx