Using htpasswd to protect your website in Nginx

Summary

To implement server-level password protection for a website using Nginx, developers can leverage Nginx's auth_basic directive alongside an htpasswd file. This involves modifying the Nginx server configuration within a location block to specify a restricted area and the path to the password file. The htpasswd file itself requires each user's password to be encrypted using crypt(3), with an example PHP script demonstrating this process. Proper file permissions, such as chown root:www-data and chmod 640, are essential for security. Finally, reloading or restarting the Nginx server applies these changes, enabling basic authentication.

We need to build a password protected website frequently such as an internal website within the team, demo website. Here what we are talking about is password protection in Nginx server level instead of application level registration and login. We are going to use Nginx server configuration and htpasswd file to achieve password authentication.

The final result looks like below(Different browsers may have different interfaces):

Authentication Required

If the authentication fails, it will report a HTTP error: 401 Authorization Required.

To achieve this function, we need to modify server configuration and create the username and password for login.

First we need to modify Nginx's server configuration, this file is usually located at /etc/nginx/sites-enabled/ in Ubuntu. We assume the file is /etc/nginx/sites-enabled/default.

server {
    server_name www.fancycedar.info
    root /www/fancycedar
 
    # ...
 
    location / {
        # Add below two lines
        auth_basic      "Restricted";
        auth_basic_user_file  htpasswd;
        # ...
    }
 
    # ...
}

Next creating htpasswd, here are something to be noted:

htpasswd path

It can be put in the same level of directories as nginx.conf. It can be in /etc/nginx/ in Ubuntu.

htpasswd content

Each line stores one user, format is username:password. Here password cannot be plain text, it should be encrypted using crypt(3). You can use some PHP codes to generate the password of htpasswd.

<?php
// Password plaintext
$password = 'some password';
// Encrypt password
$password = crypt($password, base64_encode($password));
// The password encrypted
echo $password;
?>

Then save the password string to htpasswd.

username1:xucqMk13TfooE
username2:YXTfb3xWKOMBM
...

htpasswd permission

If need to change the permission of htpasswd, run below commands:

sudo chown root:www-data htpasswd
sudo chmod 640 htpasswd

Are you ready?

After above steps are done, we can proceed with loading and restarting Nginx server.

sudo /etc/init.d/nginx reload
# or
sudo /etc/init.d/nginx restart

Source : http://www.fancycedar.info/2013/06/apache-nginx-htpasswd/

NGINX PASSWORD PROTECTED HTPASSWD

  RELATED

  COMMENTS

2
Anonymous
Sep 18, 2017 at 7:01 am

Nice. 

Grabans.com

Mirko Tebaldi
Aug 24, 2021 at 8:13 am

How to protect all /* but not /api/* ?