Password Protecting Directories with Nginx on ubuntu

Ubuntu and Nginx together doesn’t provide out-of-the-box support for password protected directories. I’ll show you how to do it.

First you will need to install “htpasswd”. (apt-get is our friend, yet again)

root@zero:/# apt-get install apache2-utils libapache-htpasswd-perl

Then open your configuration for the site you are working on and add:

location /my_procted_area {
    auth_basic "Restricted";
    auth_basic_user_file /var/www/example.com/.htpasswd;
}

Where “my_procted_area” is the folder you want to protect and “/var/www/example.com/.htpasswd” is a path outside your web root.

Then create the password file:

root@zero:/# htpasswd -c /var/www/example.com/.htpasswd USERNAME

Where “USERNAME” is the username you want to use and “/var/www/example.com/.htpasswd” is a path outside your web root (same as above).

After typing that command you’ll be prompted to enter a password and then confirm that password like this:

New password: 
Re-type new password: 
Adding password for user USERNAME

And finally, restart Nginx:

root@zero:/# /etc/init.d/nginx restart
VN:F [1.9.22_1171]
Rating: 5.0/5 (1 vote cast)
Password Protecting Directories with Nginx on ubuntu, 5.0 out of 5 based on 1 rating

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>