HTTP Basic Auth for Nginx
August 4, 2024
Introduction #
Nginx has ability to enforce HTTP Basic Auth. Following are instruction on how to setup
- Creating a password through
htpasswdlike sosudo htpasswd -c /etc/nginx/.htpasswd ducky- To install
htpasswdcommand we need to runsudo apt-get install apache2-utils - The above command will prompt for password for username
ducky. Password file/etc/nginx/.htpasswdwill be created
- To install
- Once this is done, we need to modify nginx’s config, specifically
locationblock. 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