sudo apt install apache2-utils
sudo touch /etc/nginx/htpasswd
sudo htpasswd /etc/nginx/htpasswd username
Enter password
to delete a user from the htpasswd file
sudo htpasswd -D /etc/nginx/htpasswd username
In nginx Vhost , inside the server block add the contents
location / {
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/htpasswd;
}
sudo nginx -t
sudo service nginx restart