Reference: http://nginx.org/en/docs/http/ngx_http_auth_basic_module.html
Assume the subdirectory url is http://www.example.com/admin/.
Step 1: create 2 files storing username/password (encrypted) pairs using htpasswd
# to secure the admin site
htpasswd -bc /tmp/admin_passwd.txt admin adminpassword
# to secure the main site
htpasswd -bc /tmp/site_passwd.txt user userpassword
Step 2: set up your nginx config:
server {
listen 80;
server_name www.example.com;
root /tmp/www;
location ^~ /admin/ {
auth_basic "secured site admin";
auth_basic_user_file /tmp/admin_passwd.txt;
}
location / {
auth_basic "secured site";
auth_basic_user_file /tmp/site_passwd.txt;
}
}