NGINX – restricting Access with .htpasswd

Install apache2-utils (Debian, Ubuntu) or httpd-tools (RHEL/CentOS/Oracle Linux):

apt install apache2-utils

Create password file:

htpasswd -c /etc/apache2/.htpasswd user1

You can confirm that the file contains paired usernames and hashed passwords:

cat /etc/apache2/.htpasswd

In vhost configuration add condition:

location /my-restricted-directory {
    auth_basic           “Administrator’s Area”;
    auth_basic_user_file /etc/apache2/.htpasswd; 
}

Validate nginx conf:

nginx -t

Reload nginx:

service nginx reload

Done.