How to secure your Nginx HTTP Server using Basic HTTP Authentication

Prerequisites

apt-get install nginx apache2-utils

Nginx Configuration

Generate the username/password pairs using htpasswd

sudo htpasswd -c /etc/apache2/.htpasswd user1
sudo htpasswd /etc/apache2/.htpasswd user2

or use this website https://www.htaccessredirect.net/.

The Nginx Configuration would look like this

location /api {
    satisfy all;
    auth_basic           "Administrator’s Area";
    auth_basic_user_file /etc/apache2/.htpasswd;
    deny  192.168.1.2;
    allow 192.168.1.1/24;
    allow 127.0.0.1;
    deny  all;
}

The satisfy here means that both the IP and the passwords have to match

References


About Me

My name is Omar Qunsul. I write these articles mainly as a future reference for me. So I dedicate some time to make them look shiny, and share them with the public.

You can find me on twitter @OmarQunsul, and on Linkedin.


Homepage