How to setup http password authentication with nginx

Reading Time : ~ .

HTTP Authentication is used to allow access limit to a site or particular directories by validating the username and password.

Usernames and passwords are taken from a file created(.htaccess file) and we can have many no of username/password combinations to restrict the access.

HTTP basic authentication can also be combined with other access restriction methods, for example, restricting access by IP address or by geographical location.

In this tutorial, we'll learn how to add http authentication to nginx site using username and password

Step1:

You need to install nginx in your server to add HTTP authentication.

Nginx is one of the most popular web servers in the world. It can handles load balancing, content caching, web serving, security controls, and monitoring in one easy-to-use software package.

    sudo apt-get update

    sudo apt-get install nginx

We can know status of nginx server by the following command:

    sudo service nginx status

Step2:

We are using the htpassword command to store all the usernames and passwords to restrict access to a site.

apache2-utils package supports the htpassword command, so we also need to install apache-server

    sudo apt-get install apache2-utils


That password and the associated username will be stored in a file that you specify. The password will be encrypted and the name of the file can be anything you like.

Here, we use the file /etc/nginx/.httppassword and the username testuser.

    sudo htpasswd -c /etc/nginx/.httppassword testuser


We can check the username/password information in the file that we have specified at the time of creation

    nano /etc/nginx/.htpasswd

It will have all the username/passwords to site and will store all the passwords, and these will be encrypted using either bcrypt, MD5, crypt()

Step3:

Now we should add this username/pasword information to our nginx configuration.

Add the below lines to the configuration file of the website under the location section.

     auth_basic "Private Property";

     auth_basic_user_file /etc/nginx/.htpasswd;

The value of auth_basic is any string, and will be displayed at the authentication prompt, the value of auth_basic_user_file is the path to the password file that was created at username/pasword creation.

Then nginx will validate the given information to the actual username/password. If it's valid, then it allows to render the website pages, otherwise it will render the 401 Authorization Required message

Step4:

To apply the changes to a server, restart nginx.

    sudo service nginx reload

Now you can see HTTP authentication which will ask you a username/password to access the website. If You give correct credentials, you can access the website, otherwise it will return 401 authorization required message.

    By Posted On
SENIOR DEVELOPER at MICROPYRAMID

Need any Help in your Project?Let's Talk

Latest Comments
Related Articles
Django on GAE (google app engine) Ashwin Kumar

Google App Engine (often referred to as GAE or simply App Engine) is a platform as a service (PaaS) cloud computing platform for developing and ...

Continue Reading...
Celery With Supervisor Siva Chittamuru

Celery is a task queue with focus on real-time processing, while also supports task scheduling. Task queues are used as mechanisms to distribute work across ...

Continue Reading...
Postgresql Installation and management basics. Jagadeesh V

PostgreSQL or simply postgres is the most advanced, SQL-compliant and open-source objective-RDBMS. In This Article, you will learn how to Install, Connect and Manage a ...

Continue Reading...

Subscribe To our news letter

Subscribe to our news letter to receive latest blog posts into your inbox. Please fill your email address in the below form.
*We don't provide your email contact details to any third parties