HTTP Basic Auth for Nginx

HTTP Basic Auth for Nginx

August 4, 2024
2024, Engineering
Nginx, Devops, Security

Introduction #

Nginx has ability to enforce HTTP Basic Auth. Following are instruction on how to setup

  1. Creating a password through htpasswd like so sudo htpasswd -c /etc/nginx/.htpasswd ducky
    1. To install htpasswd command we need to run sudo apt-get install apache2-utils
    2. The above command will prompt for password for username ducky. Password file /etc/nginx/.htpasswd will be created
  2. Once this is done, we need to modify nginx’s config, specifically location block. Contents of the file should loook as follow
        location /
        {
                auth_basic "Restricted Area";
                auth_basic_user_file /etc/nginx/.htpasswd;
                alias /var/www/html/ducky/;
                try_files $uri $uri/ =404;
                autoindex on;
                autoindex_exact_size off;
        }

Take special not of auth_basic and auth_basic_user_file lines