• Home
  • Ubuntu 18.04
    • Whats New?
    • Upgrade Ubuntu
    • Install Java
    • Install Node.js
    • Install Docker
    • Install Git
    • Install LAMP Stack
  • Tutorials
    • AWS
    • Shell Scripting
    • Docker
    • Git
    • MongoDB
  • Funny Tools
  • FeedBack
  • Submit Article
  • About Us
TecAdmin
Menu
  • Home
  • Ubuntu 18.04
    • Whats New?
    • Upgrade Ubuntu
    • Install Java
    • Install Node.js
    • Install Docker
    • Install Git
    • Install LAMP Stack
  • Tutorials
    • AWS
    • Shell Scripting
    • Docker
    • Git
    • MongoDB
  • Funny Tools
  • FeedBack
  • Submit Article
  • About Us

How to Enable Basic Authentication on NGINX

Written by Rahul, Updated on April 24, 2017

Nginx is one of the popular web server used for web hosting. In this tutorial, you will learn to how to configure basic authentication on Nginx for your website. This tutorial will use htpasswd command utility from Apache tools package to generate encrypted credentials file.

1. Install Apache Tools

You need htpasswd command to create .htpasswd with encrypted login details. So install apache tools to get the htpasswd command on your system.

Using Apt-Get:

$ sudo apt-get install apache2-utils

Using Yum:

$ yum install httpd-tools
  • How To SetUp Nginx Virtual Hosts on Ubuntu and Debian

2. Create Credentials File

First you need to create an empty /etc/nginx/.htpasswd file if not exists. You can also do this using -c in htpasswd command. But this overwrites existing file and you may accidentaly overwrite existing file while adding more users.

$ touch /etc/nginx/.htpasswd

Above command will create new file or just change timestamp for existing file. Let’s start adding new users using htpasswd command.

$ htpasswd -m /etc/nginx/.htpasswd user1
$ htpasswd -m /etc/nginx/.htpasswd user2
  • -m is used for creating md5 encrypted passwords.

3. Edit Nginx Configuration

At this step, edit Nginx configuration file for your server block. Add following entry in the server block you need to authenticate.

server {
    listen       80 default_server;
    server_name  _;
    root         /usr/share/nginx/html;
    location / {
	auth_basic "Restricted Area";
	auth_basic_user_file /etc/nginx/.htpasswd;
    }
}

Update the location path to restrict specific application url of your web application.

 location /restricted/ {
    auth_basic "Restricted Area";
    auth_basic_user_file /etc/nginx/.htpasswd;
 }

4. Reload Nginx Server

To apply the changes to your server configuration reload Nginx server using the following commands.

$ sudo /etc/init.d/nginx reload

systemctl users can also use the below command.

$ sudo systemctl reload nginx.service

Share it!
Share on Facebook
Share on Twitter
Share on LinkedIn
Share on Reddit
Share on Tumblr
Share on Whatsapp
Rahul
Rahul
Connect on Facebook Connect on Twitter

I, Rahul Kumar am the founder and chief editor of TecAdmin.net. I am a Red Hat Certified Engineer (RHCE) and working as an IT professional since 2009..

Leave a Reply Cancel reply

Popular Posts

  • How to Install Python 3.9 on CentOS/RHEL 7 & Fedora 32/31 0
  • How To Install VNC Server on Ubuntu 20.04 1
  • How To Install NVM on macOS with Homebrew 0
  • (Solved) apt-add-repository command not found – Ubuntu & Debian 0
  • How to Install .NET Core on Debian 10 0
© 2013-2020 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy