Create an .htpasswd File Without the htpasswd Command
Servers with only Nginx have no htpasswd command. Build .htpasswd lines and config snippets in the browser, tested on Nginx 1.24 on Ubuntu 24.04: choosing bcrypt or APR1, adding users and changing passwords, and telling a 401 from a 500 when login fails.
Contents
You want to show a client a site before it goes live, so you need a password on the staging environment only. Every guide says to run htpasswd -c /etc/nginx/.htpasswd user. On a server with only Nginx installed, though, that returns htpasswd: command not found. htpasswd is a utility that comes with Apache, packaged as apache2-utils on Debian and Ubuntu and httpd-tools on RHEL-based systems. So you end up either installing Apache packages just for Basic authentication or looking up how openssl passwd works all over again.
The .htpasswd Generator & Checker builds that one line in the browser. From a username and password, it produces a line in the same format as the htpasswd command, plus config snippets to paste into Apache, Nginx, Caddy and Traefik. It can also load an existing .htpasswd to add users, or check whether a password is the right one. Below, I used the tool, put the resulting lines on Nginx 1.24 on Ubuntu 24.04, and confirmed that logging in actually works.
Create an .htpasswd for two users and put it on Nginx
As an example, register two users on the staging environment: editor for the person building the site and client for the customer who will review it.
- Open the tool and type
editorin “Username” under “Enter users and passwords”. - Press “Add user” to add another pair of fields, and type
clientin the second “Username”. - Press “Generate” for each. A hard-to-guess 20-character password is filled in and shown as is. Use “Copy” to keep the ones you need to share.
- Leave “Hash format” at “bcrypt (recommended, htpasswd -B)” and “bcrypt cost” at “10 (recommended)”.
- Press “Create .htpasswd”. When “Created 2 line(s).” appears below, you are done.
The .htpasswd content and config snippets appear under “Your .htpasswd and config examples”.
editor:$2y$10$UdBqSn7pK3b8UrCblip2UusvfmJHs7.1/6B/QweXqrIP/olg4gke6
client:$2y$10$X9tt3KCfiZfT03DckaWTROO0r9gRaKUC1v4PF6PCDkUtU9Qjew9qC
Each line holds one user as username:hash, the same format the htpasswd command writes. The password itself is not stored in the file. $2y$ means bcrypt and the 10$ after it is the cost. Recreating a line with the same password gives a different value each time because a random salt goes in, and every one of them accepts the same password.
Then save the file with “Save .htpasswd” and put it at /etc/nginx/.htpasswd on the server. Add the content of the Nginx tab to the server block.
location / {
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/.htpasswd;
}
Check the syntax with nginx -t, then reload with nginx -s reload. With these two lines on my Ubuntu 24.04 (nginx 1.24.0), curl got a 200 with the right password and a 401 with a wrong one. Apache’s htpasswd -v also judged the lines from the tool “correct”.
Whether Nginx can read bcrypt depends on the OS
Only the Nginx column in the table says “Depends on OS crypt()”. Nginx itself only checks a few formats such as APR1-MD5 and {SHA}, and hands bcrypt and SHA-512 crypt to the OS crypt() function. Ubuntu 24.04 and recent Debian use libxcrypt for crypt(), which reads bcrypt’s $2y$, and it worked as is in this test. On older OSes, crypt() may not know bcrypt at all, and no password will let you in.
If you are dealing with an old server and are not sure, set “Hash format” to “APR1-MD5 (htpasswd default, widest support)”. Nginx checks APR1-MD5 by itself, so the OS does not matter, and the table shows “Yes” everywhere. The catch is that it only repeats MD5 1,000 times, which makes it weaker than bcrypt against brute force if the file leaks. Pick bcrypt where it is readable, and fall back to APR1-MD5 only when you find out it is not.
Each step up in bcrypt cost doubles the computation. htpasswd’s -B uses cost 5 when nothing is specified (it produced $2y$05$ in this environment too). The tool defaults to 10, a balance between resistance to brute force and the server load on every login. Going to 13 or more on a busy site makes the computation on each login a noticeable load.
Add users and change passwords
Once the staging site is in use, requests like “add one designer” or “change editor’s password” come up. Rebuilding .htpasswd from scratch means handing out everyone else’s passwords again. With the tool, you can load the current file and replace only the lines that change.
Suppose the server has the file below. admin was made with APR1-MD5, editor with bcrypt from the htpasswd command (cost 5), and legacy with the old 13-character DES crypt.
# staging users
admin:$apr1$LjeVd2Kj$pYeaR5BUNHFadwNTArMqZ/
editor:$2y$05$p4O4l.B/FAlBGLTGrfBmkOXobDlNrGlD8243MdncdJOFccfQwQtRa
legacy:77v6rgy6gozN.
- Open “Add to or edit an existing .htpasswd (optional)” and paste the file into “Current .htpasswd content” (or pick the file with “Open file”). “Loaded 3 user(s) (1 comment line(s)).” appears along with a table of users.
- In “Username” above, enter
editor, whose password changes, anddesigner, who is new, and press “Generate” for each. - The “Notes” for editor in the table says “Replaced by the line created above”.
- The “Notes” for legacy says “Legacy format that uses only the first 8 characters”. That user is no longer needed, so press “Remove”. The row is struck through and the button changes to “Undo”.
- Press “Create .htpasswd”.
In the result, the comment and admin’s line stay as they were, editor is replaced by a new $2y$10$ line, and designer is added at the end. admin’s line is carried over without touching the hash, so whoever does this does not need to know admin’s password. With this file on Nginx, admin, the new editor and designer got 200, while editor’s old password and the removed legacy got 401.
legacy gets a warning because DES crypt only uses the first 8 characters of a password. A line made from oldpass1 also accepts passwords like oldpass1-anything that differ only from the 9th character on. The tool does not check these lines either, so recreate any that remain.
When login fails, first find out whether the password is the problem
When you have set everything up but the login prompt keeps coming back, the cause is usually one of four: the password is remembered wrong, the file is not what you think it is, the server cannot read the file, or the server cannot read the format. The tool’s checker separates out the first two.
Under “Check whether a password matches” at the bottom of the page, check an .htpasswd line against the password you remember. If you loaded a file above, picking a user in “Pick from the loaded file” fills in the line. Here I picked admin, entered river-lamp-0417-cobalt in “Password” and pressed “Check”.
“Match.” means the line and password belong together. If even one character is off, such as Cobalt instead of cobalt at the end, you get “No match.” If they match and you still cannot log in, the cause is on the server, not the password.
On Nginx, server-side causes show up plainly in the error log (/var/log/nginx/error.log). I made it fail on purpose on my Nginx and compared the logs.
| What the browser shows | Error log | Cause and fix |
|---|---|---|
| Login prompt keeps coming back (401) | user "editor": password mismatch |
Wrong password or hash. Check it in the tool and recreate it if it does not match |
| Login prompt keeps coming back (401) | user "editor" was not found in "/etc/nginx/.htpasswd" |
Username typo. If only the first line of the file fails, suspect a BOM at the start |
| “500 Internal Server Error” | open() "…/.htpasswd" failed (13: Permission denied) |
The Nginx worker (www-data on Ubuntu) cannot read the file. Fix the owner and permissions |
The BOM in the second row is easy to miss. If you save the file as “UTF-8 with BOM” in a Windows editor, three invisible bytes are added at the start. When I tried a file with a BOM, client on the second line got in, but only editor on the first line was “was not found”. Nginx reads the first three bytes as part of the username, so as far as it is concerned there is no user named editor. Loading the file into the tool’s “Current .htpasswd content” strips the BOM, so saving the rebuilt file fixes it. Windows CRLF line endings on their own, on the other hand, worked fine on this Nginx.
The third row often happens when you create .htpasswd in your home directory and copy it with chmod 600 still set. When the worker cannot read it, Nginx returns a 500 instead of an authentication failure. If you see a 500 rather than a 401, you do not need to suspect the password.
Caddy and Traefik put the hash somewhere else
With Caddy and Traefik, you normally write the hash directly into the config instead of placing an .htpasswd file. Switching tabs in the tool shows the right syntax for each. Two things need attention.
The first is how few formats Caddy reads. Caddy’s basic_auth accepts only bcrypt and argon2id (made with Caddy’s own command), not APR1-MD5. If you reuse lines made with APR1-MD5 for Nginx, the Caddy column in the tool’s table turns “No” and a warning tells you to recreate them.
The second is $ when you configure Traefik through Docker Compose labels. Compose reads $ as the start of an environment variable, so a hash like $2y$10$… breaks if left as is. The “Traefik (Docker labels)” tab outputs it with each $ doubled to $$.
labels:
- "traefik.http.middlewares.basic-auth.basicauth.users=editor:$$2y$$10$$UdBqSn7pK3b8UrCblip2UusvfmJHs7.1/6B/QweXqrIP/olg4gke6,client:$$2y$$10$$X9tt3KCfiZfT03DckaWTROO0r9gRaKUC1v4PF6PCDkUtU9Qjew9qC"
- "traefik.http.middlewares.basic-auth.basicauth.realm=Restricted"
Join multiple users with commas. In Traefik’s dynamic configuration file (YAML), $ is not doubled, so use the “Traefik (file)” tab instead.
What Basic authentication can and cannot do
With Basic authentication, the browser sends the username and password on every request, only Base64-encoded. Nothing is encrypted, so on a site without HTTPS they can be read in transit. Put a certificate on the staging environment before you use it.
Basic authentication also has no attempt limit and no logout. It is enough for putting a single “staff only” door on a pre-launch site or an internal admin page, but it does not suit pages that need per-user permissions or two-factor authentication. With a password of about 20 characters from “Generate”, there is practically no risk of it being cracked by brute force. How length relates to strength is worked out in How long should a password be?.
The tool handles both the passwords and the file content inside your browser and sends them nowhere. Instead of installing htpasswd on the server, you can build the lines in your browser, put the file in place, and Basic authentication is ready.