How to setup Basic Auth password authentication for NGINX

Create a *.htpasswd
file. Eg:
touch /etc/nginx/conf.d/nginx.htpasswd
Add an user to the file:
echo -n 'user1' >> ./nginx/nginx.htpasswd
Generate a password to the user using openssl
ultility:
openssl passwd -apr1 >> ./nginx/nginx.htpasswd
The above command will prompt you for a password and confirmation. Your nginx.htpasswd
file should now look like this
user1:$apr1$EWcGMeKU$o4kl71mZcWWDwQugwt2rt.
In any Nginx route config, simple add the following to password protect the route:
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://localhost:80;
# password protection
auth_basic "Authentication Required";
auth_basic_user_file /etc/nginx/conf.d/nginx.htpasswd;
}
}