Nginx Cheatsheet

Password-Protect a website/directory

To generate password file:

sudo sh -c "echo -n '<your_username>:' >> /etc/nginx/.htpasswd"
sudo sh -c "openssl passwd -apr1 >> /etc/nginx/.htpasswd"

Inside the respective location block of your site:

location {
  ...
  auth_basic "Restricted Content";
  auth_basic_user_file /etc/nginx/.htpasswd;
}

Restart Nginx

sudo systemctl restart nginx

This post is Tagged with  the following keywords: