HTTP Authentication with Nginx

If you want to add an authentication-dialog on your website, here's how you do it with Nginx.

Step 1 - create a .htpasswd file

There's multiple ways to generate a .htpasswd file, but for now we will install apache2-utils:

$ sudo apt-get install apache2-utils

Now when that's installed, generate the .htpasswd like so:

$ sudo htpasswd -c /var/www/pydocs/website/.htpasswd exampleuser

Alternatively you can create a htpasswd file online, but this might be less secure.

 

Step 2 - update your Nginx file

 server {
  listen       portnumber;
  server_name  ip_address;
  location / {
      root   /var/www/pydocs/webiste;
      auth_basic "Restricted";
      auth_basic_user_file /var/www/pydocs/website/.htpasswd;
  }
}

 

Step 3 - restart your Nginx server

$ sudo /etc/init.d/nginx reload