nginx basic auth

Scenario: Password protected certain area of sub-dir of the web server content.

See Quote: Basic HTTP Auth nginx
Example:

location  /  {
  auth_basic            "Restricted";
  auth_basic_user_file  htpasswd;
}

htppasswd is the file store all your username and password, and is relative to directory of nginx configuration file nginx.conf.

To generate an .htpasswd file in linux, most common solution would be using openssl

4 algorithms can be chosen, (crypt, Apache MD5, MD5 and Salted SHA-1)

# this example uses crypt encryption
printf "(username):$(openssl passwd -crypt actual_passwd)\n" >> .htpasswd
# this example uses apr1 (Apache MD5) encryption
printf "(username):$(openssl passwd -apr1 actual_passwd)\n" >> .htpasswd
# this example uses MD5 encryption
printf "(username):$(openssl passwd -1 actual_passwd)\n" >> .htpasswd
# This example uses SSHA (Salted SHA-1) encryption
echo "(username):{SSHA}$(echo -n 'actual_passwdSALT' | openssl dgst -binary -sha1 | sed 's/$/MYSALT/' | base64)" >> .htpasswd