Sometimes the simplest lock is the right one. You have a staging site, a client preview, or an admin panel on your VPS, and you need it hidden from everyone except a handful of people. A full login system is overkill, and a plugin is another thing to update. That is where nginx basic authentication earns its keep: a username and password prompt handled by the web server itself, before any application code runs.
This guide covers the whole setup on Ubuntu: creating the password file, wiring it into a server block, protecting a single directory, and testing that the lock holds. You need SSH access and a working Nginx install. No plugin, no control panel, no code changes to your site.
How Nginx Basic Authentication Works
With nginx basic authentication, the server asks the browser for a username and password before serving anything. The browser then sends the credentials with every request, encoded in base64, and Nginx checks them against a password file you create on the server.
Two directives do all the work. auth_basic turns the prompt on and sets the text the browser shows, and auth_basic_user_file points Nginx at the password file. The file holds one username:hashed-password pair per line, usually created with the htpasswd command.
One thing to understand before you start: basic auth credentials travel with every request, so this only makes sense over HTTPS. On plain HTTP, anyone watching the network can read the base64 string and decode it in seconds. If your site still runs on HTTP, sort out a certificate first. The walkthrough for setting up a free SSL certificate for a subdomain on Nginx covers the Certbot steps.
Install apache2-utils and Create the Password File
The htpasswd tool ships with the apache2-utils package, even though you are not running Apache. This basic auth nginx ubuntu setup works the same on 22.04 and 24.04. Install it, then create the password file outside the web root so it can never be downloaded:
sudo apt update && sudo apt install -y apache2-utils
sudo htpasswd -c /etc/nginx/.htpasswd deploy
The -c flag creates the file. You will be asked for a password twice. Use a long, random one and store it in a password manager: changing it later means re-issuing it to everyone.
To add a second user, run the same command without -c. With the flag, you overwrite the file and delete every existing user, which is the most common way people lock themselves out of their own staging site:
sudo htpasswd /etc/nginx/.htpasswd client-name
Check the file afterwards. Each line should look like deploy:$apr1$.... If a line has a plain-text password, delete it and re-create it with htpasswd. Also confirm the file is readable by the Nginx worker user, usually www-data, but not by anyone else: sudo chmod 640 /etc/nginx/.htpasswd and sudo chown root:www-data /etc/nginx/.htpasswd get the permissions right.
Add auth_basic to Your Nginx Server Block
Open the server block for the site you want to protect. A full Nginx server block setup on Ubuntu 24.04 follows the same file layout, so the config below should look familiar:
server {
listen 443 ssl;
server_name staging.example.com;
root /var/www/staging;
index index.html;
auth_basic "Restricted area";
auth_basic_user_file /etc/nginx/.htpasswd;
location / {
try_files $uri $uri/ =404;
}
}
The two lines sit at server level, so they cover every location in the block, including static files and images. The string after auth_basic is the label the browser shows in the login box. Keep it short and descriptive, such as “Staging – Acme Corp”. Avoid putting anything sensitive in it.
For this nginx htpasswd setup, placement matters. Put the directives inside the server block, not inside location /, unless you want them to apply to one path only, which the next section covers. After saving, test the config and reload:
sudo nginx -t
sudo systemctl reload nginx
Never skip the nginx -t step. A typo in a server block can stop Nginx from reloading, and on a busy server that is a bad way to find out. If you want a safer routine for config changes, the guide on how to test a new Nginx config without taking your site down walks through a zero-downtime pattern worth adopting.
Protect One Directory Instead of the Whole Site
Often you do not want to lock the entire site. A WordPress admin area, a /stats page, or a single client preview folder are the usual cases. Move the directives into a location block instead:
location /wp-admin/ {
auth_basic "Admin area";
auth_basic_user_file /etc/nginx/.htpasswd;
try_files $uri $uri/ /index.php?$args;
}
One catch with WordPress: the login page, /wp-login.php, sits outside /wp-admin/. Protecting only /wp-admin/ leaves the login form open to brute-force attempts, which defeats half the point. Add a second location block for /wp-login.php with the same two directives. Note that admin-ajax.php lives under /wp-admin/, so plugins calling it from the front end will start prompting visitors; if that happens, exempt that one file with a nested location.
To password protect nginx site sections like this, keep one password file shared between locations. One file, several protected paths, and a single place to add or remove users.
Test the Login Before You Trust It
Open the site in a private browser window. You should get a login prompt before anything loads. Enter the credentials, confirm the page renders, then test the failure cases, because a lock you never tested is just a guess:
- Cancel the prompt. You should get a 401 response, not the page.
- Try a wrong password. Same result: 401, no content.
- Try a direct URL to a static file, such as
/style.cssor an image. It should prompt too, since the protection sits at server level. - Check from a second browser or with
curl -u deploy:password https://staging.example.com/to confirm the server side, not just your browser cache.
Then look at the logs. A 401 in the access log after a cancelled prompt is normal. A 200 for a request without credentials is not, and it means the directives are in the wrong block or a different server block is answering the request. Check that server_name matches the domain you tested and that no default server block is catching the traffic.
Also confirm the prompt works over HTTPS only. If the same site answers on port 80, either redirect HTTP to HTTPS or add the same auth directives to the port 80 block. A password sent over plain HTTP is no better than no password at all.
Common Mistakes That Leave the Door Open
Most basic auth setups fail in the same handful of ways:
- The password file sits inside the web root. A file at
/var/www/html/.htpasswdcan be downloaded by anyone who guesses the path. Keep it in/etc/nginx/. htpasswd -cwiped the other users. The create flag overwrites. Use it once, then add users without it.- Wrong file permissions. If Nginx cannot read the password file, every login fails with a 500 error. The
640 root:www-dataownership from earlier avoids this. - Protection only on
location /. Other locations, such as a/api/block or a static files location, bypass it. Put the directives at server level unless you deliberately want per-path rules. - Weak passwords shared by email or chat. One shared password means you cannot revoke one person’s access without changing it for everyone. Give each person their own account so you can remove one line when they leave.
- Basic auth as the only layer. It stops casual visitors, not determined attackers. Pair it with the other layers: the guide on securing Nginx with security headers in 2026 covers the headers side, and rate limiting plus fail2ban handle repeated login attempts.
The nginx auth_basic directive is simple by design. It does one thing, at the server level, and never depends on your application being healthy. That is why it works so well for staging sites and maintenance pages, and why it should never be the only protection on anything holding real user data.
When to Move Beyond Basic Auth
Basic auth fits a staging site, a client preview, a small internal tool, or a temporary maintenance page. It stops being enough when you need per-user permissions, login history, two-factor authentication, or a way for users to reset their own passwords. At that point, move to your application’s own login system, a single sign-on provider, or an identity-aware proxy in front of Nginx. It also fits poorly when the audience is large: every user needs a line in the password file, there is no self-service reset, and no expiry. If you find yourself managing more than a dozen accounts by hand, that is the signal to switch.
For the cases it fits, it is hard to beat: two directives, one password file, and a staging site that stays invisible until you share it. Set it up once, test it properly, and rotate passwords when the list changes.