Protect Nginx Directories with Apache2 HTTP Basic Authentication on Ubuntu 16.04 LTS

One thing that Nginx doesn’t have is HTTP Basic Authentication like what you have for Apache2 HTTP server.. However, you can use Apache2 utils to protect Nginx directories with basic password authentication… This brief tutorial shows students and new users how to use Apache2 utils to generate basic password authentication for Nginx directories.

This feature can be used to protect directories with basic passwords… It’s great feature and can be used to add additional layer of protection to existing directories..

To protect Nginx directories with basic password authentication, the steps below is a great place to start:

Step 1: Install Apache2 HTTP Utility

Again, Nginx doesn’t have this tool… so if you need to protect Nginx directories, install this Apache2 tool… To install, run the commands below

sudo apt install apache2-utils

Step 2: Create a .htpasswd file

Now that you’ve installed Apache2 utils, run the commands below to create password file for users.. The commands below will prompt you to create a new password for the username specified and store the file in the directory defined.. /etc/nginx/.htpasswd

sudo htpasswd -c /etc/nginx/.htpasswd myusername

Replace myusername with the username you wish to use. You can choose any directory to save the htpasswd file… however, you must specify the location when configuring Nginx…

After running the commands above, a new hidden passwd file for the username you selected will be saved in the /etc/nginx/.htpasswd file.

Below is the output of the command:

sudo htpasswd -c /etc/nginx/.htpasswd richard
New password:
Re-type new password:
Adding password for user richard

Step 3: Protect Nginx Directories

Now that the password file is created, use the highlighted code block below to protect a Nginx directory.

Nginx default site configuration file is at /etc/nginx/sites-available/default

Add these lines below to the directory you want to protect…

server {
    listen 80;
    listen [::]:80;
    root /var/www/html;
    index  index.php index.html index.htm;
    server_name  example.com www.example.com;

     client_max_body_size 100M;

    location / {
        try_files $uri $uri/ /index.php?$args;        
    }

    location ^~ /Private/ {
       auth_basic            "Restricted";
       auth_basic_user_file  /etc/nginx/.htpasswd;
    }
........
........

Save the file.

Everytime you attempt to browse to the /Private directory, you should get a basic HTTP authentication prompt to type a username and password.

To protect the entire Nginx site, define the root location to protect:  /var/www/html

Example:

server {
    listen 80;
    listen [::]:80;
    root /var/www/html;
    index  index.php index.html index.htm;
    server_name  example.com www.example.com;

     client_max_body_size 100M;

    location / {
        try_files $uri $uri/ /index.php?$args;        
    }

    location / {
        root /var/www/html;
        auth_basic            "Restricted";
        auth_basic_user_file  /etc/nginx/.htpasswd;
     }
.......
.......

Save the file.

The next time you browse to the domain, you will be prompted as show below…

nginx ubuntu htpasswd

That’s it!

You may also like the post below:

Setup WordPress with Apache2, MariaDB, PHP 7.1 and Varnish Proxy on Ubuntu 16.04 LTS Server

One Reply to “Protect Nginx Directories with Apache2 HTTP Basic Authentication on Ubuntu 16.04 LTS”

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: