Set Up HTTP Basic Authentication with Nginx on AlmaLinux 9 / Rocky Linux 9
Password-protect an nginx site or a single directory on AlmaLinux 9, Rocky Linux 9 or CentOS Stream 9 with htpasswd, auth_basic and a few useful variations.
HTTP basic authentication puts a username and password prompt in front of a site or a single directory. It is a quick way to lock down a staging site, an admin area or a statistics page without touching the application. This guide sets it up with nginx on AlmaLinux 9, Rocky Linux 9 or CentOS Stream 9.
CentOS 7 reached end of life in June 2024. The nginx directives are unchanged on its successors, but packages are installed with dnf and the service is managed with systemctl rather than an init script.
Requirements
- A server running AlmaLinux 9, Rocky Linux 9 or CentOS Stream 9 and a user with
sudorights. - nginx installed and serving your site. If it is not installed yet:
sudo dnf install nginx
sudo systemctl enable --now nginx
sudo firewall-cmd --permanent --add-service=http --add-service=https
sudo firewall-cmd --reload
Step 1: Install the htpasswd tool
nginx reads usernames and password hashes from a plain text file. The htpasswd utility that creates such files is part of the httpd-tools package, which installs without pulling in the Apache web server:
sudo dnf install httpd-tools
Step 2: Create the password file
Create the file with a first user. You are asked to type the password twice:
sudo htpasswd -c /etc/nginx/.htpasswd deploy
The -c option creates the file and overwrites an existing one, so leave it out when you add more users:
sudo htpasswd /etc/nginx/.htpasswd anotheruser
Keep the file outside any directory that nginx serves, and let only root and the nginx user read it:
sudo chown root:nginx /etc/nginx/.htpasswd
sudo chmod 640 /etc/nginx/.htpasswd
Each line of the file has the form name:hash. To remove a user later:
sudo htpasswd -D /etc/nginx/.htpasswd anotheruser
Step 3: Enable authentication in nginx
Two directives do the work. auth_basic switches authentication on and sets the realm text that browsers may show in the login prompt; auth_basic_user_file names the password file. Both are valid in the http, server and location contexts, so you can protect everything or only one path.
On the RHEL 9 family, the default site is the server block in /etc/nginx/nginx.conf, and additional sites go in their own files under /etc/nginx/conf.d/. Open the file that holds the server block for your site, for example:
sudo vi /etc/nginx/conf.d/example.com.conf
Protect the whole site
Put the directives directly in the server block:
server {
listen 80;
server_name example.com www.example.com;
root /usr/share/nginx/html;
auth_basic "Restricted area";
auth_basic_user_file /etc/nginx/.htpasswd;
}
Protect one directory
Put them in a location block instead; the rest of the site stays public:
server {
listen 80;
server_name example.com www.example.com;
root /usr/share/nginx/html;
location /admin/ {
auth_basic "Restricted area";
auth_basic_user_file /etc/nginx/.htpasswd;
}
}
Every directive ends with a semicolon; a missing one is the most common reason for nginx refusing to reload.
Step 4: Test and reload nginx
Check the configuration first, then reload. A reload applies the change without dropping connections:
sudo nginx -t
sudo systemctl reload nginx
Step 5: Verify
A request without credentials must now be rejected with status 401:
curl -I http://example.com/admin/
HTTP/1.1 401 Unauthorized
WWW-Authenticate: Basic realm="Restricted area"
With a valid user, curl asks for the password and the request succeeds:
curl -I -u deploy http://example.com/admin/
In a browser, opening the protected address shows a login prompt.
Useful variations
Leave one path open
When authentication is set for the whole server, auth_basic off; cancels it for a single location. This is useful for health checks or for the /.well-known/acme-challenge/ path used when issuing Let's Encrypt certificates:
location /.well-known/acme-challenge/ {
auth_basic off;
}
Skip the prompt for trusted addresses
Combine password and address rules with satisfy any. Requests from the listed network get in without a password; everyone else must log in:
location /admin/ {
satisfy any;
allow 198.51.100.0/24;
deny all;
auth_basic "Restricted area";
auth_basic_user_file /etc/nginx/.htpasswd;
}
With satisfy all, which is the default, a visitor needs both an allowed address and a valid password.
Use HTTPS
Basic authentication sends the username and password with every request, only base64-encoded. Over plain HTTP anyone on the network path can read them. Serve the protected site over HTTPS and redirect HTTP to it before you rely on the password for anything that matters.
Troubleshooting
- The correct password is rejected or you get a 500 error: nginx cannot open the password file. Read
/var/log/nginx/error.log, then check the path inauth_basic_user_fileand the ownership and mode from step 2. - The file is in a custom location and still unreadable: SELinux is enforcing by default and only lets nginx read files with a web-server label. Keeping the file under
/etc/nginxavoids the problem. Look for denials withsudo ausearch -m AVC -ts recent. - No prompt appears: the request is handled by a different
serverorlocationblock than the one you edited.sudo nginx -Tprints the complete configuration nginx is using. - Relative paths: a relative name in
auth_basic_user_fileis resolved against the nginx configuration directory, not the site root. Use an absolute path to avoid surprises.
The directive reference is in the nginx auth_basic module documentation.
Frequently asked questions
Can I create the password file without installing httpd-tools?
Yes. nginx also accepts hashes produced by openssl passwd. Generate one with openssl passwd -apr1, then add a line in the form deploy:HASH to the password file with a text editor.
Which hash formats does nginx accept?
Hashes that the system's crypt() function understands, the Apache MD5 variant (apr1) that htpasswd writes by default, and salted SHA-1 in the {SSHA} form. The nginx documentation advises against the unsalted {SHA} and {PLAIN} schemes.
Can I use different users for different directories?
Yes. Create a separate password file for each area and point each location block at its own file with auth_basic_user_file. A setting in a location overrides the one inherited from the server block.
Is basic authentication enough to protect an admin panel?
It is a useful extra layer, not a replacement for the application's own login. There is no logout and no lockout after failed attempts, so use strong passwords, serve it over HTTPS, and consider combining it with address restrictions as shown above.