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
Configure SSL with LetsEncrypt and nginx Ashwin Kumar

Configuring SSL is beneficial not only for security purpose but also for SEO too.
Linuxsoftware foundation's initiated a program called Let’s Encrypt to give ssl ...

Continue Reading...
Setting Up Sentry - Web Application Event Tracking platform Jagadeesh V

When you want to track your exception and log mesages in a UI rather than storing it in a file(which we usually do), we can ...

Continue Reading...
Ansible Galaxy Introduction. Dinesh Deshmukh

Ansible Galaxy is the hub of ansible scripts contributed by users. To follow this article its important that you know about ansible. We have 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