Password protect a directory (Apache, Nginx)

The simplest password protection on the web, what it really protects, and the settings that make it safe.

How it works: a credentials file and a server rule

Password protecting a directory relies on HTTP “Basic” authentication, supported by every browser and every web server. When a visitor requests a protected page, the server responds with a 401 status code; the browser then shows a prompt asking for a username and password, and sends them again with every request.

On the server side, you need two things: an .htpasswd file listing the authorized accounts, and a rule stating which directory to protect. The file contains one line per account, the username followed by the hashed password:

alice:$2y$12$0bR8XxXtVgP6sZkQd3Ue8uO3l9GJ7kR1bq3Yh5mS2nT4vW6xY8zAa
bob:$apr1$Wq1x2y3z$AbCdEfGhIjKlMnOpQrStU.

(These two lines show the format; the hashes are made up.) The server never stores the password itself: it hashes the one the visitor sends and compares.

It is the ideal solution for a staging site, an admin interface, internal documentation or a folder of files meant for a few people: no database, no change to the application.

Choosing the hash: bcrypt first

The hash determines how well the file holds up if it ever leaks. You will come across three formats:

  • bcrypt ($2y$ prefix): designed to be slow, with an adjustable cost. Each cost point doubles the computing time; a cost of 10 to 12 is a good trade-off. This is the recommended choice.
  • APR1-MD5 ($apr1$ prefix): Apache's variant, 1,000 iterations of MD5. It is still useful for servers that do not accept bcrypt, but it is very fast to compute, and therefore fast to attack if the file is copied.
  • SHA-1 ({SHA} prefix) and crypt: legacy formats, to replace with bcrypt as soon as possible.

The kit's .htpasswd generator produces bcrypt lines (cost 10 to 12) by default, and APR1 on request. The calculation runs in your browser.

Create the file from the command line

The htpasswd utility ships with Apache (the apache2-utils package on Debian and Ubuntu, httpd-tools on Red Hat, Rocky or Fedora). It works for Nginx too.

# Create the file with a first account, bcrypt cost 12
sudo htpasswd -c -B -C 12 /etc/apache2/.htpasswd alice

# Add an account (without -c, which would recreate the file)
sudo htpasswd -B -C 12 /etc/apache2/.htpasswd bob

# Delete an account
sudo htpasswd -D /etc/apache2/.htpasswd bob

The -c option creates the file by overwriting any existing one: use it only the first time. Without -C, htpasswd -B uses a cost of 5, a little low today. To get an APR1 line without Apache, openssl passwd -apr1 does the job.

Place the file outside the site's document root, so that it can never be downloaded, and make it readable by the web server only.

Apache: the .htaccess file

In the directory to protect, create an .htaccess file:

AuthType Basic
AuthName "Administration"
AuthUserFile /etc/apache2/.htpasswd
Require valid-user

AuthName is the text shown (depending on the browser) in the login prompt; Require valid-user accepts every account in the file, Require user alice only one. For Apache to read these directives from an .htaccess file, the site configuration must allow AllowOverride AuthConfig (or All) on that directory. If you have access to the server configuration, you can also put the same lines in a <Directory> or <Location> block. Apache 2.4 accepts bcrypt, APR1 and SHA-1.

Nginx: auth_basic

Nginx has no .htaccess: protection is declared in the site configuration.

location /admin/ {
    auth_basic           "Administration";
    auth_basic_user_file /etc/nginx/.htpasswd;
}

Check the syntax, then reload:

sudo nginx -t && sudo systemctl reload nginx

The file has the same format as for Apache. Nginx always accepts APR1 and SHA-1; for bcrypt, it relies on the system's crypt() function, so support depends on the distribution and its library. After adding a bcrypt account, test a login: if it is rejected with the right password, Nginx's error log says so, and an APR1 line takes over.

What the protection covers, and the settings that make it safe

The browser sends the username and password again with every request, merely encoded in Base64, which decodes instantly. The guide Basic Auth explained shows this in detail. Hence a few rules:

  • HTTPS is a must: it is what encrypts the header in transit. Redirect all HTTP traffic to HTTPS before turning on the protection.
  • One account per person: removing someone's access then takes a single command, with no change for anyone else.
  • Long, random passwords, generated rather than chosen.
  • A limit on attempts: Basic Auth has none built in; a tool such as fail2ban, which reads the server logs, blocks addresses that pile up failures.

Hand the credentials to the user

With Basic Auth, the administrator chooses the password: the user cannot change it themselves. So it has to be passed on to them. The generator sends onlyU the username and password in plain text, which is what the user needs; the hashed line goes on the server.

With “Send with onlyU”, these credentials go from the kit to onlyU tab to tab in your browser, without appearing in an address or going through the kit's server. onlyU encrypts them in your browser and creates a link that opens only once, valid for 24 hours, then disappears. It is free, with no sign-up. If the credentials may ever have been exposed, generate a new password and replace the line in the file: the old one stops working immediately.