Skip to content
ohsudo, Home

htpasswd Generator

Create .htpasswd lines with bcrypt (or APR1) for nginx and Apache basic authentication, check a password against a line, and copy the server config. Runs in your browser.

● Runs 100% in your browser — nothing you type leaves this page.

Need a strong one? Password Generator

.htpasswd line
Fill in a user name and a password, then Generate.
nginx
location /admin/ {
    auth_basic "Restricted";
    auth_basic_user_file /etc/nginx/.htpasswd;
}
Apache
<Location "/admin">
    AuthType Basic
    AuthName "Restricted"
    AuthUserFile /etc/apache2/.htpasswd
    Require valid-user
</Location>
The same with the htpasswd command
sudo htpasswd -B -C 10 -c /etc/nginx/.htpasswd anna   # -c creates (or empties!) the file
sudo htpasswd -B -C 10 /etc/nginx/.htpasswd another-user    # add a user
sudo chown root:www-data /etc/nginx/.htpasswd && sudo chmod 640 /etc/nginx/.htpasswd

Check a password against a line

How to use it

  1. Type a user name and a password. For a strong password, use the Password Generator.
  2. Keep bcrypt (the default) and click Generate. You get a user:hash line for your .htpasswd file.
  3. For several users, click Add to file after each one, then copy the whole file.
  4. Copy the nginx or Apache block under it, and point it at the file.

The password is hashed in your browser and never sent anywhere, nor kept in the page address.

bcrypt or APR1?

Plain text, {SHA} and DES crypt() lines aren’t offered: they’re too weak to recommend.

nginx and bcrypt

nginx checks the hash with the system’s crypt() function, so bcrypt works when the system’s C library supports it. The checks this site runs (see below) confirm it on the official nginx image and on nginx:alpine. If an older system answers every correct login with 401, check the error log, and switch to APR1 there.

Where to put the file

Terminal window
sudo chown root:www-data /etc/nginx/.htpasswd # "apache" on RHEL-based systems
sudo chmod 640 /etc/nginx/.htpasswd
sudo nginx -t && sudo systemctl reload nginx

Keep it outside the web root, so it can never be downloaded. Basic authentication sends the password with every request, so use it only over HTTPS.

FAQ

Can I check a password against an existing file?

Yes: paste one line of the file and the password under Check a password against a line. It works for bcrypt and APR1 lines.

The browser keeps asking for the password

The line doesn’t match: a typo, a user name with a different case, a file the server can’t read, or a different file than the one in the config. sudo nginx -T | grep auth_basic_user_file shows which file nginx uses. The nginx cheat sheet has the full block.

nginx answers without asking for a password

Look for a return in that location (or a rewrite … last to a place without auth_basic). return runs before nginx checks the password, so return 200 … answers everyone. Protect the location that serves the content, or the proxy_pass to your app, not a location that only returns.

Why is the line different every time?

Each hash gets a new random salt, so the same password never gives the same line. Every one of them is valid.

How is this checked?

Lines made by this tool are checked with Apache’s own htpasswd -v, and real nginx servers are asked to log in with them, on the official image and on Alpine; lines made by htpasswd are checked by the tool’s own verifier.