How to Set Up Nginx Basic Auth with htpasswd on Ubuntu 24.04
Set up Nginx auth_basic with htpasswd on Ubuntu 24.04, pick a hash (the Nginx docs say not to use {SHA}), and tell 401, 403 and 500 apart by the error log.
To put a password on Nginx on Ubuntu 24.04, create the user file with htpasswd from apache2-utils, add the auth_basic and auth_basic_user_file directives, and prefer -B (bcrypt) for new files (reasoning in [4]). auth_basic is the quickest way to put a password on an admin page or a staging site. The user file comes from htpasswd, which can write several hash formats, so the real question is which one to use. Everything below was run in an Ubuntu 24.04 container with Nginx 1.24.0, logging in once per format.
[1] htpasswd ships in the apache2-utils package. You do not need Apache on the server.
# apt install apache2-utils# htpasswd -c /etc/nginx/htpasswd alice-c creates the file. Add it to an existing file and every current user is gone, so leave -c off from the second user on: htpasswd /etc/nginx/htpasswd bob. The -b flag puts the password on the command line, which ends up in shell history.
[2] Add two directives to a server or location. The auth_basic string is the realm shown in the browser's login prompt.
server { listen 80; root /var/www/html; location / { auth_basic "closed site"; auth_basic_user_file /etc/nginx/htpasswd; }}# nginx -t && systemctl reload nginxnginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successfulPer the Nginx docs, each line of the user file is name:password. A line starting with # is a comment, and name:password:comment adds a note in a third field. To exempt one path, put auth_basic off; in its location.
[3] A request without credentials gets a 401 and the header that makes the browser ask for a login.
$ curl -si http://127.0.0.1:8080/HTTP/1.1 401 UnauthorizedWWW-Authenticate: Basic realm="closed site"[4] Choose a hash format. Each htpasswd option writes a different prefix into the file. Here is one password hashed each way.
$ htpasswd -nb alice "$PW"$ htpasswd -nbm bob "$PW"$ htpasswd -nbB carol "$PW"$ htpasswd -nbs dave "$PW"$ htpasswd -nbd erin "$PW"alice:$apr1$VmQC0BbA$4jGmg.TAh6MMKrrUCA6CX1bob:$apr1$4hdw651Y$W6S18E2c329/8VF.6GeUK/carol:$2y$05$ev3KdpMyHbNQ6Yy00/dlNuPQuIafIC2ZbvGOSn4EKrtnFVzHJoJDCdave:{SHA}XHFEJM09of/Y3bLMHe65thN5VqQ=Warning: Password truncated to 8 characters by CRYPT algorithm.erin:t1V0STR1uFXGs(-n prints to the screen instead of writing a file.) With no format flag, this version of htpasswd writes $apr1$. Each of these went into the Nginx user file, along with $6$ (SHA-512 crypt), $5$ (SHA-256 crypt), {PLAIN} and {SSHA} lines, and logged in with the same password.
alice 200bob 200carol 200dave 200erin 200frank 200gina 200hank 200ivy 200jack 200alice-wrongpw 401nouser 401| Prefix in file | Made with | Nginx 1.24.0 | What the docs say |
|---|---|---|---|
$apr1$ | htpasswd -m (default) · openssl passwd -apr1 | login works | apr1 supported |
$2y$ | htpasswd -B | login works | not mentioned; handled by crypt() |
$6$ · $5$ | openssl passwd -6 · -5 | login works | not mentioned; handled by crypt() |
{SHA} | htpasswd -s | login works | should not be used |
{SSHA} | built by hand | login works | salted SHA-1, used by some packages |
{PLAIN} | typed by hand | login works | example only, should not be used |
old crypt() (-d) | htpasswd -d | login works | supported |
"Login works" is what was observed. That $2y$, $6$ and $5$ work follows from the docs saying values encrypted with crypt() are accepted, which is a reading of the docs, not a documented promise. It depends on the C library behind crypt(), so other distributions may differ.
The Nginx docs describe {SHA} as "plain SHA-1 hashing, should not be used". It exists to help migration from other web servers, and the docs say not to use it for new passwords because unsalted SHA-1 is vulnerable to rainbow table attacks. For new files use bcrypt with -B. The htpasswd documentation calls -B "currently considered to be very secure" and says only that the default, -m, is MD5-based. $apr1$ logged in during the test and the Nginx docs list it as supported, but no official statement was found that it is as strong as bcrypt, so treat it as a default you may accept, not an equal.
[5] When authentication fails, the error log tells you why, and the status code differs by cause.
| Situation | Response | Log |
|---|---|---|
| Wrong password | 401 | user "alice": password mismatch |
| User not in the file | 401 | user "nobody" was not found in "…" |
| Worker cannot read the user file | 500 | open() "…" failed (13: Permission denied) |
| User file missing | 403 | open() "…" failed (2: No such file or directory) |
2026/10/25 11:32:36 [error] 2876#2876: *14 user "alice": password mismatch, client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "127.0.0.1:8080"2026/10/25 11:32:36 [error] 2876#2876: *15 user "nobody" was not found in "/etc/nginx/htpasswd", client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "127.0.0.1:8080"With the file set to mode 600 and owned by root, the www-data worker could not open it. A missing file looks like this:
2026/10/25 11:32:42 [crit] 2928#2928: *1 open() "/etc/nginx/htpasswd" failed (13: Permission denied), client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "127.0.0.1:8080"2026/10/25 11:32:43 [error] 2941#2941: *1 open() "/etc/nginx/nofile" failed (2: No such file or directory), client: 127.0.0.1, server: , request: "GET / HTTP/1.1", host: "127.0.0.1:8080"If the password is right but you get a 500 or a 403, check the user file's path and permissions first. The worker user (www-data) must be able to read it; it does not need to be world-readable. The test only showed mode 600 owned by root giving a 500; modes in between were not tested. A file made by htpasswd -c is usually readable by everyone, depending on the umask. To narrow it, chown root:www-data /etc/nginx/htpasswd && chmod 640 /etc/nginx/htpasswd gives read access to the worker group only (these two commands were not tested; use your worker's user and group names if they differ, and chmod 644 puts it back). The same permission logic is covered in the 403 post.
[6] Two malformed-file cases. A line without a colon placed after the alice line did not stop alice from logging in (200). A line with a bare password and no hash, such as kim:s3cret-pw, returned 401 even with the right password. To store plaintext you need the documented {PLAIN} prefix, which the docs say not to use, so treat it as test-only.
[7] To remove a user or check a password, use htpasswd rather than editing the file. -D deletes and -v verifies. The check also confirms that -c overwrites the file (f is a scratch file; creating carol with -c wiped the two users before it).
$ htpasswd -D f carol$ htpasswd -vb f dave pw4$ htpasswd -vb f dave badDeleting password for user carolPassword for user dave correct.password verification failedAnalyze your Nginx log in the browser ↗ — nothing is uploaded.
Check it with the htpasswd · bcrypt generator tool ↗ — calculated in your browser.