The Short Answer
location /admin/ {
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/.htpasswd;
}The text after auth_ is the realm, which some browsers show in the login box. The steps below create the password file, set its permissions, and test the result.
Step 1: Install htpasswd
sudo apt install apache2-utils # Debian and Ubuntu
sudo dnf install httpd-tools # AlmaLinux and Rocky LinuxBoth packages contain only the Apache helper tools; they do not install the Apache web server.
Step 2: Create the Password File
sudo htpasswd -c /etc/nginx/.htpasswd deploy
sudo htpasswd /etc/nginx/.htpasswd userThe first command creates the file with the user deploy and asks for a password. -c creates a new file and overwrites an existing one, so leave it out for every user after the first, as in the second line. Other tasks:
- Change a password: run the second form again with the same name.
- Remove a user:
sudo htpasswd -D /etc/nginx/.htpasswd user. - Password format: keep htpasswd's default. Nginx documents crypt, apr1 (MD5), and the RFC 2307 schemes for this file and does not list bcrypt, so do not add
-B.
Step 3: Set Owner and Mode
sudo chown root:www-data /etc/nginx/.htpasswd
sudo chmod 640 /etc/nginx/.htpasswdOn AlmaLinux and Rocky Linux use root:nginx, the group Nginx runs as there. Keep the file outside the web root, so it can never be downloaded as a page; /etc/nginx is a good place.
Step 4: Protect a Path or the Whole Site
Put the two lines in a location to protect one path, or directly in the server block to protect the whole site. To leave one path open inside a protected site, switch it off there:
server {
server_name staging.example.com;
auth_basic "Staging";
auth_basic_user_file /etc/nginx/.htpasswd;
location /.well-known/acme-challenge/ {
auth_basic off;
}
}Then test and reload:
sudo nginx -t
sudo systemctl reload nginxReload rather than restart: a failed test leaves the running server untouched, while a restart with a broken file takes the site down.
Use It Only over HTTPS
Basic authentication sends the user name and password with every request, encoded but not encrypted. Over plain HTTP anyone on the path can read them. Put the protection in the block that listens on 443, get a certificate with Certbot for Nginx, and redirect HTTP to HTTPS.
Combine It with an IP Allow List
location /admin/ {
satisfy any;
allow 203.0.113.0/24;
deny all;
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/.htpasswd;
}With satisfy any, a visitor from the allowed range gets in without a password, and everyone else must log in. With the default, satisfy all, a visitor needs both the right address and the password.
Test It
curl -I https://example.com/admin/
curl -I -u deploy https://example.com/admin/The first request should get a 401 with a WWW-Authenticate header. The second asks for the password and should get a 200. Failed logins appear in the error log. The difference between 401 and 403 is explained in 401 vs 403.
Limits of Basic Auth
- No logout and no lockout: the browser remembers the login until it closes, and Nginx does not block repeated guesses. Add a rate limit with
limit_; see 429 Too Many Requests.req - One shared gate: it suits a staging site or an admin path, not a site with many users and roles.
Updated:
Linux VPS with Full Root Access
MUVCloud cloud servers run on KVM with Enterprise NVMe SSD storage and full root access, in Turkey, the Netherlands, and the USA.
More Guides
No Limits on Service or Support
You are not on your own as your projects grow. Our support engineers are ready 24/7, on several channels, whenever you need them.

- 100% customer satisfaction goal
- Our own hardware
- 24/7 ticket and email support
- 4 countries with locations