Register now and start sharing your code snippets.
-->

Password protecting a folder/resource with Nginx

Shell Script (Bash) posted about 1 year ago by christian

First add the following to your Nginx configuration file:

   1  location / {
   2    auth_basic            "Restricted";
   3    auth_basic_user_file  /etc/nginx/htpasswd;
   4  }

Then create the htpasswd file:

   1  # this be passwords
   2  thisbetheusername:thisbeencryptedpass:yercomment

To generate a htpasswd password without installing Apache you can use the following Perl or Ruby code:

Perl

   1  perl -le 'print crypt("password", "salt")'

Ruby (run in irb)

   1  "password".crypt("salt")

The crypt() method uses 56-bit DES encryption, which is used in /etc/passwd and htpasswd.

Tagged perl, ruby, nginx, auth_basic, htpasswd