Most guides tell you to install apache2-utils for the htpasswd command. On a server or in a container without it, openssl can make the same line.
printf 'admin:%s\n' "$(openssl passwd -apr1)" | sudo tee -a /etc/nginx/htpasswd
openssl asks for the password and prints an $apr1$ hash, the Apache MD5 format that nginx reads. The printf puts the user name in front. Then protect a location:
location /admin/ {
auth_basic "Admin";
auth_basic_user_file /etc/nginx/htpasswd;
proxy_pass http://127.0.0.1:8081/;
}
Reload, and the browser asks for a login. A request without credentials gets a 401, and with curl -u admin:... it goes through. The text after auth_basic is shown in some browser prompts. For a subpath that must stay open, like a health check, put auth_basic off; in its own location.
Basic auth sends the password with every request, only base64 encoded, so use it over HTTPS only. Avoid putting the password on the command line as an argument to openssl, where it ends up in your shell history; I’d use the prompt, which is safer. The file must be readable by the nginx worker user but not by everyone. And it is a single shared lock, without rate limiting or lockout, so pair it with limit_req for anything exposed to the internet.
Further reading: ngx_http_auth_basic_module, openssl-passwd
Going deeper: ๐ Guide: AdGuard Home + Tailscale (Secure Split-Horizon DNS)